/*
     * // Runtime Only
     * // type: class type
     * // methodName: method name
     * // TCount: generic parameter count
     * // vc: JSVCall instance
     * public static MethodInfo MakeGenericConstructor(Type type, int TCount, int paramCount, JSVCall vc)
     * {
     *  // Get generic method by name and param count.
     *  ConstructorInfo conT = JSDataExchangeMgr.GetGenericConstructorInfo(type, TCount, paramCount);
     *  if (conT == null)
     *  {
     *      return null;
     *  }
     *
     *  // get T types
     *  Type[] genericTypes = new Type[TCount];
     *  for (int i = 0; i < TCount; i++)
     *  {
     *      // Get generic types from js.
     *      System.Type t = JSDataExchangeMgr.GetTypeByName(JSMgr.datax.getString(JSDataExchangeMgr.eGetType.GetARGV));
     *      genericTypes[i] = t;
     *      if (t == null)
     *      {
     *          return null;
     *      }
     *  }
     *
     *  // Make generic method.
     *  MethodInfo method = methodT.MakeGenericMethod(genericTypes);
     *  return method;
     * }
     * // Runtime Only
     * // called by MakeGenericConstructor
     * // get generic Constructor by matching TCount,paramCount, if more than 1 match, return null.
     * static ConstructorInfo GetGenericConstructorInfo(Type type, int TCount, int paramCount)
     * {
     *  ConstructorInfo[] constructors = type.GetConstructors();
     *  if (constructors == null || constructors.Length == 0)
     *  {
     *      return null;
     *  }
     *
     *  ConstructorInfo con = null;
     *  for (int i = 0; i < constructors.Length; i++)
     *  {
     *      if (constructors[i].IsGenericMethodDefinition &&
     *          constructors[i].GetGenericArguments().Length == TCount &&
     *          constructors[i].GetParameters().Length == paramCount)
     *      {
     *          if (con == null)
     *              con = constructors[i];
     *          else
     *          {
     *              Debug.LogError("More than 1 Generic Constructor found!!! " + GetTypeFullName(type) + "." + name);
     *              return null;
     *          }
     *      }
     *  }
     *  if (con == null)
     *  {
     *      Debug.LogError("No generic constructor found! " + GetTypeFullName(type));
     *  }
     *  return con;
     * }*/
    public static ConstructorInfo makeGenericConstructor(Type type, ConstructorID constructorID)
    {
        int tCount = type.GetGenericArguments().Length;

        Type[] genericTypes = new Type[tCount];
        for (int i = 0; i < tCount; i++)
        {
            string      typeName = JSApi.getStringS((int)JSApi.GetType.Arg);
            System.Type t        = JSDataExchangeMgr.GetTypeByName(typeName, typeof(CSRepresentedObject));
            genericTypes[i] = t;
            if (t == null)
            {
                return(null);
            }
        }

        var exactType        = type.MakeGenericType(genericTypes);
        var exactConstructor = GenericTypeCache.getConstructor(exactType, constructorID);

        return(exactConstructor);
    }
Esempio n. 2
0
    public static ConstructorInfo getConstructor(Type type, ConstructorID id)
    {
        TypeMembers tmember = getMembers(type);
        var         arr     = tmember.cons;

        if (arr != null)
        {
            for (var i = -1; i < arr.Length; i++)
            {
                ConstructorInfo curr;
                if (i == -1)
                {
                    if (id.index >= 0 && arr.Length > id.index)
                    {
                        curr = arr[id.index];
                    }
                    else
                    {
                        continue;
                    }
                }
                else
                {
                    curr = arr[i];
                }
                if (matchParameters(curr.GetParameters(), id.paramTypeNames, id.paramFlags))
                {
                    if (i != -1)
                    {
                        id.index = i;
                    }
                    return(curr);
                }
            }
        }
        Debug.LogError(new StringBuilder().AppendFormat("GenericTypeCache.getConstructor({0}) fail", type.Name));
        return(null);
    }
 public static ConstructorInfo getConstructor(Type type, ConstructorID id)
 {
     TypeMembers tmember = getMembers(type);
     var arr = tmember.cons;
     if (arr != null)
     {
         for (var i = -1; i < arr.Length; i++)
         {
             ConstructorInfo curr;
             if (i == -1)
             {
                 if (id.index >= 0 && arr.Length > id.index)
                     curr = arr[id.index];
                 else
                     continue;
             }
             else
             {
                 curr = arr[i];
             }
             if (matchParameters(curr.GetParameters(), id.paramTypeNames, id.paramFlags))
             {
                 if (i != -1)
                     id.index = i;
                 return curr;
             }
         }
     }
     Debug.LogError(new StringBuilder().AppendFormat("GenericTypeCache.getConstructor({0}) fail", type.Name));
     return null;
 }