Exemple #1
0
        public static void AvoidPropertyInstanceIsNull(object instance)
        {
            List <PropertyInfo> propertyInfos = instance.GetType().GetProperties().ToList();

            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                XmlIgnoreAttribute xmlIgnoreAttribute =
                    (XmlIgnoreAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(XmlIgnoreAttribute));
                NonMemberAttribute nonMemberAttribute =
                    (NonMemberAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(NonMemberAttribute));
                if (xmlIgnoreAttribute != null || nonMemberAttribute != null)
                {
                    continue;
                }
                object value = propertyInfo.GetValue(instance, null);
                if (value == null)
                {
                    Type type = propertyInfo.PropertyType;
                    if (type == typeof(string))
                    {
                        value = string.Empty;
                    }
                    else
                    {
                        value = Activator.CreateInstance(type);
                    }
                    propertyInfo.SetValue(instance, value, null);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// 屬性值複製, 將來源實體數值複製到目的實體, 主要作為繼承對象數值複製
        /// </summary>
        /// <param name="srcInstance">來源實體</param>
        /// <param name="destInstance">目的實體</param>
        public static void BlindingInstanceProperty(object srcInstance, object destInstance)
        {
            List <PropertyInfo> srcPropertyInfos  = srcInstance.GetType().GetProperties().ToList();
            List <PropertyInfo> destPropertyInfos = destInstance.GetType().GetProperties().ToList();
            List <PropertyInfo> propertyInfos     = new List <PropertyInfo>();

            for (int i = 0; i < srcPropertyInfos.Count; i++)
            {
                if (destPropertyInfos.Exists(p => p.PropertyType == srcPropertyInfos[i].PropertyType))
                {
                    propertyInfos.Add(srcPropertyInfos[i]);
                }
            }
            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                XmlIgnoreAttribute xmlIgnoreAttribute =
                    (XmlIgnoreAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(XmlIgnoreAttribute));
                NonMemberAttribute nonMemberAttribute =
                    (NonMemberAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(NonMemberAttribute));
                if (xmlIgnoreAttribute != null || nonMemberAttribute != null)
                {
                    continue;
                }
                object value = propertyInfo.GetValue(srcInstance, null);
                propertyInfo.SetValue(destInstance, value, null);
            }
        }
Exemple #3
0
 public bool Save(string folderPath, string fileName, string sectionName)
 {
     this.FolderPath  = folderPath;
     this.FileName    = fileName;
     this.SectionName = sectionName;
     try
     {
         fileName = string.IsNullOrEmpty(fileName) ? this.GetType().Name : fileName;
         IniFile        iniFile    = new IniFile(folderPath, fileName);
         PropertyInfo[] properties = this.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
         foreach (PropertyInfo property in properties)
         {
             NonMemberAttribute attribute =
                 (NonMemberAttribute)Attribute.GetCustomAttribute(property, typeof(NonMemberAttribute));
             if (attribute != null)
             {
                 continue;
             }
             object value = property.GetValue(this, null);
             if (value == null)
             {
                 continue;
             }
             string valueString = PropertyHelper.PropertyInfoGetValue(this, property);
             if (valueString != null)
             {
                 iniFile.WriteValue(sectionName, property.Name, valueString);
             }
         }
         return(true);
     }
     catch (Exception ex)
     {
         ExceptionHelper.MessageBoxShow(ex, "SystemHint");
         return(false);
     }
 }
Exemple #4
0
        public bool Load(string folderPath, string fileName, string sectionName)
        {
            this.FolderPath  = folderPath;
            this.FileName    = fileName;
            this.SectionName = sectionName;
            _isReadFile      = true;
            try
            {
                IniFile iniFile = new IniFile(folderPath, fileName);
                if (iniFile.GetSectionNames().Where(x => x == sectionName).Count() == 0)
                {
                    //找不到參數自動寫入預設值
                    Save(folderPath, fileName, sectionName);
                }
                PropertyInfo[] properties = this.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
                foreach (var property in properties)
                {
                    NonMemberAttribute nonMemberAttribute =
                        (NonMemberAttribute)Attribute.GetCustomAttribute(property, typeof(NonMemberAttribute));
                    if (nonMemberAttribute != null)
                    {
                        continue;
                    }

                    string defaultValue      = string.Empty;
                    object readInstanceValue = property.GetValue(this, null);
                    if (readInstanceValue != null)
                    {
                        defaultValue = PropertyHelper.PropertyInfoGetValue(this, property);
                        if (defaultValue == null)
                        {
                            //HalconDotNet.HObject不進行讀寫
                            continue;
                        }
                    }
                    DefaultValueAttribute defaultValueAttribute =
                        (DefaultValueAttribute)Attribute.GetCustomAttribute(property, typeof(DefaultValueAttribute));
                    if (defaultValueAttribute != null)
                    {
                        defaultValue = defaultValueAttribute.Value.ToString();
                    }
                    string readValue = iniFile.GetString(sectionName, property.Name, defaultValue);
                    if (string.IsNullOrEmpty(readValue))
                    {
                        continue;
                    }
                    PropertyHelper.PropertyInfoSetValue(this, property, readValue);
                }
                return(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("ParameterINI.Load({0}, {1}, {2}) Error", folderPath, fileName, sectionName));
                ExceptionHelper.MessageBoxShow(ex, "SystemHint");
                return(false);
            }
            finally
            {
                _isReadFile = false;
            }
        }