示例#1
0
 public virtual object ResetPropertyDefaultValue(string PropertyName, out ValueDefaultType valueDefaultType)
 {
     throw new NotImplementedException();
 }
示例#2
0
 public void ResetAllPropertiesDefaultValue(ValueDefaultType filterType = (ValueDefaultType)(-1))
 {
     this.Clear();
 }
示例#3
0
 public object GetPropertyDefaultValue(string PropertyName, out ValueDefaultType valueDefaultType) => throw new NotImplementedException();
 public virtual void ResetAllPropertiesDefaultValue(ValueDefaultType filterType = (ValueDefaultType)(-1))
 {
     throw new NotImplementedException();
 }
        public static void ResetInstaceAllPropertiesByDefaultValue(this object Instance, ValueDefaultType overrideType = ValueDefaultType.All)
        {
            var properties = XConfig.Select_ResetAllDefaultProperties(Instance.GetType());

            foreach (var property in properties)
            {
                if (property.CanWrite)
                {
                    var value = Instance.GetPropertyDefaultValueEx(property, out var type);
                    if (overrideType.HasFlag(type))
                    {
                        property.SetValue(Instance, value);
                    }
                }
                else if (property.CanRead && property.GetValue(Instance) is IDefaultProperty propertyInstance)
                {
                    propertyInstance.ResetAllPropertiesDefaultValue(overrideType);
                }
            }
        }
 public static object GetPropertyDefaultValueEx(this object instance, PropertyInfo property, out ValueDefaultType defaultValueType)
 {
     if (property.GetCustomAttribute(typeof(DefaultValueAttribute)) is DefaultValueAttribute valueAttr)
     {
         defaultValueType = ValueDefaultType.ValueAttribute;
         return(valueAttr.Value);
     }
     if (property.GetCustomAttribute(typeof(DefaultValueByMethodAttribute)) is DefaultValueByMethodAttribute methodAttr)
     {
         defaultValueType = ValueDefaultType.MethodAttribute;
         var method = instance.GetType().GetMethod(methodAttr.MethodName);
         return(method.Invoke(instance, null));
     }
     if (property.GetCustomAttribute(typeof(DefaultValueNewInstanceAttribute)) is DefaultValueNewInstanceAttribute)
     {
         defaultValueType = ValueDefaultType.NewInstanceAttribute;
         return(GetDefault(property.PropertyType));
     }
     defaultValueType = ValueDefaultType.NoAttribute;
     if (property.PropertyType.IsValueType)
     {
         return(GetDefault(property.PropertyType));
     }
     return(null);
 }
        /// <summary>
        /// Get DefaultValue by DefaultValueAttribute or DefaultValueByMethodAttribute
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="PropertyName"></param>
        /// <param name="defaultValueType"></param>
        /// <returns></returns>
        public static object GetPropertyDefaultValueEx(this object instance, string propertyName, out ValueDefaultType defaultValueType)
        {
            var property = instance.GetType().GetProperty(propertyName);

            return(GetPropertyDefaultValueEx(instance, property, out defaultValueType));
        }