3

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.

Simeon Pilgrim
  • 22,906
  • 3
  • 32
  • 45
Raúl Roa
  • 12,061
  • 13
  • 49
  • 64
  • What does this question have to-do with regex? Also duplicate of this question: http://stackoverflow.com/questions/826398/is-it-possible-to-dynamically-compile-and-execute-c-code-fragments – Simeon Pilgrim Nov 19 '09 at 22:53

3 Answers3

4

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

Dynamic Source Code Generation and Compilation

Randolpho
  • 55,384
  • 17
  • 145
  • 179
1

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

Take a look here

Elalfer
  • 5,312
  • 20
  • 25
1

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...
}
Jason
  • 525
  • 3
  • 10