在运行时创建C#类 [英] Creating C# Classes at runtime

查看:115
本文介绍了在运行时创建C#类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直好奇,在运行时动态的C#创建类和整个本文栽了跟头。 <一href="http://olondono.blogspot.com/2008/02/creating-$c$c-at-runtime.html">http://olondono.blogspot.com/2008/02/creating-$c$c-at-runtime.html我好奇地听到一些利弊关于建设一类的在运行时。

I have been curious about dynamically create class at runtime in C# and stumbled across this article. http://olondono.blogspot.com/2008/02/creating-code-at-runtime.html I am curious to hear some pros and cons regarding construction of a class at runtime.

任何意见?

推荐答案

元编程具有构建时code-gneneration的所有优点,但没有额外的code步骤。这是库code很常见,如运筹学和管理学,序列化,某些类型的AOP,DI / IoC的容器等。

Meta-programming has all the advantages of build-time code-gneneration, but without the additional code step. This is very common in library code, such as ORMs, serializers, some types of AOP, DI/IoC containers, etc.

  • +避免了需要额外的构建步骤或写世俗code
  • +例如code能够处理的实际在运行时的情况,而不是处理周围的包装任何不常见的边缘情况下包装或手(Decorator模式)
  • +允许codeGEN在场景中的元数据在运行时才知道
  • +运行时IL可以有更多的机会进入私人领域等,这要归功于如何 DynamicMethod的可以与类型相关联;完全生成(DLL)code,需要 [InternalsVisibleTo] 或类似的,这也许是不可能
  • - 并不是所有的系统的支持的运行code根;上禁用某些服务器设置,紧凑的框架,iPhone等
  • - 它的错误丑的事情。不管你怎么做,这是不可以正常code。
  • - 它需要的东西实际上是如何在幕后工作的一个很好的理解
  • +如果迫使你得到的东西实际上是如何在幕后工作的一个很好的理解
  • + avoids the need for extra build steps or writing mundane code
  • + such code can handle what actually is the case at runtime, rather than having to handle any uncommon edge-cases or lots of wrappers around wrappers (decorator pattern)
  • + allows codegen in scenarios where the metadata is only known at runtime
  • + runtime IL can have more access to private fields etc, thanks to how DynamicMethod can be associated with a type; fully generated (dll) code would require [InternalsVisibleTo] or similar, which may be impossible
  • - not all systems support runtime code-gen; it is disabled on some server setups, compact framework, iPhone, etc
  • - it is bug ugly to do. Regardless of how you do it, this is not normal code.
  • - it needs a really good understanding of how things actually work under the covers
  • + if forces you to get a really good understanding of how things actually work under the covers

我目前正在重写现有的库来使用运行时白细胞介素的产生;这是非常有益的,我很喜欢它;但它不像的什么的我之前写的。

I'm currently re-writing an existing library to use runtime IL generation; it is very rewarding and I'm happy with it; but it is unlike anything I've written before.

这篇关于在运行时创建C#类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆