protected override T arrayToObject(EzyArray array, EzyUnmarshaller unmarshaller)
        {
            T obj = (T)Activator.CreateInstance(objectType);

            PropertyInfo[] properties = objectType.GetProperties();

            for (int i = 0; i < properties.Length; ++i)
            {
                PropertyInfo targetProperty = properties[i];
                EzyValue     anno           = targetProperty.GetCustomAttribute <EzyValue>();
                int          index          = anno != null ? anno.index : i;

                if (index >= array.size())
                {
                    continue;
                }

                Type   outType  = targetProperty.PropertyType;
                object rawValue = array.getByOutType(index, outType);
                if (rawValue == null)
                {
                    continue;
                }
                object value = unmarshaller.unmarshallByOutType(rawValue, outType);
                if (targetProperty.PropertyType == value.GetType())
                {
                    targetProperty.SetValue(obj, value);
                }
                else
                {
                    MethodInfo parseMethod = targetProperty.PropertyType.GetMethod(
                        "TryParse",
                        BindingFlags.Public | BindingFlags.Static,
                        null,
                        new[] {
                        typeof(string),
                        targetProperty.PropertyType.MakeByRefType()
                    },
                        null
                        );

                    if (parseMethod != null)
                    {
                        object[] parameters = new[] { value, null };
                        bool     success    = (bool)parseMethod.Invoke(null, parameters);
                        if (success)
                        {
                            targetProperty.SetValue(obj, parameters[1]);
                        }
                    }
                }
            }
            return(obj);
        }
Example #2
0
        protected override T mapToObject(EzyObject map, EzyUnmarshaller unmarshaller)
        {
            PropertyInfo[] properties = objectType.GetProperties();

            T obj = (T)Activator.CreateInstance(objectType);

            foreach (PropertyInfo property in properties)
            {
                Type outType = property.PropertyType;

                object   rawValue = null;
                EzyValue anno     = property.GetCustomAttribute <EzyValue>();
                if (anno != null)
                {
                    rawValue = map.getByOutType(anno.name, outType);
                }
                else
                {
                    rawValue = map.getByOutType(property.Name, outType);
                    if (rawValue == null)
                    {
                        string keyString = char.ToLower(property.Name[0]).ToString();
                        if (property.Name.Length > 1)
                        {
                            keyString += property.Name.Substring(1);
                        }
                        rawValue = map.getByOutType(keyString, outType);
                    }
                }
                if (rawValue == null)
                {
                    continue;
                }

                object value = unmarshaller.unmarshallByOutType(rawValue, outType);
                if (outType == value.GetType())
                {
                    property.SetValue(obj, value);
                }
                else
                {
                    MethodInfo parseMethod = property.PropertyType.GetMethod(
                        "TryParse",
                        BindingFlags.Public | BindingFlags.Static,
                        null,
                        new[] {
                        typeof(string),
                        property.PropertyType.MakeByRefType()
                    },
                        null
                        );

                    if (parseMethod != null)
                    {
                        object[] parameters = new[] { value, null };
                        bool     success    = (bool)parseMethod.Invoke(null, parameters);
                        if (success)
                        {
                            property.SetValue(obj, parameters[1]);
                        }
                    }
                }
            }
            return(obj);
        }