Пример #1
0
        /// <summary>
        /// Creates the Handler objects from the corresponding section in Loggers.config.
        /// </summary>
        /// <param name="config"><c>XmlConfiguration</c> section of Loggers.config to pull Handler object settings.</param>
        /// <returns></returns>
        private void FillHandlers(
            XmlConfiguration config)
        {
            NameValueCollection handlerColl = null;
            LogHandler handler = null;
            string handlerClass = null;
            String attrName = null;
            String attrValue = null;

            // Loop handler nodes
            for (int i = 1; i <= config.SelectNodes("//handlers/handler").Count; i++) {
                handlerColl = config.GetAttributes("//handlers/handler[" + i + "]");

                try {
                    // Create handler
                    handlerClass = handlerColl["class"];
                    handler = (LogHandler) Activator.CreateInstance(Type.GetType(handlerClass));

                    // Loop handler attributes and dynamically create the objects properties
                    for (int j = 0; j < handlerColl.Count; j++) {
                        attrName = handlerColl.GetKey(j);
                        attrValue = handlerColl.Get(j);
                        //  set the corresponding attribute of the object
                        SetObjectAttribute(handler, attrName, attrValue);
                    }
                    // Add handler to collection
                    this.AddHandler(handler);
                } catch (Exception e) {
                    string m = e.Message;
                }

                handler = null;
                handlerClass = null;
                handlerColl = null;
            }
        }
Пример #2
0
        public ArrayList Init(
            XmlConfiguration config)
        {
            ArrayList objList = new ArrayList();

            #region Removing code thats not used anymore
            /*
            MemberInfo[] attrList;
            string attrValue;
            Type aType;
            PropertyInfo aProperty;
            Object propertyValue = null;
            MethodInfo aMethod;
            */
            #endregion

            string className = config.GetValue("//object");
            string tableName = config.GetValue("//table");
            XmlNodeList columns = config.SelectNodes("//field");

            // Build string array
            string[] fields = new string[columns.Count];
            int i = 0;

            foreach (XmlNode node in columns) {
                fields.SetValue(node.Attributes["name"].Value, i);
                i++;
            }

            // Get data
            IGenericDao dao = DaoFactory.GetGenericDao();
            DataSet ds = dao.GetData(tableName, fields);
            DataTable dt = ds.Tables[tableName];

            // Loop through the data table to build the object list
            for (i = 0; i < dt.Rows.Count; i++) {
                DataRow dr = dt.Rows[i];

                Object anObj = Activator.CreateInstance(Type.GetType(className));

                NameValueCollection attributes = new NameValueCollection();

                foreach (XmlNode node in columns) {
                    attributes.Add(node.Attributes["object_attr"].Value, dr[node.Attributes["name"].Value].ToString());
                }

                ReflectionUtilities.Deserialize(anObj, attributes);

                #region Duplicate code from ReflectionUtilities
                /*
                aType = anObj.GetType();

                // Loop through the fields in the config file to set properties for the object
                foreach (XmlNode node in columns) {
                    attrList = aType.FindMembers(MemberTypes.Property,
                                                 BindingFlags.Instance | BindingFlags.Public,
                                                 this.FindMembersCanWriteDelegate,
                                                 //Type.FilterNameIgnoreCase,
                                                 node.Attributes["object_attr"].Value);
                    attrValue = dr[node.Attributes["name"].Value].ToString();
                    propertyValue = null;

                    // TODO: Add error tracking code here
                    if (attrList.Length == 1) {
                        aProperty = (PropertyInfo) attrList[0];

                        if (aProperty.PropertyType.IsEnum) {
                            // Use enumeration values defined in dbMapping if available.
                            NameValueCollection enumConfig =
                                (NameValueCollection) ConfigurationSettings.GetConfig("dbMappings/" + aProperty.PropertyType.Name);

                            if (enumConfig != null && enumConfig[attrValue] != null) {
                                attrValue = enumConfig[attrValue];
                            }

                            aMethod = aProperty.PropertyType.BaseType.GetMethod(
                                "Parse",
                                new Type[3]{
                                           	Type.GetType("System.Type"),
                                           	"".GetType(),
                                           	true.GetType()
                                           });

                            if ((aMethod != null) && (aMethod.IsStatic)) {
                                propertyValue = aMethod.Invoke(
                                    null,
                                    new object[3]{
                                                 	aProperty.PropertyType,
                                                 	attrValue,
                                                 	true
                                                 });
                            }
                        } else if (Type.GetTypeCode(aProperty.PropertyType) == Type.GetTypeCode("".GetType())) {
                            propertyValue = attrValue;
                        } else {
                            aMethod = aProperty.PropertyType.GetMethod("Parse", new Type[1]{"".GetType()});

                            if ((aMethod != null) && (aMethod.IsStatic)) {
                                try {
                                    propertyValue = aMethod.Invoke(
                                        null,
                                        new object[1]{attrValue});
                                } catch (Exception e) {
            // TODO: Logger name should not be hardcoded here!
                                    LogManager.GetCurrentClassLogger().Error(
                                        errorMessage => errorMessage("DbSingletonBase.Init: PropertyType = '{0}', attrValue = '{1}'",
                                                                     aProperty.PropertyType,
                                                                     attrValue),
                                                                     e);
                                }
                            }
                        }

                        try {
                            if (propertyValue != null) {
                                aMethod = aProperty.GetSetMethod();
                                aMethod.Invoke(anObj, new[]{propertyValue});
                            }
                        } catch (MissingMethodException e) {
                            // TODO: Handle MissingMethodException
                            LogManager.GetCurrentClassLogger().Error(
                                        errorMessage => errorMessage("Could not get the property value from field type on the object. Property Name: {0}, Property Type: {1} ",
                                                                     aProperty.PropertyType,
                                                                     attrValue),
                                                                     e);
                        }
                    }

                }*/
                #endregion

                objList.Add(anObj);
            }
            dt.Dispose();
            ds.Dispose();

            return objList;
        }