Ejemplo n.º 1
0
 protected void OutputInterface(TypeScriptWriter writer, ApiInterfaceDesc i)
 {
     writer.Write($"interface {getName(i.TsName)}");
     if (i.Extends.Any())
     {
         writer.Write(" extends ");
         writer.Write(i.Extends.Select(e => getNamespace(e.TsName) == getNamespace(i.TsName) ? getName(e.TsName) : e.TsName).Order().JoinString(", "));
     }
     writer.WriteLine(" {");
     using (writer.Indent())
     {
         foreach (var prop in i.Properties.OrderBy(p => p.TsName))
         {
             writer.WriteLine($"{prop.TsName}: {prop.TsType.GetTypeScript()};");
         }
     }
     writer.WriteLine("}");
 }
Ejemplo n.º 2
0
        /// <summary>
        ///     Adds an interface for the specified type to the set of API declarations. May be called multiple times for the
        ///     same type with no ill effects. Recursively adds all additional interfaces required by the one being added.
        ///     Returns true if an interface will exist in TypeScript, or false when it's not possible or desirable to expose
        ///     this type in TypeScript. This logic is based entirely on whether the type is contained in one of the
        ///     whitelisted assemblies (see <see cref="ApiDesc.Assemblies"/> and <see cref="AddAssembly(Assembly)"/>), but can
        ///     be fully customized by overriding this method.</summary>
        protected virtual bool AddInterface(Type type)
        {
            if (Api.Interfaces.ContainsKey(type))
            {
                return(true);
            }
            if (!ShouldAddType(type))
            {
                return(false);
            }
            var iface = new ApiInterfaceDesc();

            Api.Interfaces[type] = iface;
            iface.Type           = type;
            iface.TsName         = type.FullName;
            if (type.BaseType != null && AddInterface(type.BaseType))
            {
                iface.Extends.Add(Api.Interfaces[type.BaseType]);
            }
            foreach (var prop in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                var ptype = MapType(prop.PropertyType);
                if (ptype == null)
                {
                    continue;
                }
                iface.Properties.Add(new ApiPropertyDesc {
                    Member = prop, TsType = ptype, TsName = prop.Name
                });
            }
            foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.Instance))
            {
                var ftype = MapType(field.FieldType);
                if (ftype == null)
                {
                    continue;
                }
                iface.Properties.Add(new ApiPropertyDesc {
                    Member = field, TsType = ftype, TsName = field.Name
                });
            }
            return(true);
        }