Question

I have a current assembly in my application and I would like to add a class from external cs file into this assembly. Is it possible to do it? I would like to use it like plug-ins. Now I'm trying use:

System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();

and Activator,but without success. My application is using Roslyn, so maybe it can do it. Any idea will be appreciated.

Edit: Next problem with it is: Can I use external file (cs file with class) to get instance from this file but the constructor of class needs reference to sceneManager from current assembley. So is possible to send a reference to Roslyn of something like that and get instance of class from it?

Was it helpful?

Solution

You cannot modify an existing assembly that has already been loaded.

Instead, you can compile code to a new assembly (using Roslyn, CodeDOM, Sigil, or similar libraries) and load that assembly using reflection.

OTHER TIPS

A '.cs' file by itself is just text. You can't do anything with it without compiling it through some route. But no: you can't add extra classes into an assembly at runtime. You can compile the code at runtime via CSharpCodeProvider or similar, and load the generated assembly. It is a lot of messing, though. Depending on the context, tools like Iron Python may be preferable, if you need to do a lot of things from scripts at runtime.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top