Example #1
0
 /// <summary>
 /// Gets the <see cref="TypeMapAttribute"/> if present.
 /// </summary>
 /// <returns>The instance of <see cref="TypeMapAttribute"/>.</returns>
 public TypeMapAttribute GetTypeMapAttribute()
 {
     if (isTypeMapAttributeWasSet)
     {
         return(typeMapAttribute);
     }
     isTypeMapAttributeWasSet = true;
     return(typeMapAttribute = PropertyInfo.GetCustomAttribute(StaticType.TypeMapAttribute) as TypeMapAttribute);
 }
Example #2
0
 /// <summary>
 /// Gets the <see cref="TypeMapAttribute"/> if present.
 /// </summary>
 /// <returns>The instance of <see cref="TypeMapAttribute"/>.</returns>
 public TypeMapAttribute GetTypeMapAttribute()
 {
     if (m_isTypeMapAttributeWasSet)
     {
         return(m_typeMapAttribute);
     }
     m_isTypeMapAttributeWasSet = true;
     return(m_typeMapAttribute = PropertyInfo.GetCustomAttribute(typeof(TypeMapAttribute)) as TypeMapAttribute);
 }
Example #3
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));
        }
Example #4
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);
            }
        }
Example #5
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);
        }
Example #6
0
 public static object ToEnova(object obj)
 {
     if (obj != null)
     {
         if (obj.GetType().IsEnum)
         {
             var map = TypeMapAttribute.GetByConnectorType(obj.GetType());
             if (map != null && !string.IsNullOrEmpty(map.EnovaType))
             {
                 return(Enum.ToObject(Type.GetType(map.EnovaType), (int)obj));
             }
         }
         else if (obj is API.Types.ObjectBase && ((API.Types.ObjectBase)obj) != null)
         {
             return(((API.Types.ObjectBase)obj).EnovaObject);
         }
     }
     return(null);
 }
Example #7
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));
        }