Пример #1
0
        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;
        }
Пример #2
0
        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;
            }
        }
Пример #3
0
        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);
            }
        }
 public void EnrichField(IProcessingContext context, FieldInfo fieldInfo)
 {
     GenerateAttributeElements(context, fieldInfo.GetCustomAttributesData());
 }
Пример #5
0
        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;
            }
        }
        /// <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;
        }