Reputation: 12386
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, constructors and methods from a file? Taking into account that a file could have more than one class just like a typical cs file.
Upvotes: 3
Views: 1908
Reputation: 525
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...
}
Upvotes: 1
Reputation: 5338
You can use Runtime Compiler (Reflection) in MS.NET
Take a look here
Upvotes: 1
Reputation: 56391
Yes, it's possible to create a class at runtime from a file.
Dynamic Source Code Generation and Compilation
Upvotes: 4