static void AddEvent(TypeBuilder typeBuilder, EventInfo duckEvent, EventInfo evt, FieldBuilder duckField) { EventBuilder evtBuilder = typeBuilder.DefineEvent(evt.Name, EventAttributes.None, evt.EventHandlerType); var addMethod = typeBuilder.AddMethod(duckEvent.GetAddMethod(), evt.GetAddMethod(), duckField); evtBuilder.SetAddOnMethod(addMethod); var removeMethod = typeBuilder.AddMethod(duckEvent.GetRemoveMethod(), evt.GetRemoveMethod(), duckField); evtBuilder.SetRemoveOnMethod(removeMethod); }
static void AddProperty(TypeBuilder typeBuilder, PropertyInfo duckProp, PropertyInfo prop, FieldBuilder duckField) { PropertyBuilder propBuilder = typeBuilder.DefineProperty(prop.Name, PropertyAttributes.None, prop.PropertyType, prop.ParameterTypes()); if (prop.CanRead) { var getMethod = typeBuilder.AddMethod(duckProp.GetGetMethod(), prop.GetGetMethod(), duckField); propBuilder.SetGetMethod(getMethod); } if (prop.CanWrite) { var setMethod = typeBuilder.AddMethod(duckProp.GetSetMethod(), prop.GetSetMethod(), duckField); propBuilder.SetSetMethod(setMethod); } }
static void DefineMembers(Type duck, Type @interface, TypeBuilder typeBuilder, FieldBuilder duckField, ref bool defined) { foreach (var method in @interface.GetMethods().Where(mi => !mi.IsSpecialName)) // ignore get and set property methods { if (defined) { throw new InvalidCastException("More than one method one interface"); } CheckDelegateMatchesMethod(duck, @interface, method); var duckMethod = duck.GetMethod("Invoke"); typeBuilder.AddMethod(duckMethod, method, duckField); defined = true; } }
static void DefineMembers(Type duck, Type @interface, TypeBuilder typeBuilder, FieldBuilder duckField, MissingMethods missingMethods) { foreach (var method in @interface.GetTypeInfo().GetMethods().Where(mi => !mi.IsSpecialName)) // ignore get and set property methods { var duckMethod = duck.FindDuckMethod(method, PublicInstance, missingMethods); typeBuilder.AddMethod(duckMethod, method, duckField); } foreach (var prop in @interface.GetTypeInfo().GetProperties()) { var duckProp = duck.FindDuckProperty(prop, PublicInstance, missingMethods); AddProperty(typeBuilder, duckProp, prop, duckField); } foreach (var evt in @interface.GetTypeInfo().GetEvents()) { var duckEvent = duck.FindDuckEvent(evt, PublicInstance, missingMethods); AddEvent(typeBuilder, duckEvent, evt, duckField); } }
internal void AddMethod(Builder.MethodInfo method) { _typeBuilder.AddMethod(method); }