简体   繁体   中英

import classes when class declaration in another file - typewriter

I'm using typerwriter extension to create ts file corresponding to c# file, but there is some variables from type that declared in another file so I have to add import statement.

to do so, I want to get all the classes name that in this file and add import only if class name isnt in this list.

in this example BDTO in another file.

public class ADTO
{
    public uint id { get; set; }
    public uint editionId { get; set; }
    public BDTO mask { get; set; }
}

tst code:

 string Imports(Class c, File f)
{
    IEnumerable<String> classes = f.Classes.Select(p=> p.name);

    IEnumerable<Type> types = c.Properties
        .Select(p => p.Type)
        .Where(t =>((!t.IsPrimitive  || t.IsEnum) && !classes.Contains(t.name)))
        .Select(t => t.IsGeneric ? t.TypeArguments.First() : t)
        .Where(t => t.Name != c.Name && t.Name != "DbGeography")
        .Distinct();
    return string.Join(Environment.NewLine, types.Select(t => $"import {{ {t.Name} }} from './misc';").Distinct());
}

$Classes(*DTO)[
$Imports

export interface $Name {
    $Properties[
    // $LoudName
    $name: $Type;]
}]

this adding: $Imports instead of: import {BDTO} from './misc'

I had the same issue. I think it is because the method can only take the one parameter, of type Class .

This example shows another way of solving the problem: https://github.com/frhagn/Typewriter/issues/270

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM