tags:

views:

385

answers:

1

Using T4 code generation, is it possible to access the types defined in the current project?

For example, if I have an interface and I want to delegate its implementation to another class, i.e.

interface IDoSomething {
    public void do_something();
}

class DoSomethingImpl : IDoSomething {
    public void do_something() {
        // implementation...
    }
}

class SomeClass : IDoSomething {
    IDoSomething m_doSomething = new DoSomethingImpl();

    // forward calls to impl object
    public void do_something() {
        m_doSomething.do_something();
    }
}

I would like to automate the call-forwarding in SomeClass with code generation; is this possible?

+5  A: 

I would recommend using CodeModel. Here is an example. Stay away from Reflection, T4 will lock the compiled binaries. If CodeModel doesn't work for what you are trying to do, try Introspection (example).

Oleg Sych
I'm reading the example, but GetEnum doesn't appear to be defined anywhere that I can figure?
Maslow