public EventBuilder CreateInterceptableEvent(EventDefinition eventDefinition) { if (eventDefinition is null) { throw new ArgumentNullException(nameof(eventDefinition)); } var attributes = StaticReadOnlyFieldAttributes; FieldBuilder memberField = DefineField(eventDefinition.MemberFieldName, typeof(EventInfo), attributes); FieldBuilder addMethodField = DefineField(eventDefinition.AddMethodDefinition.CallerMethodFieldName, typeof(MethodInfo), attributes); FieldBuilder removeMethodField = DefineField(eventDefinition.RemoveMethodDefinition.CallerMethodFieldName, typeof(MethodInfo), attributes); EventBuilder eventBuilder = DefineEvent(eventDefinition.Name, eventDefinition.Attributes, eventDefinition.EventHandlerType); var addMethodDefinition = eventDefinition.AddMethodDefinition; MethodBuilder addMethodBuilder = interceptableMethodBuilder.CreateMethod(this, addMethodDefinition, eventDefinition.Event, memberField, addMethodField); AttributeHelper.AddInterceptorAttributes(addMethodBuilder, eventDefinition.AddOnInterceptorAttributes); eventBuilder.SetAddOnMethod(addMethodBuilder); var removeMethodDefinition = eventDefinition.RemoveMethodDefinition; MethodBuilder removeMethodBuilder = interceptableMethodBuilder.CreateMethod(this, removeMethodDefinition, eventDefinition.Event, memberField, removeMethodField); AttributeHelper.AddInterceptorAttributes(removeMethodBuilder, eventDefinition.RemoveOnInterceptorAttributes); eventBuilder.SetRemoveOnMethod(removeMethodBuilder); eventMappings.Add(new EventMapping(eventDefinition, eventBuilder, memberField, addMethodField, removeMethodField, addMethodBuilder, removeMethodBuilder)); return(eventBuilder); }
protected virtual void DefineAttributes(MethodBuilder methodBuilder, MethodDefinition methodDefinition) { if (ShouldAddInterceptionAttributes(methodDefinition)) { AttributeHelper.AddInterceptorAttributes(methodBuilder, methodDefinition.InterceptorAttributes); } }
public PropertyBuilder CreateInterceptableProperty(PropertyDefinition propertyDefinition) { if (propertyDefinition is null) { throw new ArgumentNullException(nameof(propertyDefinition)); } var attributes = StaticReadOnlyFieldAttributes; FieldBuilder memberField = DefineField(propertyDefinition.MemberFieldName, typeof(PropertyInfo), attributes); FieldBuilder getMethodField = null; FieldBuilder setMethodField = null; if (propertyDefinition.GetMethodDefinition != null) { getMethodField = DefineField(propertyDefinition.GetMethodDefinition.CallerMethodFieldName, typeof(MethodInfo), attributes); } if (propertyDefinition.SetMethodDefinition != null) { setMethodField = DefineField(propertyDefinition.SetMethodDefinition.CallerMethodFieldName, typeof(MethodInfo), attributes); } Type returnType = propertyDefinition.PropertyType; PropertyBuilder propertyBuilder = DefineProperty(propertyDefinition.Name, propertyDefinition.Attributes, returnType, new Type[] { returnType }); MethodBuilder getMethodBuilder = null; MethodBuilder setMethodBuilder = null; var getMethodDefinition = propertyDefinition.GetMethodDefinition; if (getMethodDefinition != null) { getMethodBuilder = interceptableMethodBuilder.CreateMethod(this, getMethodDefinition, propertyDefinition.Property, memberField, getMethodField); AttributeHelper.AddInterceptorAttributes(getMethodBuilder, propertyDefinition.GettterInterceptorAttributes); propertyBuilder.SetGetMethod(getMethodBuilder); } var setMethodDefinition = propertyDefinition.SetMethodDefinition; if (setMethodDefinition != null) { setMethodBuilder = interceptableMethodBuilder.CreateMethod(this, setMethodDefinition, propertyDefinition.Property, memberField, setMethodField); AttributeHelper.AddInterceptorAttributes(setMethodBuilder, propertyDefinition.SetterInterceptorAttributes); propertyBuilder.SetSetMethod(setMethodBuilder); } propertyMappings.Add(new PropertyMapping(propertyDefinition, memberField, getMethodField, setMethodField, propertyBuilder, getMethodBuilder, setMethodBuilder)); return(propertyBuilder); }
private void AddCustomAttributes() { typeBuilder.SetCustomAttribute(new CustomAttributeBuilder(Constructors.SerializableAttributeConstructor, new object[0])); typeBuilder.SetCustomAttribute(new CustomAttributeBuilder(Constructors.XmlIncludeAttributeConstructor, new object[] { typeDefinition.Type })); var options = typeDefinition.Options; if (options != null) { foreach (var additionalTypeAttribute in options.AdditionalTypeAttributes) { typeBuilder.SetCustomAttribute(new CustomAttributeBuilder(additionalTypeAttribute.Constructor, additionalTypeAttribute.Args)); } } if (typeDefinition.TypeDefinitionType == TypeDefinitionType.InterfaceProxy) { AttributeHelper.AddInterceptorAttributes(typeBuilder, typeDefinition.InterceptorAttributes); } }