Beispiel #1
0
        public T FromEnova <T, F>(string name)
        {
            var f  = typeof(F);
            var tm = TypeMapAttribute.GetByApiType(f);

            if (tm != null && !string.IsNullOrEmpty(tm.EnovaType))
            {
                f = Type.GetType(tm.EnovaType);
            }
            return(FromEnova <T>(name, f));
        }
Beispiel #2
0
        public API.Types.IObjectBase CreateObject(Type type, object enovaObject = null, object[] args = null)
        {
            var  attr      = type.IsInterface ? TypeMapAttribute.GetByApiType(type) : TypeMapAttribute.GetByConnectorType(type);
            Type enovaType = null;

            API.Types.IObjectBase obj = null;
            if (attr != null)
            {
                enovaType = string.IsNullOrEmpty(attr.EnovaType) ? null : Type.GetType(attr.EnovaType);
                obj       = (API.Types.IObjectBase)attr.ConnectorType.GetConstructor(new Type[0]).Invoke(new object[0]);
            }
            if (enovaType != null && obj != null)
            {
                if (args != null && args.Length > 0)
                {
                    var typeArr = new Type[args.Length];
                    var argsArr = new object[args.Length];
                    for (int i = 0; i < args.Length; i++)
                    {
                        if (args[i] is API.Types.IObjectBase)
                        {
                            typeArr[i] = ((API.Types.IObjectBase)args[i]).EnovaObject.GetType();
                            argsArr[i] = ((API.Types.IObjectBase)args[i]).EnovaObject;
                        }
                        else
                        {
                            typeArr[i] = args[i].GetType();
                            argsArr[i] = args[i];
                        }
                    }
                    obj.EnovaObject = enovaType.GetConstructor(typeArr).Invoke(argsArr);
                    return(obj);
                }
                else
                {
                    if (enovaObject != null)
                    {
                        obj.EnovaObject = enovaObject;
                    }
                    else
                    {
                        obj.EnovaObject = enovaType.GetConstructor(new Type[0]).Invoke(new object[0]);
                    }
                    return(obj);
                }
            }
            else
            {
                throw new Exception("Brak lub źle zarejestrowana mapa dla typu " + type.FullName);
            }
        }
Beispiel #3
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));
        }