Esempio n. 1
0
        public static void AddAttribute(
            this TypeBuilder typeBuilder,
            AttributeSpecification attributeSpecification)
        {
            CustomAttributeBuilder customAttributeBuilder = CreateAttributeWithValuesFromTemplate(attributeSpecification);

            typeBuilder.SetCustomAttribute(customAttributeBuilder);
        }
Esempio n. 2
0
        private static CustomAttributeBuilder CreateAttributeWithValuesFromTemplate(AttributeSpecification attributeSpecification)
        {
            var propertiesWithValues  = new List <PropertyInfo>();
            var nonNullPropertyValues = new List <Object>();

            if (attributeSpecification.Template != null)
            {
                var properties = GetWritableProperties(attributeSpecification.Template);

                object[] propertyValues = GetPropertyValues(attributeSpecification.Template, properties);


                for (int i = 0; i < properties.Length; i++)
                {
                    if (propertyValues[i] == null)
                    {
                        continue;
                    }

                    propertiesWithValues.Add(properties[i]);
                    nonNullPropertyValues.Add(propertyValues[i]);
                }
            }

            ConstructorInfo constructor;

            if (attributeSpecification.Constructor != null)
            {
                constructor = attributeSpecification.Constructor;
            }
            else
            {
                constructor = attributeSpecification.Type.GetConstructor(new Type[] { });
            }

            object[] constructorParams;
            if (attributeSpecification.Constructor != null)
            {
                constructorParams = attributeSpecification.ConstructorParameters;
            }
            else
            {
                constructorParams = new object[] {}
            };


            return(new CustomAttributeBuilder(constructor, constructorParams,
                                              propertiesWithValues.ToArray(), nonNullPropertyValues.ToArray()));
        }
        private static CustomAttributeBuilder CreateAttributeWithValuesFromTemplate(AttributeSpecification attributeSpecification)
        {
            var propertiesWithValues = new List<PropertyInfo>();
            var nonNullPropertyValues = new List<Object>();

            if (attributeSpecification.Template != null)
            {
                var properties = GetWritableProperties(attributeSpecification.Template);

                object[] propertyValues = GetPropertyValues(attributeSpecification.Template, properties);


                for (int i = 0; i < properties.Length; i++)
                {
                    if (propertyValues[i] == null)
                        continue;

                    propertiesWithValues.Add(properties[i]);
                    nonNullPropertyValues.Add(propertyValues[i]);
                }
            }

            ConstructorInfo constructor;
            if (attributeSpecification.Constructor != null)
                constructor = attributeSpecification.Constructor;
            else
                constructor = attributeSpecification.Type.GetConstructor(new Type[] { });

            object[] constructorParams;
            if (attributeSpecification.Constructor != null)
                constructorParams = attributeSpecification.ConstructorParameters;
            else
                constructorParams = new object[] {};


            return new CustomAttributeBuilder(constructor, constructorParams, 
                propertiesWithValues.ToArray(), nonNullPropertyValues.ToArray());
        }
        public static void AddAttribute(
            this TypeBuilder typeBuilder,
            AttributeSpecification attributeSpecification)
        {
            CustomAttributeBuilder customAttributeBuilder = CreateAttributeWithValuesFromTemplate(attributeSpecification);

            typeBuilder.SetCustomAttribute(customAttributeBuilder);
        }