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


C# FieldInfo.GetCustomAttributesData方法代码示例

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


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

示例1: GetAttributesTextFor

        public virtual IEnumerable<string> GetAttributesTextFor(FieldInfo field, Usage defaultUsage, ParsingPolicyAttribute[] parsingPolicies)
        {
            var result = new List<string>();
            var fieldType = field.FieldType;
            string usageAttribute = null;
            bool isForcedUsage = defaultUsage == Usage.ForceRequired || defaultUsage == Usage.ForceOptional;
            if (isForcedUsage)
            {
                if (defaultUsage == Usage.ForceRequired) usageAttribute = AttributeBuilder.GetTextFor<RequiredAttribute>();
                else if (defaultUsage == Usage.ForceOptional) usageAttribute = AttributeBuilder.GetTextFor<OptionalAttribute>();
            }
            else if (fieldType.IsValueType && Nullable.GetUnderlyingType(field.FieldType) == null)
            {
                usageAttribute = AttributeBuilder.GetTextFor<RequiredAttribute>();
            }
            else if (Nullable.GetUnderlyingType(field.FieldType) != null)
            {
                usageAttribute = AttributeBuilder.GetTextFor<OptionalAttribute>();
            }
            else if (!field.IsDefined<RequiredAttribute>() && !field.IsDefined<OptionalAttribute>())
            {
                if (defaultUsage == Usage.Required) usageAttribute = AttributeBuilder.GetTextFor<RequiredAttribute>();
                else if (defaultUsage == Usage.Optional) usageAttribute = AttributeBuilder.GetTextFor<OptionalAttribute>();
            }
            if (usageAttribute != null)
                result.Add(usageAttribute);

            var attributes = field.GetCustomAttributes().ToArray();
            var attributesData = field.GetCustomAttributesData();
            for (int i = 0; i < attributes.Length; i++)
            {
                // Skip any usage attributes, if field usage is forced by the calling method.
                bool isUsageAttribute = attributes[i] is OptionalAttribute || attributes[i] is RequiredAttribute;
                if (isForcedUsage && isUsageAttribute) continue;
                // Get c# compatible attribute text.
                string attributeText = CommonAttributeTranslator.Translate(attributes[i], attributesData[i], field);
                if (attributeText != null)
                    result.Add(attributeText);
            }
            return result;
        }
开发者ID:lukyad,项目名称:Eco,代码行数:41,代码来源:CommonAttributesGenerator.cs

示例2: _getStringLength

        private int _getStringLength(FieldInfo fieldInfo)
        {
            try
            {
                int length = 0;
                foreach (Attribute attr in fieldInfo.GetCustomAttributes(false))
                {
                    if (attr is MaxLengthAttribute)
                    {

                        var attributeData = fieldInfo.GetCustomAttributesData();
                        CustomAttributeData cd = attributeData[0];
                        length = (int)cd.ConstructorArguments[0].Value;
                    }

                }
                return length;
            }
            catch (Exception)
            {
                throw;
            }
        }
开发者ID:kocyigityunus,项目名称:sharpBasicOrm,代码行数:23,代码来源:SqlTextBuilder.cs

示例3: AppendFieldInfo

        static private void AppendFieldInfo(FieldInfo field, StringBuilder sb)
        {
            sb.Append(".field ");

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

            if (field.IsPrivate) sb.Append("private ");
            if (field.IsPublic) sb.Append("public ");
            if (field.IsFamily) sb.Append("family ");
            if (field.IsAssembly) sb.Append("assembly ");
            if (field.IsFamilyOrAssembly) sb.Append("famorassem ");
            if (field.IsFamilyAndAssembly) sb.Append("famandassem ");

            if (field.IsInitOnly) sb.Append("initonly ");
            if (field.IsLiteral) sb.Append("literal ");
            if (field.IsNotSerialized) sb.Append("notserialized ");
            if (field.Attributes.HasFlag(FieldAttributes.SpecialName)) sb.Append("specialname ");
            if (field.Attributes.HasFlag(FieldAttributes.RTSpecialName)) sb.Append("rtspecialname ");
            if (field.IsPinvokeImpl) sb.Append("pinvokeimpl ");

            sb.Append(field.IsStatic ? "static " : "instance ");
            AppendType(field.FieldType, sb);
            sb.Append(" ");
            sb.Append(field.Name);

            if (field.IsLiteral)
            {
                AppendValue(field.GetRawConstantValue(), sb);
            }
        }
开发者ID:elemk0vv,项目名称:roslyn-1,代码行数:46,代码来源:MetadataSignatureHelper.cs

示例4: EnrichField

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

示例5: validateAttribute

        public void validateAttribute(Attribute attr, FieldInfo fInfo)
        {
            try
            {
                object obj = fInfo.GetValue(this);

                if (attr is RequiredAttribute)
                {
                    if (obj == null)
                        throw new Exception("RequiredAttribute exception. Required Fields must not equal to null");

                    if (obj.ToString().Length == 0 )
                        throw new Exception("RequiredAttribute exception. string lengths  must not equal to zero");

                }

                if (attr is MaxLengthAttribute)
                {
                    var attributeData = fInfo.GetCustomAttributesData();
                    CustomAttributeData cd = attributeData[0];
                    int uzunluk = (int)cd.ConstructorArguments[0].Value;

                    string objString = (string)obj;

                    if (objString.Length > uzunluk)
                        throw new Exception("MaxLengthAttribute exception.");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
开发者ID:kocyigityunus,项目名称:sharpBasicOrm,代码行数:33,代码来源:ModelBase.cs

示例6: SimpleTypeObjectToNode

        /// <summary>
        /// Convert simple type object to TreeNode
        /// </summary>
        /// <param name="obj">The object which need to convert</param>
        /// <param name="parentobj">The parent object of the object which need to convert</param>
        /// <param name="Info">The field Info of the object in parent object</param>
        /// <param name="startIndex">The start index of this object in message stream</param>
        /// <param name="offset">The offset of this object</param>
        /// <returns>The TreeNode with object value information</returns>
        public static TreeNode SimpleTypeObjectToNode(object obj, object parentobj, FieldInfo Info, ref int startIndex, out int offset)
        {
            int os = 0;
            TreeNode tn = new TreeNode(string.Format("{0}:{1}", Info.Name, obj.ToString()));
            Type type = obj.GetType();
            if (Info.GetCustomAttributesData().Count != 0 && Info.GetCustomAttributes(typeof(BitAttribute), false) != null)
            {
                Type parentType = parentobj.GetType();
                FieldInfo[] FieldsInparent = parentType.GetFields();
                List<int> FieldsWithBitAttribute = new List<int>();
                foreach (FieldInfo f in FieldsInparent)
                {
                    if (f.GetCustomAttributesData().Count != 0 && f.GetCustomAttributes(typeof(BitAttribute), false) != null)
                    {
                        BitAttribute attribute = (BitAttribute)f.GetCustomAttributes(typeof(BitAttribute), false)[0];
                        FieldsWithBitAttribute.Add(attribute.BitLength);

                        if (f == Info)
                            break;
                    }
                }
                int FieldsBitLength = FieldsWithBitAttribute.Sum();
                int[] Fields = FieldsWithBitAttribute.ToArray();

                int n = FieldsBitLength / 8;
                int r = FieldsBitLength % 8;
                int thisFieldBitLength = Fields[Fields.Length - 1];
                int thisn = thisFieldBitLength / 8;
                int forntEleSum = FieldsBitLength - thisFieldBitLength; // This value indicate the fields sum before this field.
                for (int i = FieldsWithBitAttribute.Count - 2; i > 0; i--) // This loop is used to update n and r, if there are fields sum is xompleted bytes, so n and r should subtract the byte length.
                {
                    forntEleSum -= Fields[i];
                    if (forntEleSum % 8 == 0)
                    {
                        n = (FieldsBitLength - forntEleSum) / 8;
                        r = (FieldsBitLength - forntEleSum) % 8;
                    }
                }
                if (FieldsWithBitAttribute.Count != 1 && (FieldsBitLength - thisFieldBitLength) % 8 != 0)
                {
                    startIndex -= 1;
                }
                if (thisn < n)
                {
                    if ((FieldsBitLength - thisFieldBitLength) % 8 == 0)// This is for bit field which is start from a new byte, but is not the first bit filed in parent field.
                    {
                        os += (thisn + 1);
                    }
                    else
                    {
                        if (r != 0)
                            os += (thisn + 2);
                        else
                            os += (thisn + 1);
                    }
                }
                else
                {
                    os += (thisn + 1);
                }
            }
            else
            {
                if (type.Name == "String")
                {
                    os = ((string)obj).Length * 2;
                }
                else if (type.Name != "Boolean")
                {
                    os = Marshal.SizeOf(type);
                }
                else
                {
                    os = sizeof(Boolean);
                }
            }

            Position ps = new Position(startIndex, os);
            tn.Tag = ps;
            startIndex += os;
            offset = os;
            return tn;
        }
开发者ID:OfficeDev,项目名称:Office-Inspectors-for-Fiddler,代码行数:92,代码来源:BaseStructure.cs


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