Reflection.Emit vs CodeDOM

前端 未结 3 1892
孤独总比滥情好
孤独总比滥情好 2020-11-30 23:48

What are some pros/cons for using the Reflection.Emit library versus CodeDOM for dynamically generating code at runtime?

I am trying to generate som

3条回答
  •  無奈伤痛
    2020-12-01 00:37

    Code that targets CodeDom tends to be easier to maintain, since you're generating C# code and not IL (more people can read C# than IL). Futhermore, if you get your CodeDom code wrong, you get a compiler error; if you generate invalid IL, you get a fatal exception or a crash.

    However, because CodeDom invokes the csc.exe compiler, it's a little slower to get the code ready for use. With Reflection.Emit, you can generate code directly into memory.

    CodeDom is probably fine for most things; the XmlSerializer and the WinForms designer use it.

提交回复
热议问题