Пример #1
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);
 }
Пример #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);
            }
        }