protected override EzyArray objectToArray(T obj, EzyMarshaller marshaller)
        {
            int count = 0;
            SortedDictionary <int, object> valueByIndex = new SortedDictionary <int, object>();

            foreach (PropertyInfo property in objectType.GetProperties())
            {
                EzyValue anno     = property.GetCustomAttribute <EzyValue>();
                int      index    = anno != null ? anno.index : count;
                object   rawValue = property.GetValue(obj);
                object   value    = rawValue != null?marshaller.marshall <object>(rawValue) : null;

                valueByIndex[index] = value;
                ++count;
            }
            EzyArray array = EzyEntityFactory.newArray();

            for (int i = 0; i < count; ++i)
            {
                object value = null;
                if (valueByIndex.ContainsKey(i))
                {
                    value = valueByIndex[i];
                }
                array.add(value);
            }
            return(array);
        }
Beispiel #2
0
        protected override EzyObject objectToMap(T obj, EzyMarshaller marshaller)
        {
            EzyObject map = EzyEntityFactory.newObject();

            foreach (PropertyInfo property in objectType.GetProperties())
            {
                string   key  = null;
                EzyValue anno = property.GetCustomAttribute <EzyValue>();
                if (anno != null)
                {
                    key = anno.name;
                }
                else
                {
                    key = property.Name.Length <= 1
                        ? char.ToLower(property.Name[0]).ToString()
                        : char.ToLower(property.Name[0]) + property.Name.Substring(1);
                }
                object rawValue = property.GetValue(obj);
                object value    = rawValue != null?marshaller.marshall <object>(rawValue) : null;

                map.put(key, value);
            }
            return(map);
        }
        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);
        }
Beispiel #4
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);
        }