mardi 10 septembre 2019

Passing Type to generic Method

I have a list of interface that I will be doing reflexion code on.
I want to pass those type to the generic Method.

var serviceList = new []{
    typeof(Namespace.Service1.InterfaceFoo),
    typeof(Namespace.Service2.InterfaceBar),
};

foreach(var t in serviceList){
    // CodeFactory_GenerateWrapper(); 
}

CodeFactory_GenerateWrapper<T>(){
    var currType = typeof(T);

    if (!currType.IsInterface) {
        throw new ArgumentException($">> Bad T: {currType.Name}. It's not an interface"); 
    }

    var asyncMethods =
        currType
            .GetMethods()
            .Where(x => x.Name.EndsWith("async", StringComparison.InvariantCultureIgnoreCase))
            .Select(x => x.Name.ToLowerInvariant().Replace("async", ""))
            .ToList();

    // Do other thing
}

I can't have a CodeFactory_GenerateWrapper<T>(T instance) as T is an interface. Using the Web Service "Client" implementing the interface is not a solution has we will have to initialize Namespace.Service1.FooClient() that will fail because the solution does not have a definition for those endpoints.

For now I have a CodeFactory_GenerateWrapper per service interface, with the same code:

CodeFactory_GenerateWrapper_Foo(){
    var currType = typeof(InterfaceFoo);
    // [...]
}

CodeFactory_GenerateWrapper_Bar(){
    var currType = typeof(InterfaceBar);
    // [...]
}





Aucun commentaire:

Enregistrer un commentaire