mercredi 13 juillet 2016

Dynamically create a class by interface

I have some expirience with .Net Expressions, when I'm able to dynamically generate methods. It's fine, it's good.

But now I need to generate a whole class, and it seems that the only way to do it is Emit whole IL which is totally inacceptable (it's impossible to support).

Assume we have following interface:

public interface IFoo
{
    [Description("5")]
    int Bar();
    [Description("true")]
    bool Baz();
}

which should be converted to:

public class Foo : IFoo
{
    public int Bar() => 5;
    public bool Baz() => true;
}

How can I achieve it? Is it even possible without 3rd party tools and libs? I know there is plenty of useful utils on GitHub, but I really don't want to import a whole MVVM framework to just make some code generation.

If I could just use Expressions, and create a class with methods I already generated with it. But for now I don't know how to do it.





Aucun commentaire:

Enregistrer un commentaire