C#中的动态类继承 [英] Dynamic Class Inheritance in C#

查看:84
本文介绍了C#中的动态类继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我用谷歌搜索,但找不到一个好的答案,这是我的问题;

Hi I googled but couldn''t find a good answer, heres my question;
Is it possible to derive classses in runtime?

推荐答案

有一种方法:您可以在运行时使用System.Reflection.Emit创建一个程序集,然后在该程序集中,请创建具有必需功能的类型(它可以是从某些现有类型派生的类),然后在同一运行时中使用System.Reflection来使用它.请参阅:
http://msdn.microsoft.com/en-us/library/system.reflection. embed.aspx [^ ],
http://msdn.microsoft.com/en-us/library/3y322t50.aspx [ ^ ].

这是一篇有趣的简单介绍性文章: http://www.drdobbs.com/生成代码运行时带有反射/184416570 [使用Reflection.Emit创建动态类型的简介 [ ^ ],
使用反射创建动态类型的简介.Emit:第2部分 [ ^ ].

优秀的CodeProject文章:
使用Reflection.Emit的动态类型 [ http://www.codeproject.com/search.aspx?doctypeid=1&q= Reflection.emit [ ^ ].

我必须补充一点,使用System.Reflection.Emit创建代码是在某些情况下要使用的非常重要的功能.例如,它用于序列化,以使从元数据生成的代码获得良好的性能.此CodeProject文章显示了它的另一个重要应用:
使用Reflection.Emit将表达式预编译为MSIL [ ^ ].

如果需要MSIL,则需要很好的知识.这项工作非常乏味,并且代码真的很难调试. C#中正在调试的代码仅向您显示代码生成的过程,而没有显示生成的代码.因此,只有在想要获得一些奇迹般的,非常根本的普遍结果时,才应使用它.这不是您日常编程的工作.

—SA
There is one way: you can create an assembly during run time, using System.Reflection.Emit, and, in this assembly, create a type with required features (it could be a class derived from some existing type) and later use it using System.Reflection — in in the same runtime. Please see:
http://msdn.microsoft.com/en-us/library/system.reflection.emit.aspx[^],
http://msdn.microsoft.com/en-us/library/3y322t50.aspx[^].

This is an interesting simple introductory article: http://www.drdobbs.com/generating-code-at-run-time-with-reflect/184416570[^].

This is one CodeProject article, more in depth:
Introduction to Creating Dynamic Types with Reflection.Emit[^],
Introduction to Creating Dynamic Types with Reflection.Emit: Part 2[^].

A good CodeProject article:
Dynamic Type Using Reflection.Emit[^].

I generally recommend to look at CodeProject articles on the topic; some are very interesting:
http://www.codeproject.com/search.aspx?doctypeid=1&q=Reflection.emit[^].

I must add that creating code using System.Reflection.Emit is a very important feature to be used in some scenarios. For example, it is used in serialization, for getting good performance of the code generated from metadata. This CodeProject article shows another important application of it:
Using Reflection.Emit to Precompile Expressions to MSIL[^].

It requires good knowledge if MSIL. The work is tedious enough and the code is really hard to debug. The code in C# being debugged only shows you the process of code generation, but not generated code. So, you should only use it when you want to get some miraculous and very fundamental universal results. This is not a work for your everyday programming.

—SA


这篇关于C#中的动态类继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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