Ejemplo n.º 1
0
        /// <summary>
        /// 构造一个指定泛型参数的JS完整类型名
        /// </summary>
        public string MakeGenericJSFullTypeName(string templateArgs)
        {
            var name = CodeGenUtils.Concat(".", this.jsNamespace, this.jsPureName);

            // var name = string.IsNullOrEmpty(this.jsNamespace) ? this.jsPureName : this.jsNamespace + "." + this.jsPureName;
            return(string.Format("{0}<{1}>, ", name, templateArgs));
        }
        public void AddModuleEntry(string moduleName, string runtimeVarName, string moduleVarName, TypeBindingInfo typeBindingInfo)
        {
            var csType        = this.cg.bindingManager.GetCSTypeFullName(typeBindingInfo.type);
            var csNamespace   = this.cg.bindingManager.prefs.ns;
            var csBindingName = typeBindingInfo.csBindingName;
            var jsNamespace   = CodeGenUtils.Concat(", ", CodeGenUtils.ConcatAsLiteral(", ", typeBindingInfo.tsTypeNaming.jsNamespaceSlice), $"\"{typeBindingInfo.tsTypeNaming.jsNameNormalized}\"");

            AddStatement($"{runtimeVarName}.AddTypeReference({moduleVarName}, typeof({csType}), {csNamespace}.{csBindingName}.Bind, {jsNamespace});");
        }
Ejemplo n.º 3
0
        public TSTypeNaming(BindingManager bindingManager, Type type, TypeTransform typeTransform)
        {
            this.type = type;

            var naming          = typeTransform?.GetTypeNaming() ?? type.Name;
            var indexOfTypeName = naming.LastIndexOf('.');

            if (indexOfTypeName >= 0)
            {
                // 指定的命名中已经携带了"."
                var indexOfInnerTypeName = naming.IndexOf('+');
                if (indexOfInnerTypeName >= 0)
                {
                    this.jsModule = naming.Substring(0, indexOfInnerTypeName);
                    var rightName = naming.Substring(indexOfInnerTypeName + 1);
                    var lastIndexOfInnerTypeName = rightName.LastIndexOf('+');
                    if (lastIndexOfInnerTypeName >= 0)
                    {
                        this.jsNamespace = rightName.Substring(0, lastIndexOfInnerTypeName);
                        this.jsName      = rightName.Substring(lastIndexOfInnerTypeName + 1);
                    }
                    else
                    {
                        this.jsNamespace = "";
                        this.jsName      = rightName;
                    }
                }
                else
                {
                    this.jsModule    = naming.Substring(0, indexOfTypeName);
                    this.jsNamespace = "";
                    this.jsName      = naming.Substring(indexOfTypeName + 1);
                }

                var gArgIndex = this.jsName.IndexOf("<");
                if (gArgIndex < 0)
                {
                    this.jsPureName = this.jsName;
                }
                else
                {
                    this.jsPureName = this.jsName.Substring(0, gArgIndex);
                }
            }
            else
            {
                this.jsModule    = type.Namespace ?? "";
                this.jsNamespace = "";

                // 处理内部类层级
                var declaringType = type.DeclaringType;
                while (declaringType != null)
                {
                    this.jsNamespace = string.IsNullOrEmpty(this.jsNamespace) ? declaringType.Name : $"{declaringType.Name}.{this.jsNamespace}";
                    declaringType    = declaringType.DeclaringType;
                }

                if (type.IsGenericType)
                {
                    this.jsName     = naming.Contains("`") ? naming.Substring(0, naming.IndexOf('`')) : naming;
                    this.jsPureName = this.jsName;

                    if (type.IsGenericTypeDefinition)
                    {
                        if (!naming.Contains("<"))
                        {
                            this.jsName += "<";
                            var gArgs = type.GetGenericArguments();

                            for (var i = 0; i < gArgs.Length; i++)
                            {
                                this.jsName += gArgs[i].Name;
                                if (i != gArgs.Length - 1)
                                {
                                    this.jsName += ", ";
                                }
                            }
                            this.jsName += ">";
                        }
                    }
                    else
                    {
                        foreach (var gp in type.GetGenericArguments())
                        {
                            this.jsName += "_" + gp.Name;
                        }
                    }
                }
                else
                {
                    this.jsName = naming;

                    //TODO: 整理 jsPureName 的取值流程 (对于泛型中的嵌套的处理等)
                    var gArgIndex = this.jsName.IndexOf("<");
                    if (gArgIndex < 0)
                    {
                        this.jsPureName = this.jsName;
                    }
                    else
                    {
                        this.jsPureName = this.jsName.Substring(0, gArgIndex);
                    }
                }
            }

            if (string.IsNullOrEmpty(this.jsNamespace))
            {
                this.jsModuleAccess       = this.jsName;
                this.jsModuleImportAccess = this.jsPureName;
                this.jsLocalName          = "";
            }
            else
            {
                var i = this.jsNamespace.IndexOf('.');
                this.jsModuleAccess       = i < 0 ? this.jsNamespace : this.jsNamespace.Substring(0, i);
                this.jsModuleImportAccess = this.jsModuleAccess;
                this.jsLocalName          = CodeGenUtils.Concat(".", i < 0 ? "" : this.jsNamespace.Substring(i + 1), this.jsName);
            }

            if (this.jsModuleAccess.EndsWith("[]"))
            {
                this.jsModuleAccess = this.jsModuleAccess.Substring(0, this.jsModuleAccess.Length - 2);
            }

            this.jsDepth    = this.jsModuleAccess.Split('.').Length;
            this.jsFullName = CodeGenUtils.Concat(".", jsModule, jsNamespace, jsName);
        }
Ejemplo n.º 4
0
        public string GetTSTypeFullName(Type type, bool isOut)
        {
            if (type == null || type == typeof(void))
            {
                return("void");
            }

            if (type.IsByRef)
            {
                if (isOut)
                {
                    return($"{this.cg.bindingManager.GetDefaultTypePrefix()}Out<{GetTSTypeFullName(type.GetElementType())}>");
                }
                return($"{this.cg.bindingManager.GetDefaultTypePrefix()}Ref<{GetTSTypeFullName(type.GetElementType())}>");
                // return GetTSTypeFullName(type.GetElementType());
            }

            List <string> names;

            if (this.cg.bindingManager.GetTSTypeNameMap(type, out names))
            {
                return(names.Count > 1 ? $"({String.Join(" | ", names)})" : names[0]);
            }

            if (type == typeof(Array))
            {
                return("Array<any>");
            }

            if (type == typeof(ScriptPromise))
            {
                return("Promise<void>");
            }

            if (type.IsSubclassOf(typeof(ScriptPromise)))
            {
                if (type.IsGenericType)
                {
                    var gt = type.GetGenericArguments()[0];
                    return("Promise<" + GetTSTypeFullName(gt) + ">");
                }
                return("Promise<any>");
            }

            if (type.IsArray)
            {
                var elementType = type.GetElementType();
                var tsFullName  = GetTSTypeFullName(elementType);
                var rank        = type.GetArrayRank();

                if (rank == 1)
                {
                    return("Array<" + tsFullName + ">");
                }
                return("Array<" + tsFullName + ", " + rank + ">");
            }

            var info = this.cg.bindingManager.GetExportedType(type);

            if (info != null)
            {
                var gDef = GetTSGenericTypeDefinition(type);
                if (!string.IsNullOrEmpty(gDef))
                {
                    return(gDef);
                }

                var tsTypeNaming = info.tsTypeNaming;
                if (tsTypeNaming.jsModule == this.tsModule)
                {
                    return(CodeGenUtils.Concat(".", tsTypeNaming.jsModuleAccess, tsTypeNaming.jsLocalName));
                }

                var localAlias = GetAlias(type);
                if (localAlias != null)
                {
                    return(CodeGenUtils.Concat(".", localAlias, tsTypeNaming.jsLocalName));
                }
                return(tsTypeNaming.jsFullName);
            }

            if (type.BaseType == typeof(MulticastDelegate))
            {
                var delegateBindingInfo = this.cg.bindingManager.GetDelegateBindingInfo(type);
                if (delegateBindingInfo != null)
                {
                    // var nargs = delegateBindingInfo.parameters.Length;
                    var ret = GetTSTypeFullName(delegateBindingInfo.returnType);
                    // var t_arglist = (nargs > 0 ? ", " : "") + GetTSArglistTypes(delegateBindingInfo.parameters, false);
                    var v_arglist = GetTSArglistTypes(delegateBindingInfo.parameters, true);
                    // return $"{CodeGenerator.NamespaceOfInternalScriptTypes}.Delegate{nargs}<{ret}{t_arglist}> | (({v_arglist}) => {ret})";
                    return($"({v_arglist}) => {ret}");
                }
            }

            if (type.IsGenericType)
            {
                if (type.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    var gArgs   = type.GetGenericArguments();
                    var gArgsTS = GetTSTypeFullName(gArgs[0]);
                    return($"{this.cg.bindingManager.GetDefaultTypePrefix()}Nullable<{gArgsTS}>");
                }
            }
            else
            {
                if (type.IsGenericParameter)
                {
                    return(type.Name);
                }
            }

            return("any");
        }