コード例 #1
0
 public Option SetOptionValue(string key, object value)
 {
     PropertyInfo[] propertyInfoList = this.Entity.GetType().GetProperties();
     foreach (PropertyInfo info in propertyInfoList)
     {
         object[] valueAttrs = info.GetCustomAttributes(false);
         foreach (var attr in valueAttrs)
         {
             if (attr is OptionValueAttribute)
             {
                 OptionValueAttribute valueAttr = (OptionValueAttribute)attr;
                 if (valueAttr.Name != key)
                 {
                     continue;
                 }
                 OnOptionChanging(new OptionChangeEventArgs(this, key, value));//选项值发生改变前的事件的注册
                 this.XmlElement.SetAttribute(valueAttr.Name, value.ToString());
                 Object obj = UtilityConvert.ConvertTo(this.XmlElement.GetAttribute(valueAttr.Name), info.PropertyType);
                 info.SetValue(this.Entity, obj, null);
                 OnOptionChanged(new OptionChangeEventArgs(this, key, value));//选项值发生改变后的事件的注册
             }
             else
             {
                 continue;
             }
         } //foreach
     }     //foreach PropertyInfo
     return(null);
 }
コード例 #2
0
        public static Option[] Load(Assembly ass, Type type, OptionAttribute optionAttr)
        {
            List <Option> options = new List <Option>();

            #region
            if (!optionAttr.IsCollection)//如果不是集合型的选项
            {
                #region
                Option option = new Option();
                option.Name = optionAttr.OptionSectionName;
                XmlNode optionNode = OptionManager.Instance.OptionDocument.DocumentElement.SelectSingleNode(optionAttr.OptionSectionName);
                if (optionNode != null)//当选项的xml节点可以从选项文件找到
                {
                    //从类型创建对象
                    option.Entity = UtilityType.CreateObject(ass, type.FullName, type, true, null);
                    //从类型获得该类型的所有属性
                    PropertyInfo[] propertyInfoList = type.GetProperties();

                    //为每个属性赋值
                    foreach (PropertyInfo info in propertyInfoList)
                    {
                        object[] valueAttrs = info.GetCustomAttributes(false);
                        foreach (var attr in valueAttrs)
                        {
                            if (attr is OptionValueAttribute)
                            {
                                OptionValueAttribute valueAttr     = (OptionValueAttribute)attr;
                                XmlElement           optionElement = (XmlElement)optionNode;
                                Object obj = UtilityConvert.ConvertTo(optionElement.GetAttribute(valueAttr.Name), info.PropertyType);
                                info.SetValue(option.Entity, obj, null);
                                option.XmlElement = optionElement;
                            }
                            else
                            {
                                continue;
                            }
                        } //foreach
                    }     //foreach PropertyInfo
                }
                #endregion
                OptionManager.Instance.Options.Add(option);
                option.OnOptionLoaded(new OptionLoadedEventArgs(option));
                options.Add(option);
            }
            else
            {
                #region
                XmlNode optionNode = OptionManager.Instance.OptionDocument.DocumentElement.SelectSingleNode(optionAttr.ParentSectionName);
                if (optionNode != null)//当选项的xml节点可以从选项文件找到
                {
                    if (optionNode.HasChildNodes)
                    {
                        foreach (XmlNode optionChildrenNode in optionNode)
                        {
                            Option option = new Option();
                            #region
                            if (optionChildrenNode.NodeType == XmlNodeType.Element)
                            {
                                option.Name = optionAttr.OptionSectionName;

                                //从类型创建对象
                                option.Entity = UtilityType.CreateObject(ass, type.FullName, type, true, null);
                                //从类型获得该类型的所有属性
                                PropertyInfo[] propertyInfoList = type.GetProperties();

                                //为每个属性赋值
                                foreach (PropertyInfo info in propertyInfoList)
                                {
                                    object[] valueAttrs = info.GetCustomAttributes(false);
                                    foreach (var attr in valueAttrs)
                                    {
                                        if (attr is OptionValueAttribute)
                                        {
                                            OptionValueAttribute valueAttr     = (OptionValueAttribute)attr;
                                            XmlElement           optionElement = (XmlElement)optionChildrenNode;
                                            Object obj = UtilityConvert.ConvertTo(optionElement.GetAttribute(valueAttr.Name), info.PropertyType);
                                            info.SetValue(option.Entity, obj, null);
                                            option.XmlElement = optionElement;
                                        }
                                        else
                                        {
                                            continue;
                                        }
                                    } //foreach
                                }     //foreach PropertyInfo
                            }
                            else
                            {
                                continue;
                            }
                            #endregion
                            OptionManager.Instance.Options.Add(option);
                            option.OnOptionLoaded(new OptionLoadedEventArgs(option));
                            options.Add(option);
                        }
                    }
                }
                #endregion
            }
            #endregion
            return(options.ToArray());
        }