简体   繁体   中英

How to generate a C# class file from C# code behind?

Is it possible to create a C# class ( classname.cs ) file at runtime and then use the class at runtime.

If it is, then how can I implement this?

Have a look at the CSharpCodeProvider class. There's a complete example on the MSDN page

Check out the classes in the namespace Microsoft.CSharp . The CSharpCodeProvider has a method CompileAssemblyFromSource which can generate an in-memory assembly from your source.

Please look Generate C# Class Library project programatically with given fields . Not sure what means by runtime. But you can create many classes just clicking save (run t4 template).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM