Пример #1
0
 public static void SetPropertyValues <T>(T source, Dictionary <string, string> values)
 {
     if ((object)source == null || values == null || values.Count == 0)
     {
         return;
     }
     foreach (PropertyInfo property in source.GetType().GetProperties())
     {
         try
         {
             string str = values[property.Name];
             if (str != null)
             {
                 Type type = property.GetValue((object)source, (object[])null).GetType();
                 try
                 {
                     object obj = SFClass.ValueAs(type, str);
                     property.SetValue((object)source, obj, (object[])null);
                 }
                 catch
                 {
                 }
             }
         }
         catch
         {
         }
     }
 }
Пример #2
0
        public static T ConvertStringToObject <T>(string value, T defaultValue)
        {
            T obj = default(T);

            try
            {
                if (string.IsNullOrEmpty(value))
                {
                    return(defaultValue);
                }
                Type type = typeof(T);
                try
                {
                    return((T)SFClass.ValueAs(type, value));
                }
                catch
                {
                    return(defaultValue);
                }
            }
            catch
            {
                return(defaultValue);
            }
        }
Пример #3
0
 public static bool IsBrowsable(object source, string propertyName)
 {
     if (source == null || string.IsNullOrEmpty(propertyName))
     {
         return(false);
     }
     return(SFClass.IsBrowsable(SFClass.GetPropertyInfo(source, propertyName)));
 }
Пример #4
0
 public static string Category(object source, string propertyName)
 {
     if (source == null || string.IsNullOrEmpty(propertyName))
     {
         return("Misc");
     }
     return(SFClass.Category(SFClass.GetPropertyInfo(source, propertyName)));
 }
Пример #5
0
 public static void CloneObject(object cloneFrom, object cloneTo)
 {
     foreach (PropertyInfo propertyInfo in SFClass.PropertyInfoGetAll(cloneFrom))
     {
         try
         {
             SFClass.SetPropertyValue(cloneTo, propertyInfo.Name, propertyInfo.GetValue(cloneFrom));
         }
         catch
         {
         }
     }
 }
Пример #6
0
        public static void SetPropertyValue(object source, string propertyName, object value)
        {
            if (source == null || value == null || string.IsNullOrEmpty(propertyName))
            {
                return;
            }
            PropertyInfo propertyInfo = SFClass.GetPropertyInfo(source, propertyName);

            if (!(propertyInfo != (PropertyInfo)null))
            {
                return;
            }
            try
            {
                if (propertyInfo.PropertyType == value.GetType())
                {
                    if (value.GetType().IsGenericType)
                    {
                        object     obj    = propertyInfo.GetValue(source);
                        IList      list   = (IList)value;
                        MethodInfo method = obj.GetType().GetMethod("Add");
                        obj.GetType().GetMethod("Clear").Invoke(obj, (object[])null);
                        for (int index = 0; index < list.Count; ++index)
                        {
                            method.Invoke(obj, new object[1]
                            {
                                list[index]
                            });
                        }
                    }
                    else
                    {
                        propertyInfo.SetValue(source, value);
                    }
                }
                else
                {
                    try
                    {
                        propertyInfo.SetValue(source, SFClass.ValueAs(propertyInfo.PropertyType, value.ToString()));
                    }
                    catch
                    {
                    }
                }
            }
            catch
            {
            }
        }
Пример #7
0
 public static void CopyObject <T>(T sourceObject, T valuesObject)
 {
     if ((object)sourceObject == null || (object)valuesObject == null)
     {
         return;
     }
     PropertyInfo[] properties = sourceObject.GetType().GetProperties();
     valuesObject.GetType().GetProperties();
     for (int index = 0; index < properties.Length; ++index)
     {
         if (properties[index].CanWrite)
         {
             properties[index].SetValue((object)valuesObject, SFClass.GetPropertyValue((object)sourceObject, properties[index].Name), (object[])null);
         }
     }
 }
Пример #8
0
        public static object CloneObject(object source)
        {
            object instance = Activator.CreateInstance(source.GetType());

            foreach (PropertyInfo propertyInfo in SFClass.PropertyInfoGetAll(source))
            {
                try
                {
                    SFClass.SetPropertyValue(instance, propertyInfo.Name, propertyInfo.GetValue(source));
                }
                catch
                {
                }
            }
            return(instance);
        }
Пример #9
0
        public static Dictionary <string, string> GetListAsDictionary <T>(List <T> sourceObject, string prefix)
        {
            Dictionary <string, string> dictionary = new Dictionary <string, string>();

            for (int index = 0; index < sourceObject.Count; ++index)
            {
                Dictionary <string, string> objectAsDictionary = SFClass.GetObjectAsDictionary <T>(sourceObject[index], prefix + (object)index + "_");
                if (objectAsDictionary != null && objectAsDictionary.Count > 0)
                {
                    foreach (KeyValuePair <string, string> keyValuePair in objectAsDictionary)
                    {
                        dictionary.Add(keyValuePair.Key, keyValuePair.Value);
                    }
                }
            }
            return(dictionary);
        }
Пример #10
0
        public static T GetPropertyValue <T>(object source, string propertyName)
        {
            if (source == null || string.IsNullOrEmpty(propertyName))
            {
                return(default(T));
            }
            PropertyInfo propertyInfo = SFClass.GetPropertyInfo(source, propertyName);

            if (propertyInfo != (PropertyInfo)null)
            {
                try
                {
                    return((T)propertyInfo.GetValue(source));
                }
                catch
                {
                }
            }
            return(default(T));
        }
Пример #11
0
        public static void SetObjectFromString(ref object sourceObject, char delimiter, string value)
        {
            if (string.IsNullOrEmpty(value) || sourceObject == null)
            {
                return;
            }
            List <string> stringList = new List <string>((IEnumerable <string>)value.Split(delimiter));

            if (stringList.Count <= 0)
            {
                return;
            }
            for (int index = 0; index < stringList.Count; ++index)
            {
                string[] strArray = stringList[index].Split(':');
                if (strArray.Length == 2)
                {
                    SFClass.SetPropertyValue(sourceObject, strArray[0], (object)strArray[1]);
                }
            }
        }
Пример #12
0
        public static string PropertyValuesToString(object source, SFClass.ObjectPropertyAccessType access, string delimiter = ":")
        {
            if (source == null)
            {
                return("");
            }
            Dictionary <string, string> propertyStringValues = SFClass.GetPropertyStringValues(source, access);

            if (propertyStringValues == null || propertyStringValues.Count <= 0)
            {
                return("");
            }
            StringBuilder stringBuilder = new StringBuilder();

            foreach (string key in propertyStringValues.Keys)
            {
                if (stringBuilder.Length > 0)
                {
                    stringBuilder.Append(delimiter);
                }
                stringBuilder.Append(key).Append(";").Append(propertyStringValues[key]);
            }
            return(stringBuilder.ToString());
        }
Пример #13
0
        public static void SetObjectFromDictionary <T>(T sourceObject, Dictionary <string, string> values)
        {
            Dictionary <string, string> dictionary = new Dictionary <string, string>();

            if ((object)sourceObject == null)
            {
                return;
            }
            foreach (PropertyInfo property in sourceObject.GetType().GetProperties())
            {
                try
                {
                    string name = property.Name;
                    string str;
                    try
                    {
                        str = values[name];
                    }
                    catch
                    {
                        str = (string)null;
                    }
                    if (str != null)
                    {
                        if (property.CanWrite)
                        {
                            object obj = property.GetValue((object)sourceObject, (object[])null);
                            if (obj is DateTime)
                            {
                                DateTime date = SFStrings.ToDate(str);
                                property.SetValue((object)sourceObject, (object)date);
                            }
                            else if (obj is bool)
                            {
                                if (str.ToLower() == "true" || str == "1")
                                {
                                    property.SetValue((object)sourceObject, (object)true);
                                }
                                else
                                {
                                    property.SetValue((object)sourceObject, (object)false);
                                }
                            }
                            else if (!(obj is int))
                            {
                                if (!(obj is float))
                                {
                                    if (!(obj is Decimal))
                                    {
                                        Type type = obj.GetType();
                                        property.SetValue((object)sourceObject, SFClass.ValueAs(type, str));
                                    }
                                }
                            }
                        }
                    }
                }
                catch
                {
                }
            }
        }
Пример #14
0
 public static string GetObjectAsString <T>(T sourceObject, string delimiter)
 {
     return(SFClass.GetObjectAsString <T>(sourceObject, delimiter, false));
 }
Пример #15
0
 public static string GetObjectAsString <T>(T sourceObject)
 {
     return(SFClass.GetObjectAsString <T>(sourceObject, ",", false));
 }
Пример #16
0
 public static Dictionary <string, string> GetObjectAsDictionary <T>(T sourceObject)
 {
     return(SFClass.GetObjectAsDictionary <T>(sourceObject, ""));
 }