Esempio n. 1
0
        //check if class property has attribute with true value
        public bool GetAttrValue(System.Reflection.PropertyInfo propertyInfo_)
        {
            bool result = false;

            IList <System.Reflection.CustomAttributeData> attributeData = propertyInfo_.GetCustomAttributesData();

            foreach (System.Reflection.CustomAttributeData attributeData_ in attributeData)
            {
                string typeName = attributeData_.Constructor.DeclaringType.Name;
                if (typeName.EndsWith("Attribute"))
                {
                    typeName = typeName.Substring(0, typeName.Length - 9);
                }
                IList <CustomAttributeNamedArgument> namedArgument = attributeData_.NamedArguments;
                //attribs[typeName]
                foreach (System.Reflection.CustomAttributeNamedArgument namedArgument_ in namedArgument)
                {
                    if (namedArgument_.TypedValue.Value.GetType().Equals(typeof(Boolean)))
                    {
                        result = (bool)namedArgument_.TypedValue.Value;
                    }
                }
            }

            return(result);
        }
        private static string RealAttributeGetParamValue(System.Reflection.PropertyInfo param)
        {
            var colunmParam = "";
            var attr        = param.GetCustomAttributesData().ToList();

            foreach (var customAttributeTypedArgument in attr.Select(customAttributeData =>
                                                                     (customAttributeData.ConstructorArguments as
                                                                      System.Collections.ObjectModel.ReadOnlyCollection
                                                                      <System.Reflection.CustomAttributeTypedArgument>)).SelectMany(asd => asd))
            {
                colunmParam = customAttributeTypedArgument.Value.ToString();
            }
            return(colunmParam);
        }
Esempio n. 3
0
        private static void CreateDecoratedProperty(TypeBuilder tb, PropertyInfo prop, Type decoratorType, string decoratedPropertyName, Type decoratedType)
        {
            var propertyName = prop.Name;
            var propertyType = prop.PropertyType;

            var propertyBuilder = tb.DefineProperty(propertyName, PropertyAttributes.None, propertyType, null);

            foreach (var builder in prop.GetCustomAttributesData().Select(GetAttributeCopy))
                propertyBuilder.SetCustomAttribute(builder);

            var getPropMthdBldr = tb.DefineMethod("get_" + propertyName,
                MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig, propertyType,
                Type.EmptyTypes);

            var getIl = getPropMthdBldr.GetILGenerator();
            getIl.Emit(OpCodes.Ldarg_0);
            getIl.Emit(OpCodes.Castclass, decoratorType);
            getIl.Emit(OpCodes.Callvirt, decoratorType.GetProperty(decoratedPropertyName,
                BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).GetGetMethod(true));
            getIl.Emit(OpCodes.Callvirt, decoratedType.GetProperty(propertyName,
                BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).GetGetMethod(true));
            getIl.Emit(OpCodes.Ret);

            var setPropMthdBldr = tb.DefineMethod("set_" + propertyName,
                MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig, null,
                new[] { propertyType });

            propertyBuilder.SetGetMethod(getPropMthdBldr);

            var setIl = setPropMthdBldr.GetILGenerator();

            //var modifyProperty = setIl.DefineLabel();
            //var exitSet = setIl.DefineLabel();
            //setIl.MarkLabel(modifyProperty);
            setIl.Emit(OpCodes.Ldarg_0);
            //setIl.Emit(OpCodes.Castclass, decoratorType);
            setIl.Emit(OpCodes.Callvirt, decoratorType.GetProperty(decoratedPropertyName,
                BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).GetGetMethod(true));
            setIl.Emit(OpCodes.Ldarg_1);
            setIl.Emit(OpCodes.Callvirt, decoratedType.GetProperty(propertyName,
                BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).GetSetMethod(true));
            //setIl.Emit(OpCodes.Nop);
            //setIl.MarkLabel(exitSet);
            setIl.Emit(OpCodes.Ret);

            propertyBuilder.SetSetMethod(setPropMthdBldr);
        }
        static private void AppendPropertyInfo(PropertyInfo property, StringBuilder sb)
        {
            sb.Append(".property ");

            foreach (var attribute in property.GetCustomAttributesData())
            {
                AppendCustomAttributeData(attribute, sb);
                sb.Append(" ");
            }
            foreach (var modreq in property.GetRequiredCustomModifiers())
            {
                sb.Append("modreq(");
                AppendType(modreq, sb);
                sb.Append(") ");
            }
            foreach (var modopt in property.GetOptionalCustomModifiers())
            {
                sb.Append("modopt(");
                AppendType(modopt, sb);
                sb.Append(") ");
            }

            if (property.CanRead && property.CanWrite)
            {
                sb.Append("readwrite ");
            }
            else if (property.CanRead)
            {
                sb.Append("readonly ");
            }
            else if (property.CanWrite)
            {
                sb.Append("writeonly ");
            }

            if (property.Attributes.HasFlag(PropertyAttributes.SpecialName)) sb.Append("specialname ");
            if (property.Attributes.HasFlag(PropertyAttributes.RTSpecialName)) sb.Append("rtspecialname ");

            var propertyAccesors = property.GetAccessors();
            if (propertyAccesors.Length > 0)
            {
                sb.Append(propertyAccesors[0].IsStatic ? "static " : "instance ");
            }
            AppendType(property.PropertyType, sb);
            sb.Append(" ");
            sb.Append(property.Name);

            var indexParameters = property.GetIndexParameters();
            if (indexParameters.Length > 0)
            {
                sb.Append("(");
                foreach (var indexParameter in indexParameters)
                {
                    AppendParameterInfo(indexParameter, sb);
                    AppendComma(sb);
                }
                RemoveTrailingComma(sb);
                sb.Append(")");
            }
        }
 public void EnrichProperty(IProcessingContext context, PropertyInfo propertyInfo)
 {
     GenerateAttributeElements(context, propertyInfo.GetCustomAttributesData());
 }
Esempio n. 6
0
        /// <summary>
        /// Obtiene el custom attribute de la propiedad a traves del nombre
        /// </summary>
        /// <param name="property"></param>
        /// <param name="attributeName"></param>
        /// <returns></returns>
        public static System.Reflection.CustomAttributeData GetCustomAttributeByAttibute(System.Reflection.PropertyInfo property, string attributeName)
        {
            var namePropertyEntityAttribute = property.GetCustomAttributesData().FirstOrDefault(a => a.AttributeType.Name.Equals(attributeName));

            return(namePropertyEntityAttribute);
        }
Esempio n. 7
0
        private ValdrAttribute GetPropertyAttribute(PropertyInfo prop, string attributeName)
        {
            foreach (var data in prop.GetCustomAttributesData())
            {
                if (data.AttributeType.Name == attributeName)
                {
                    var attr = new ValdrAttribute();

                    if (attributeName == nameof(StringLengthAttribute) && data.ConstructorArguments.Count > 0)
                    {
                        attr.Maximum = (int)data.ConstructorArguments[0].Value;
                    }
                    else if (attributeName == nameof(RangeAttribute) && data.ConstructorArguments.Count > 1)
                    {
                        attr.Minimum = (int)data.ConstructorArguments[0].Value;
                        attr.Maximum = (int)data.ConstructorArguments[1].Value;
                    }
                    else if (attributeName == nameof(RegularExpressionAttribute) && data.ConstructorArguments.Count > 0)
                    {
                        attr.Pattern = (string)data.ConstructorArguments[0].Value;
                    }

                    if (data.NamedArguments != null)
                    {
                        foreach (var item in data.NamedArguments)
                        {
                            GetAttribute(attr, item.MemberName, item.TypedValue.Value);
                        }
                    }

                    return attr;
                }
            }

            return null;
        }
Esempio n. 8
0
        /// <summary>
        /// Gets the used namespaces in the property, including the property type 
        /// and also all of its custom attributes and attribute values.
        /// </summary>
        private static IEnumerable<string> GetUsedTypes(PropertyInfo property)
        {
            Guard.NotNull(() => property, property);

            var usedTypes = new[] { property.PropertyType.FullName }.AsEnumerable();

            var customAttributes = property.GetCustomAttributesData();

            // Add the namespaces of all attribute types
            usedTypes = usedTypes.Concat(customAttributes
                .Select(data => data.Constructor.DeclaringType.FullName));

            // Add the namespaces of all the parameter of all constructors
            usedTypes = usedTypes.Concat(customAttributes
                .SelectMany(data => data.ConstructorArguments)
                .Select(arg => arg.ArgumentType.FullName));

            // Add the namespaces of all the named parameters
            usedTypes = usedTypes.Concat(customAttributes
                .SelectMany(data => data.NamedArguments)
                .Select(arg => arg.TypedValue.ArgumentType.FullName));

            // Add the namespaces of all constructor arguments that are typeof(..)
            usedTypes = usedTypes.Concat(
                from attr in customAttributes
                from arg in attr.ConstructorArguments
                where arg.ArgumentType == typeof(Type)
                let type = (Type)arg.Value
                select type.FullName);

            // Add the namespaces of all named arguments that are typeof(..)
            usedTypes = usedTypes.Concat(
                from attr in customAttributes
                from arg in attr.NamedArguments
                where arg.TypedValue.ArgumentType == typeof(Type)
                let type = (Type)arg.TypedValue.Value
                select type.FullName);

            return usedTypes;
        }