compile code and add it to the context

Hello,

I'm using the c# compiler interface in order to compile code on runtime.

in the compiled code I'm adding classes, and I want thses classes to be available in the next compilation.

Is there a way to add a reference the in-memory assembly

thanks,




Answer this question

compile code and add it to the context

  • Tom Frey

    You are a little bit unclear. What i figured out is that you need posibility to see all new things you're writing and changing immediately. Probably you reference that library as a file reference instead of a Project Reference.



  • Aziz Sedaghat

    As far as I know, you cannot add a reference unless you have a path, which means you are stuck to have the file on disk. I am not sure why there is this limitation, as it would stand to reason that all the metadata are equally available in-memory with the loaded assembly, but I couldn't find an accessible method that would allow you to reference an assembly directly.

    Anyway, if you look at how the compilation takes place, you will notice that a file is always written to disk, with the only difference that if you do not specify an output path yourself, a temp file will be created and later deleted. Which means you don't pay any additional IO penalty, except for the fact that you will have to keep track and delete the generated assembly yourself.

    I never tried what you ask, but I guess you could change the file at will. The only problem is that unless you load the assembly in a separate AppDomain, you won't be able to unload the previous copy.

    Sorry for the bad news
    --mc


  • Ray.B

    You will need a full path to the assembly you just created.

    If you are creating your assemblies with GenerateInMemory = false, all you need to do is to add a reference to the assembly. You can retrieve the full path using the property CompilerResults.CompiledAssembly.Location.

    If you are creating your assemblies with GenerateInMemory = true, this won't be possible as while the assembly is actually written to the disk (it always is, regardless of how you set the flag), it will be a temporary file that won't be kept.

    The only solution is to set explicitly a full path to CompilerParameters.OutputAssembly and use that very same full path to add a reference (CompilerParameters.ReferencedAssemblies.Add()). This will mean that the file is no longer temporary and will persist on the disk. The good news is that you have full control over where the assembly is going to reside, so you can clean up later.

    HTH
    --mc


  • Javfarary

    is there a way to avoid all the IO operation, like writing files. I want real in-memory assembly.

    another question - if lets say I created an assembly and referenced it. can I detach the reference, replace the file, and reload the assembly

    thanks.



  • Roland J Young

    I'm compiling code at runtime - the compiled code contains new classes. I want the new classes to be available in the next compilation I do

  • h3nry

    I still have no clear picture, what you want to acomplish. If you explain your problem little bit more, you will receive much clear answers, probably in completly different direction.
    My second gues is that you are trying to replace Application referenced assembly with new version, which has new functions. Is that what you are trying to do

  • compile code and add it to the context