예제 #1
0
        private static Type getMappedConnectorBaseType(object obj)
        {
            var t = obj.GetType().BaseType;
            TypeMapAttribute tm = null;

            while (t != typeof(object))
            {
                tm = TypeMapAttribute.GetByEnova(t.FullName);
                if (tm != null)
                {
                    break;
                }
                t = t.BaseType;
            }
            return(tm == null ? typeof(API.Types.ObjectBase) : tm.ConnectorType);
        }
예제 #2
0
        public static T FromEnova <T>(object obj)
        {
            if (obj != null)
            {
                if (obj is API.Types.IObjectBase)
                {
                    obj = ((API.Types.IObjectBase)obj).EnovaObject;
                }
                if (typeof(T).IsAssignableFrom(obj.GetType()))
                {
                    return((T)obj);
                }
                if (obj.GetType().IsEnum&& typeof(T).IsEnum)
                {
                    return((T)Enum.ToObject(typeof(T), (int)obj));
                }
                var con = TypeDescriptor.GetConverter(obj.GetType());
                if (con != null && con.CanConvertTo(typeof(T)))
                {
                    return((T)con.ConvertTo(obj, typeof(T)));
                }
                else
                {
                    con = TypeDescriptor.GetConverter(typeof(T));
                    if (con != null && con.CanConvertFrom(obj.GetType()))
                    {
                        return((T)con.ConvertFrom(obj));
                    }
                }
                var tm = TypeMapAttribute.GetByEnova(obj);
                if (tm == null)
                {
                    tm = TypeMapAttribute.GetByApiType(typeof(T));
                }
                if (tm == null && EnovaService.ConnectorSide)
                {
                    var t = obj.GetType().BaseType;
                    while (t != typeof(object))
                    {
                        tm = TypeMapAttribute.GetByEnova(t.FullName);
                        if (tm != null)
                        {
                            break;
                        }
                        t = t.BaseType;
                    }
                }

                Type connectorType = tm.ConnectorType;
                if (connectorType == null)
                {
                    connectorType = getConnectorType(tm.ApiType, obj);
                }

                if (tm != null && connectorType != null)
                {
                    var r = connectorType.GetConstructor(new Type[0]).Invoke(new object[0]);
                    if (r is API.Types.ObjectBase)
                    {
                        ((API.Types.ObjectBase)r).EnovaObject = obj;
                    }
                    return((T)r);
                }

                throw new Exception("Brak zarejestrowanej mapy dla " + obj.GetType().FullName);
            }
            return(default(T));
        }