public void BuildConstructorTest(Type parentType, Type declaringType, Type classType) { // Arrange var typeBuilder = _proxyTypeBuilderFactory.CreateBuilder(parentType); if (declaringType.IsInterface) { typeBuilder.AddInterface(declaringType); } var constructorInfos = declaringType.GetConstructors(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); Assert.That(constructorInfos.Length, Is.EqualTo(1)); // Act typeBuilder.BuildConstructor(constructorInfos.First()); var type = typeBuilder.CreateType(); // Assert foreach (var interfaceType in declaringType.GetInterfaces()) { Assert.That(interfaceType.IsAssignableFrom(type), Is.True); } Assert.That(parentType.IsAssignableFrom(type), Is.True); Assert.That(declaringType.IsAssignableFrom(type), Is.True); var actualConstructorInfos = type.GetConstructors(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); var expectedConstructorInfos = classType.GetConstructors(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); Assert.That(actualConstructorInfos.Length, Is.EqualTo(expectedConstructorInfos.Length)); MemberAssert.AreEquivalent(actualConstructorInfos.Single(), expectedConstructorInfos.Single()); }
public void BuildMethodOverrideTest(Type parentType, Type declaringType, Type classType) { // Arrange var typeBuilder = _proxyTypeBuilderFactory.CreateBuilder(parentType); if (declaringType.IsInterface) { typeBuilder.AddInterface(declaringType); } var methodInfo = declaringType.GetMethod("Method"); Assert.That(methodInfo, Is.Not.Null); // Act typeBuilder.BuildMethod(methodInfo); var type = typeBuilder.CreateType(); // Assert Assert.DoesNotThrow(() => Activator.CreateInstance(type)); foreach (var interfaceType in declaringType.GetInterfaces()) { Assert.That(interfaceType.IsAssignableFrom(type), Is.True); } Assert.That(parentType.IsAssignableFrom(type), Is.True); Assert.That(declaringType.IsAssignableFrom(type), Is.True); var isExplicit = declaringType.IsInterface; MethodInfo actual; if (isExplicit) { actual = type.GetMethod(methodInfo.GetFullName(), BindingFlags.NonPublic | BindingFlags.Instance); } else { actual = type.GetMethod(methodInfo.Name); } var expected = classType.GetMethod(methodInfo.Name); MemberAssert.AreEquivalent(actual, expected, isExplicit); }
public void BuildMethodTest(Type parentType, Type declaringType, string methodName) { // Arrange var typeBuilder = _proxyTypeBuilderFactory.CreateBuilder(parentType); var methodInfo = declaringType.GetMethod(methodName); Assert.That(methodInfo, Is.Not.Null); // Act typeBuilder.BuildMethod(methodInfo); // Assert var type = typeBuilder.CreateType(); Assert.DoesNotThrow(() => Activator.CreateInstance(type)); var actual = type.GetMethod(methodName); var expected = declaringType.GetMethod(methodName); MemberAssert.AreEquivalent(actual, expected, false); }