Exemplo n.º 1
0
        TypeScriptType MakeHubInterface(Type hubType)
        {
            var name  = "I" + hubType.Name;
            var cName = name + "Client";

            var serverDeclaration = "interface " + name + " {\n";
            var clientDeclaration = "interface " + cName + " {\n";
            var count             = 0;
            var sep          = "";
            var hubAttribute = "";

            hubType.GetMethods()
            .Where(mi => mi.DeclaringType.Name == hubType.Name).ToList()
            .ForEach(mi =>
            {
                serverDeclaration += "    " + CamelCase(mi.Name) + "(";
                clientDeclaration += "    " + CamelCase(mi.Name) + "(";


                var retTS         = _dataTypes.GetTypeScriptType(mi.ReturnType);
                var retServerType = retTS.Name == "System.Void" ? "void" : "IPromise<" + retTS.Name + ">";
                var retClientType = retTS.Name == "System.Void" ? "void" : "" + retTS.Name + "";
                mi.GetParameters().ToList()
                .ForEach((pi) =>
                {
                    sep = (count != 0 ? ", " : "");
                    count++;
                    var tst            = _dataTypes.GetTypeScriptType(pi.ParameterType);
                    serverDeclaration += sep + pi.Name + ": " + tst.Name;
                    clientDeclaration += sep + pi.Name + ": " + tst.Name;
                });

                serverDeclaration += "): " + retServerType + ";\n";
                clientDeclaration += "): " + retClientType + ";\n";
                count              = 0;
            });

            serverDeclaration += "}";
            clientDeclaration += "}";


            hubAttribute = hubType.CustomAttributes
                           .Where(ad => ad.AttributeType.Name == "HubNameAttribute")
                           .Select(ad => ad.ConstructorArguments.FirstOrDefault().Value.ToString())
                           .FirstOrDefault();

            var ret = new TypeScriptType
            {
                Name        = name,
                Declaration = serverDeclaration
            };

            _hubCache.Add(name, ret);

            _hubAliasesCache.Add(name, hubAttribute);

            _clientCache.Add(cName, new TypeScriptType
            {
                Name        = cName,
                Declaration = clientDeclaration
            });

            return(ret);
        }
Exemplo n.º 2
0
        public TypeScriptType GetTypeScriptType(Type type)
        {
            TypeScriptType value;
            var            typeName = type.FullName;

            if (cache.TryGetValue(typeName, out value))
            {
                return(value);
            }


            // Nullables..
            if (type.FullName.Contains("Nullable"))
            {
                value = MakeFromNullable(type);
            }
            // Tasks..
            else if (type.FullName.Contains("Task") || type.GetInterfaces().Any(X => X.FullName.Contains("Task")))
            {
                value = MakeFromTask(type);
            }
            // Dictionaries -- these should come before IEnumerables, because they also implement IEnumerable
            else if (type.GetInterfaces().Any(X => X.FullName.Contains("IDictionary")))
            {
                value = MakeDictionary(type);
            }
            // Arrays
            else if (typeName.Contains("[]"))
            {
                value = MakeArray(typeof(List <Object>));
            }
            else if (type.GetInterfaces().Any(X => X.FullName.Contains("IList") || X.FullName.Contains("IEnumerable")))
            {
                value = MakeArray(type);
            }
            else if (type.GetInterfaces().Any(X => X.FullName.Contains("Tuple")))
            {
                value = MakeTuple(type);
            }
            else if (type.IsEnum)
            {
                value = MakeEnum(type);
            }
            else if (type.IsClass || type.IsInterface)
            {
                value = MakeClassOrInterface(type);
            }
            else if (type.FullName == "System.Void")
            {
                value = new TypeScriptType
                {
                    Name        = "void",
                    Declaration = ""
                };
            }
            else
            {
                Console.WriteLine("Warning:" + type);
                value = new TypeScriptType
                {
                    Name        = "UNKNOWN TYPE " + type,
                    Declaration = ""
                };
            }


            cache.Add(type.FullName, value);

            return(value);
        }