Example #1
0
        public static bool TryChangeType(this string val, Type changeType, out object result)
        {
            result = (object)null;
            if (Enumerable.Contains <Type>((IEnumerable <Type>)changeType.GetInterfaces(), typeof(IConvertible)))
            {
                return(String2Extensions.TryConvertChangeType(val, changeType, out result));
            }
            if (changeType == typeof(Guid))
            {
                Guid result1 = Guid.Empty;
                if (!Guid.TryParse(val, out result1))
                {
                    return(false);
                }
                result = (object)result1;
                return(true);
            }
            if (!String2Extensions.IsNullableType(changeType))
            {
                return(false);
            }
            Type underlyingType = Nullable.GetUnderlyingType(changeType);

            if (underlyingType != (Type)null)
            {
                return(String2Extensions.TryConvertChangeType(val, underlyingType, out result));
            }
            return(String2Extensions.TryConvertChangeType(val, changeType, out result));
        }
Example #2
0
        public static T ReadInstance <T>(XElement element, CollectionAndValue <PropertyInfo> properties) where T : new()
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }
            T obj = new T();

            if (System.Attribute.IsDefined((MemberInfo)typeof(T), typeof(CollectionDataContractAttribute)))
            {
                obj.GetType().GetMethod("AddRange");
                List <string> list = new List <string>();
                foreach (XElement xelement in element.Elements())
                {
                    list.Add(xelement.Value);
                }
                obj.GetType().GetMethod("AddRange").Invoke((object)obj, new object[1]
                {
                    (object)list.ToArray()
                });
                return(obj);
            }
            foreach (XElement xelement in element.Elements())
            {
                PropertyInfo property = MessageReader.GetProperty <T>(obj, xelement.Name.ToString());
                if (property != (PropertyInfo)null)
                {
                    CollectionAndValue <PropertyInfo> collectionAndValue = new CollectionAndValue <PropertyInfo>(property);
                    properties.Add(collectionAndValue);
                    if (!xelement.IsEmpty && !string.IsNullOrEmpty(xelement.Value))
                    {
                        Type propertyType = property.PropertyType;
                        if (propertyType == typeof(DateTime) || propertyType == typeof(DateTime?))
                        {
                            if (!string.IsNullOrWhiteSpace(xelement.Value))
                            {
                                CultureInfo cultureInfo = new CultureInfo("en-US");
                                string[]    formats     = new string[6]
                                {
                                    "yyyy-MM-ddTHH:mm:ss",
                                    "yyyy-MM-ddTHH:mm:ss.ff",
                                    "ddd MMM dd HH:mm:ss +0000 yyyy",
                                    "yyyy/MM/dd HH:mm:ss",
                                    "yyyy/MM/dd HH:mm:ss.ff",
                                    "ddd, dd MMM yyyy HH:mm:ss GMT"
                                };
                                DateTime dateTime = DateTime.ParseExact(xelement.Value, formats, (IFormatProvider)cultureInfo, DateTimeStyles.None);
                                property.SetValue((object)obj, (object)dateTime, (object[])null);
                            }
                        }
                        else if (property.PropertyType == typeof(string))
                        {
                            property.SetValue((object)obj, (object)xelement.Value, (object[])null);
                        }
                        else
                        {
                            object result = (object)null;
                            if (propertyType.IsValueType)
                            {
                                if (!String2Extensions.TryChangeType(xelement.Value, property.PropertyType, out result))
                                {
                                    throw new Exception();
                                }
                                property.SetValue((object)obj, result, (object[])null);
                            }
                            else
                            {
                                MethodInfo methodInfo = MessageReader._readInstance.MakeGenericMethod(propertyType);
                                property.SetValue((object)obj, methodInfo.Invoke((object)null, new object[2]
                                {
                                    (object)xelement,
                                    (object)collectionAndValue
                                }), (object[])null);
                            }
                        }
                    }
                }
            }
            return(obj);
        }