public void OverridingSimpleMembersOfOpenGenericClass() { CustomClassEmitter classEmitter = new CustomClassEmitter(Scope, "OverridingSimpleMembersOfOpenGenericClass", typeof(GenericClassWithAllKindsOfMembers <>), Type.EmptyTypes, TypeAttributes.Public | TypeAttributes.Class, false); MethodInfo baseMethod = typeof(GenericClassWithAllKindsOfMembers <>).GetMethod("Method"); var overriddenMethod = classEmitter.CreateMethodOverride(baseMethod); overriddenMethod.ImplementByBaseCall(baseMethod); PropertyInfo baseProperty = typeof(GenericClassWithAllKindsOfMembers <>).GetProperty("Property"); CustomPropertyEmitter overriddenProperty = classEmitter.CreatePropertyOverride(baseProperty); overriddenProperty.GetMethod = classEmitter.CreateMethodOverride(baseProperty.GetGetMethod()); overriddenProperty.GetMethod.ImplementByBaseCall(baseProperty.GetGetMethod()); EventInfo baseEvent = typeof(GenericClassWithAllKindsOfMembers <>).GetEvent("Event"); CustomEventEmitter overriddenEvent = classEmitter.CreateEventOverride(baseEvent); overriddenEvent.AddMethod = classEmitter.CreateMethodOverride(baseEvent.GetAddMethod()); overriddenEvent.AddMethod.ImplementByBaseCall(baseEvent.GetAddMethod()); overriddenEvent.RemoveMethod = classEmitter.CreateMethodOverride(baseEvent.GetRemoveMethod()); overriddenEvent.RemoveMethod.ImplementByBaseCall(baseEvent.GetRemoveMethod()); Type builtType = classEmitter.BuildType(); GenericClassWithAllKindsOfMembers <int> instance = (GenericClassWithAllKindsOfMembers <int>)Activator.CreateInstance(builtType.MakeGenericType(typeof(int))); instance.Method(5); Assert.That(instance.Property, Is.EqualTo(0)); instance.Event += delegate { return(0); }; instance.Event -= delegate { return(0); }; }
public void OverrideGenericMethod_WithConstraint_ModifiedByClosedGenericClass() { Type baseType = typeof(GenericClassWithGenericMethod <IConvertible, List <string>, int, object, IConvertible, List <List <IConvertible[]> > >); var classEmitter = new CustomClassEmitter( Scope, "OverrideGenericMethod_WithConstraint_ModifiedByClosedGenericClass", baseType, Type.EmptyTypes, TypeAttributes.Public | TypeAttributes.Class, false); MethodInfo baseMethod = baseType.GetMethod("GenericMethod"); var methodEmitter = classEmitter.CreateMethodOverride(baseMethod); methodEmitter.ImplementByBaseCall(baseMethod); Type builtType = classEmitter.BuildType(); var instance = (GenericClassWithGenericMethod <IConvertible, List <string>, int, object, IConvertible, List <List <IConvertible[]> > >) Activator.CreateInstance(builtType); string result = instance.GenericMethod(1, new List <int[]> (), new List <List <IConvertible[]> > ()); Assert.That(result, Is.EqualTo("1, System.Collections.Generic.List`1[System.Int32[]], System.Collections.Generic.List`1[System.Collections.Generic.List`1[" + "System.IConvertible[]]]")); }
public void MethodNameAndVisibilityArePreservedOnOverride() { var classEmitter = new CustomClassEmitter(Scope, "MethodNameAndVisibilityArePreservedOnOverride", typeof(ClassWithAllKindsOfMembers), new[] { typeof(IMarkerInterface) }, TypeAttributes.Public | TypeAttributes.Class, false); var toStringMethod = classEmitter.CreateMethodOverride(typeof(object).GetMethod("ToString", _declaredInstanceBindingFlags)); toStringMethod.AddStatement(new ReturnStatement(new ConstReference("P0wned!"))); var finalizeMethod = classEmitter.CreateMethodOverride(typeof(object).GetMethod("Finalize", _declaredInstanceBindingFlags)); finalizeMethod.AddStatement(new ReturnStatement()); var getterMethod = classEmitter.CreateMethodOverride(typeof(ClassWithAllKindsOfMembers).GetMethod("get_Property", _declaredInstanceBindingFlags)); getterMethod.AddStatement(new ReturnStatement()); Type builtType = classEmitter.BuildType(); MethodInfo overriddenToString = builtType.GetMethod("ToString", _declaredInstanceBindingFlags); Assert.That(overriddenToString.Name, Is.EqualTo("ToString")); Assert.That(overriddenToString.IsPublic, Is.True); Assert.That(overriddenToString.IsFinal, Is.False); Assert.That(overriddenToString.IsStatic, Is.False); Assert.That(overriddenToString.IsSpecialName, Is.False); Assert.That(overriddenToString.Attributes & MethodAttributes.ReuseSlot, Is.EqualTo(MethodAttributes.ReuseSlot)); MethodInfo overriddenFinalize = builtType.GetMethod("Finalize", _declaredInstanceBindingFlags); Assert.That(overriddenFinalize.Name, Is.EqualTo("Finalize")); Assert.That(overriddenFinalize.IsPublic, Is.False); Assert.That(overriddenFinalize.IsFamily, Is.True); Assert.That(overriddenFinalize.IsStatic, Is.False); Assert.That(overriddenFinalize.IsSpecialName, Is.False); Assert.That(overriddenToString.Attributes & MethodAttributes.ReuseSlot, Is.EqualTo(MethodAttributes.ReuseSlot)); MethodInfo overriddenGetter = builtType.GetMethod("get_Property", _declaredInstanceBindingFlags); Assert.That(overriddenGetter.Name, Is.EqualTo("get_Property")); Assert.That(overriddenGetter.IsPublic, Is.True); Assert.That(overriddenGetter.IsStatic, Is.False); Assert.That(overriddenGetter.IsSpecialName, Is.True); Assert.That(overriddenGetter.Attributes & MethodAttributes.ReuseSlot, Is.EqualTo(MethodAttributes.ReuseSlot)); }
public void EventNamePreservedOnOverride() { var classEmitter = new CustomClassEmitter(Scope, "EventNamePreservedOnOverride", typeof(ClassWithAllKindsOfMembers), Type.EmptyTypes, TypeAttributes.Public | TypeAttributes.Class, false); CustomEventEmitter eventEmitter = classEmitter.CreateEventOverride(typeof(ClassWithAllKindsOfMembers).GetEvent("Event", _declaredInstanceBindingFlags)); eventEmitter.AddMethod = classEmitter.CreateMethodOverride(typeof(ClassWithAllKindsOfMembers).GetMethod("add_Event", _declaredInstanceBindingFlags)); eventEmitter.AddMethod.AddStatement(new ReturnStatement()); eventEmitter.RemoveMethod = classEmitter.CreateMethodOverride(typeof(ClassWithAllKindsOfMembers).GetMethod("remove_Event", _declaredInstanceBindingFlags)); eventEmitter.RemoveMethod.AddStatement(new ReturnStatement()); Type builtType = classEmitter.BuildType(); Assert.That(builtType.GetEvent("Event", _declaredInstanceBindingFlags), Is.Not.Null); }
public void CreateEventOverride() { var classEmitter = new CustomClassEmitter(Scope, "CreateEventOverride", typeof(ClassWithAllKindsOfMembers), Type.EmptyTypes, TypeAttributes.Public | TypeAttributes.Class, false); CustomEventEmitter eventEmitter = classEmitter.CreateEventOverride(typeof(ClassWithAllKindsOfMembers).GetEvent("Event", _declaredInstanceBindingFlags)); eventEmitter.AddMethod = classEmitter.CreateMethodOverride(typeof(ClassWithAllKindsOfMembers).GetMethod("add_Event", _declaredInstanceBindingFlags)); eventEmitter.AddMethod.AddStatement(new ReturnStatement()); eventEmitter.RemoveMethod = classEmitter.CreateMethodOverride(typeof(ClassWithAllKindsOfMembers).GetMethod("remove_Event", _declaredInstanceBindingFlags)); eventEmitter.RemoveMethod.AddStatement(new ReturnStatement()); Type builtType = classEmitter.BuildType(); var instance = (ClassWithAllKindsOfMembers)Activator.CreateInstance(builtType); EventHandler eventHandler = delegate { }; instance.Event += eventHandler; instance.Event -= eventHandler; }
public void OverrideConstrainedGenericMethod() { CustomClassEmitter classEmitter = new CustomClassEmitter(Scope, "OverrideConstrainedGenericMethod", typeof(ClassWithConstrainedGenericMethod), Type.EmptyTypes, TypeAttributes.Public | TypeAttributes.Class, false); MethodInfo baseMethod = typeof(ClassWithConstrainedGenericMethod).GetMethod("GenericMethod"); var methodEmitter = classEmitter.CreateMethodOverride(baseMethod); methodEmitter.ImplementByBaseCall(baseMethod); Type builtType = classEmitter.BuildType(); ClassWithConstrainedGenericMethod instance = (ClassWithConstrainedGenericMethod)Activator.CreateInstance(builtType); string result = instance.GenericMethod("1", 2, "2"); Assert.That(result, Is.EqualTo("1, 2, 2")); }
public void CreateMethodOverride() { var classEmitter = new CustomClassEmitter(Scope, "CreateMethodOverride", typeof(object), new[] { typeof(IMarkerInterface) }, TypeAttributes.Public | TypeAttributes.Class, false); var toStringMethod = classEmitter.CreateMethodOverride(typeof(object).GetMethod("ToString")); toStringMethod.AddStatement(new ReturnStatement(new ConstReference("P0wned!"))); Type builtType = classEmitter.BuildType(); MethodInfo method = builtType.GetMethod("ToString", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly); Assert.That(method, Is.Not.Null); Assert.That(method.IsPublic, Is.True); object instance = Activator.CreateInstance(builtType); Assert.That(instance.ToString(), Is.EqualTo("P0wned!")); }
public void CreatePropertyOverride() { var classEmitter = new CustomClassEmitter(Scope, "CreatePropertyOverride", typeof(ClassWithAllKindsOfMembers), Type.EmptyTypes, TypeAttributes.Public | TypeAttributes.Class, false); CustomPropertyEmitter property = classEmitter.CreatePropertyOverride(typeof(ClassWithAllKindsOfMembers).GetProperty("Property", _declaredInstanceBindingFlags)); Assert.That(property.GetMethod, Is.Null); Assert.That(property.SetMethod, Is.Null); // only override getter, not setter property.GetMethod = classEmitter.CreateMethodOverride(typeof(ClassWithAllKindsOfMembers).GetMethod("get_Property", _declaredInstanceBindingFlags)); property.GetMethod.AddStatement(new ReturnStatement(new ConstReference(17))); Type builtType = classEmitter.BuildType(); var instance = (ClassWithAllKindsOfMembers)Activator.CreateInstance(builtType); Assert.That(instance.Property, Is.EqualTo(17)); instance.Property = 7; // inherited, not overridden }