开发者

How to create class at runtime from file

开发者 https://www.devze.com 2022-12-12 13:25 出处:网络
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? T

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

精彩评论

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