T4 code世代:在当前项目中访问类型世代、类型、项目、code

2023-09-03 03:14:48 作者:别勾引我ゝ我不近男色|▎

使用T4 code一代,是有可能访问在当前项目中定义的类型?

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();
    }
}

我想自动化呼叫转移的 SomeClass的与code产生;这可能吗?

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

推荐答案

我会建议使用codeModel。下面是一个example.远离反射,T4将锁定编译的二进制文件。如果codeModel不适合工作,你正在做什么,试着反省(example).

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).