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 PropertyNamePreservedOnOverride() { var classEmitter = new CustomClassEmitter(Scope, "PropertyNamePreservedOnOverride", typeof(ClassWithAllKindsOfMembers), Type.EmptyTypes, TypeAttributes.Public | TypeAttributes.Class, false); CustomPropertyEmitter property = classEmitter.CreatePropertyOverride(typeof(ClassWithAllKindsOfMembers).GetProperty("Property", _declaredInstanceBindingFlags)); Assert.That(property.PropertyBuilder.Name, Is.EqualTo("Property")); classEmitter.BuildType(); }
public void CreateIndexedPropertyOverride() { var classEmitter = new CustomClassEmitter(Scope, "CreateIndexedPropertyOverride", typeof(ClassWithAllKindsOfMembers), Type.EmptyTypes, TypeAttributes.Public | TypeAttributes.Class, false); PropertyInfo baseProperty = typeof(ClassWithAllKindsOfMembers).GetProperty("Item", _declaredInstanceBindingFlags); CustomPropertyEmitter property = classEmitter.CreatePropertyOverride(baseProperty); property.CreateGetMethod().ImplementByBaseCall(baseProperty.GetGetMethod()); property.CreateSetMethod().ImplementByBaseCall(baseProperty.GetSetMethod()); Type builtType = classEmitter.BuildType(); var instance = (ClassWithAllKindsOfMembers)Activator.CreateInstance(builtType); Assert.That(instance[17], Is.EqualTo("17")); instance[18] = "foo"; }
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 }