Raúl Roa
Raúl Roa

Reputation: 12386

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, 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

Answers (3)

Jason
Jason

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

Elalfer
Elalfer

Reputation: 5338

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

Take a look here

Upvotes: 1

Randolpho
Randolpho

Reputation: 56391

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

Dynamic Source Code Generation and Compilation

Upvotes: 4

Related Questions