Пример #1
0
        /// <summary>
        /// Populates the Dictionary <see cref="ErrorCodeCatalogue" /> from
        /// all public Constants of the Type that is passed in.
        /// </summary>
        /// <param name="AErrorCodesType">Type that contains public error Constants.</param>
        public static void BuildErrorCodeInventory(Type AErrorCodesType)
        {
            object[]        ErrCodeAttributes;
            ErrCodeInfo     ErrCodeDetails = null;
            ErrCodeCategory ErrCodeCat;
            string          ErrCodeValue;

//TLogging.Log("BuildErrorCodeInventory: AErrorCodesType Type: " + AErrorCodesType.Name);

            FieldInfo[] ErrcodesFields = AErrorCodesType.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);

            try
            {
                for (int FieldCounter = 0; FieldCounter < ErrcodesFields.Length; FieldCounter++)
                {
                    FieldInfo FieldInf = ErrcodesFields[FieldCounter];
                    ErrCodeDetails = null;

                    // We are cataloging only public Constants!
                    if ((!((FieldInf.Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.InitOnly)) &&
                        ((FieldInf.Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public))
                    {
                        // Constants' identifier (name) must start with "ERR_"!
                        if (FieldInf.Name.StartsWith("ERR_"))
                        {
                            FieldInfo pi = AErrorCodesType.GetField(FieldInf.Name);
                            ErrCodeAttributes = pi.GetCustomAttributes(typeof(ErrCodeAttribute), false);

                            ErrCodeValue = FieldInf.GetValue(FieldInf).ToString();

                            if (ErrCodeValue.EndsWith("V"))
                            {
                                ErrCodeCat = ErrCodeCategory.Validation;
                            }
                            else if (ErrCodeValue.EndsWith("N"))
                            {
                                ErrCodeCat = ErrCodeCategory.NonCriticalError;
                            }
                            else
                            {
                                ErrCodeCat = ErrCodeCategory.Error;
                            }

                            if (ErrCodeAttributes.Length > 0)
                            {
                                foreach (ErrCodeAttribute Attr in ErrCodeAttributes)
                                {
                                    ErrCodeDetails = new ErrCodeInfo(ErrCodeValue, AErrorCodesType.FullName, FieldInf.Name,
                                                                     Attr.ShortDescription, Attr.FullDescription,
                                                                     Attr.ErrorMessageText, Attr.ErrorMessageTitle, ErrCodeCat, Attr.HelpID, Attr.ControlValueUndoRequested);
                                }
                            }
                            else
                            {
                                ErrCodeDetails = new ErrCodeInfo(ErrCodeValue, AErrorCodesType.FullName, FieldInf.Name,
                                                                 String.Empty, String.Empty,
                                                                 String.Empty, String.Empty, ErrCodeCat, String.Empty);
                            }

                            try
                            {
                                ErrorCodeCatalogue.Add(ErrCodeValue, ErrCodeDetails);
                            }
                            catch (ArgumentException)
                            {
                                ErrCodeInfo DefiningErrCodeInfo = ErrorCodes.GetErrorInfo(ErrCodeValue);

                                throw new EDuplicateErrorCodeException(
                                          String.Format(
                                              "An attempt to add Error Code with value '{0}' through constant '{1}' failed, as there is already an Error Code with that value: it is defined through constant '{2}'.",
                                              ErrCodeValue, AErrorCodesType.FullName + "." + FieldInf.Name,
                                              DefiningErrCodeInfo.ErrorCodeConstantClass + "." + DefiningErrCodeInfo.ErrorCodeConstantName));
                            }
                        }
                    }
                }
            }
            finally
            {
                CataloguedTypes.Add(AErrorCodesType.Name, AErrorCodesType);
//TLogging.Log("BuildErrorCodeInventory: Added " + AErrorCodesType.Name + " to the CataloguedTypes.");
//            System.Windows.Forms.MessageBox.Show("ErrorCodesInventory has " + ErrorCodeCatalogue.Count.ToString() + " Error Codes.");
            }
        }
Пример #2
0
        private static void ProcessFile(CSParser AParsedFile, ref Dictionary <string, ErrCodeInfo> AErrorCodes)
        {
            ErrCodeInfo     ErrCodeDetails = null;
            string          ErrCodeValue;
            string          ShortDescription = String.Empty;
            string          LongDescription  = String.Empty;
            ErrCodeCategory ErrCodeCat;

            foreach (TypeDeclaration t in AParsedFile.GetClasses())
            {
                foreach (object child in t.Children)
                {
                    if (child is INode)
                    {
                        INode node = (INode)child;
                        Type  type = node.GetType();

                        if (type.Name == "FieldDeclaration")
                        {
                            FieldDeclaration fd = (FieldDeclaration)node;

                            foreach (VariableDeclaration vd in fd.Fields)
                            {
                                foreach (AttributeSection attrSection in fd.Attributes)
                                {
                                    foreach (ICSharpCode.NRefactory.Ast.Attribute attr in attrSection.Attributes)
                                    {
                                        LongDescription = String.Empty;

                                        if (attr.Name == "ErrCodeAttribute")
                                        {
                                            ErrCodeValue = ((PrimitiveExpression)vd.Initializer).Value.ToString();

                                            if (ErrCodeValue.EndsWith("V"))
                                            {
                                                ErrCodeCat = ErrCodeCategory.Validation;
                                            }
                                            else if (ErrCodeValue.EndsWith("N"))
                                            {
                                                ErrCodeCat = ErrCodeCategory.NonCriticalError;
                                            }
                                            else
                                            {
                                                ErrCodeCat = ErrCodeCategory.Error;
                                            }

//                                        TLogging.Log("");
//                                        TLogging.Log("");
//                                        TLogging.Log("");
//                                        TLogging.Log(vd.Name + " = " + ((PrimitiveExpression)vd.Initializer).Value.ToString());

                                            foreach (Expression e in attr.PositionalArguments)
                                            {
//                                            TLogging.Log("ShortDescription: " + ShortDescription);
                                                ShortDescription = ((PrimitiveExpression)e).Value.ToString();
                                            }

                                            foreach (Expression e in attr.NamedArguments)
                                            {
                                                if (((NamedArgumentExpression)e).Name == "FullDescription")
                                                {
                                                    LongDescription = ExpressionToString(((NamedArgumentExpression)e).Expression);
                                                }

//                                            TLogging.Log("NamedArgumentExpression Name: " + LongDescription);
                                            }

                                            ErrCodeDetails = new ErrCodeInfo(ErrCodeValue, t.Name, vd.Name,
                                                                             ShortDescription, LongDescription,
                                                                             String.Empty, String.Empty, ErrCodeCat, String.Empty, false);

                                            AErrorCodes.Add(ErrCodeValue, ErrCodeDetails);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }