开发者

How to create class at runtime from file

Is it possible to create a class at runtime from a file without parsing it?

If not what would be the best approach to retrieve class properties, construc开发者_开发百科tors and methods from a file? Taking into account that a file could have more than one class just like a typical cs file.


Yes, it's possible to create a class at runtime from a file.

Dynamic Source Code Generation and Compilation


You can use Runtime Compiler (Reflection) in MS.NET

Take a look here


If you are using ASP.NET take a look at BuildManager.GetCompiledAssembly.

Here's an example usage:

Assembly a = BuildManager.GetCompiledAssembly("~/TestClass.cs");
foreach (Type t in a.GetExportedTypes()) {
    object obj = Activator.CreateInstance(t)
    // Do something with obj...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜