Пример #1
0
    public static TypeScriptInterface CreateInterface(string assemblyFile, string rootType, bool odata = false)
    {
        var moduleDefinition = ModuleDefinition.ReadModule(assemblyFile);

        if (moduleDefinition == null)
        {
            throw new ArgumentException(string.Format("The assembly '{0}' could not be loaded.", assemblyFile), "assemblyFile");
        }

        var typeDefintion = moduleDefinition.GetType(rootType);

        if (typeDefintion == null)
        {
            throw new ArgumentException(string.Format("The type '{0}' could not be found.", rootType), "rootType");
        }

        var scriptInterface = new TypeScriptInterface();

        scriptInterface.Name = typeDefintion.Name;

        // get all properties
        var propertyDefinitions = typeDefintion
                                  .Traverse(t => t.BaseType == null ? null : t.BaseType.Resolve())
                                  .SelectMany(t => t.Properties);

        foreach (var propertyDefinition in propertyDefinitions)
        {
            var scriptPropery = new TypeScriptProperty();
            scriptPropery.Name = propertyDefinition.Name;

            var propertyType = propertyDefinition.PropertyType;
            scriptPropery.Type       = propertyType.ToScriptType(odata);
            scriptPropery.IsNullable = propertyDefinition.PropertyType.IsNullable();
            scriptPropery.IsArray    = propertyDefinition.PropertyType.IsScriptArray();
            scriptInterface.Properties.Add(scriptPropery);
        }

        return(scriptInterface);
    }
Пример #2
0
        private bool GenerateProperty(TypeScriptProperty tsProperty, TextWriter output) {
            output.Write($"{GenerateCamelCaseName(tsProperty.Name)}");
            
            if (tsProperty.IsOptional) 
                output.Write("?");
            
            output.Write(": ");

            if (tsProperty.Type.IsPrimitive) {
                GeneratePrimitive(tsProperty.Type as TypeScriptPrimitive, output);
            }
            else if (tsProperty.Type.IsArray) {
                GenerateArray(tsProperty.Type as TypeScriptArray, output);
            }
            else { 
                output.Write(tsProperty.Type.Name);
            }
            
            output.WriteLine(';');

            //output.WriteLine($": {tsProperty.Type.Name};");
            return true;
        }