当前位置: 首页>>代码示例>>C#>>正文


C# PropertyInfo.GetCustomAttributesData方法代码示例

本文整理汇总了C#中System.Reflection.PropertyInfo.GetCustomAttributesData方法的典型用法代码示例。如果您正苦于以下问题:C# PropertyInfo.GetCustomAttributesData方法的具体用法?C# PropertyInfo.GetCustomAttributesData怎么用?C# PropertyInfo.GetCustomAttributesData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Reflection.PropertyInfo的用法示例。


在下文中一共展示了PropertyInfo.GetCustomAttributesData方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateDecoratedProperty

        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);
        }
开发者ID:altaricka,项目名称:vDesign,代码行数:47,代码来源:DecoratorBuilder.cs

示例2: AppendPropertyInfo

        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(")");
            }
        }
开发者ID:elemk0vv,项目名称:roslyn-1,代码行数:60,代码来源:MetadataSignatureHelper.cs

示例3: EnrichProperty

 public void EnrichProperty(IProcessingContext context, PropertyInfo propertyInfo)
 {
     GenerateAttributeElements(context, propertyInfo.GetCustomAttributesData());
 }
开发者ID:abclassic,项目名称:LBi.LostDoc,代码行数:4,代码来源:AttributeDataEnricher.cs

示例4: GetPropertyAttribute

        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;
        }
开发者ID:ljubomir,项目名称:valdr-dotnet,代码行数:36,代码来源:Parser.cs

示例5: GetUsedTypes

        /// <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;
        }
开发者ID:StevenVanDijk,项目名称:NuPattern,代码行数:44,代码来源:CodeGeneration.cs


注:本文中的System.Reflection.PropertyInfo.GetCustomAttributesData方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。