示例#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 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
            {
            }
        }
示例#4
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
                {
                }
            }
        }