Csharp/C#教程:T4代码生成:当前项目中的访问类型分享


T4代码生成:当前项目中的访问类型

使用T4代码生成,是否可以访问当前项目中定义的类型?

例如,如果我有一个接口,我想将其实现委托给另一个类,即

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的呼叫转发; 这可能吗?

虽然这不能解决锁定问题(虽然我听说过VS2010会这样做),但您可以尝试将dll复制到临时位置并使用复制的程序集。

上述就是C#学习教程:T4代码生成:当前项目中的访问类型分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 <#@ template debug="false" hostspecific="false" language="C#" #> <#@ output extension=".txt" #> <#@ import namespace="System.Reflection" #> <#@ import namespace="System.IO" #> <# var newFileName = System.IO.Path.GetTempFileName(); System.IO.File.Copy(@"C:DevelopmentCustomAssembly.dll",newFileName,true); var assembly = Assembly.LoadFrom(newFileName); var type = assembly.GetType("CustomAssembly.DummyClass"); #> <#=newFileName#> <#=type#> 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1008685.html

(0)
上一篇 2021年12月29日
下一篇 2021年12月29日

精彩推荐