1

How would I add a dynamically loaded assembly as a reference to the CompilerParameters.RerencedAssemblies collection? I know I can add a reference by Assembly.Location, but my dynamically loaded assembly has no location. It is in the AppDomain though. But I cannot find a way to add it as a referenced assembly. Storing it in the GAC is no option.

I tried to serialize it to disk and then add it as a reference, but that fails miserably. (The serialized assembly is not a valid dll).

Any thoughts? (.Net 4.5)

BTW, this is a duplicate of this question, but it got no answers. And it's pretty old, so that's why I'm asking it again.

Community
  • 1
  • 1
Albert Romkes
  • 1,125
  • 1
  • 8
  • 19
  • How is that dynamic assembly loaded ? Where is it loaded from ? – fahadash Jul 30 '14 at 14:25
  • The assembly is uploaded to a DB (3th party app). The 3th party app loads this Assembly with `Assembly.Load` (outside my control). And this loaded assembly has references. Which are loaded in the AppDomain, but with an empty Location since they are not in the GAC. – Albert Romkes Jul 30 '14 at 15:46

1 Answers1

1

Perhaps you need AssemblyBuilder

If your dynamic assembly was built using AssemblyBuilder, Here is what MSDN says about dynamic assemblies.

A persistable dynamic assembly is saved using the AssemblyBuilder.Save method. The Save method specifies the name of the file to which the assembly should be written.

You can use the Save method to save it to file and then use the assembly location for adding reference.

fahadash
  • 3,133
  • 1
  • 30
  • 59
  • I cannot cast my assembly to an AssemblyBuilder. I can see my assembly loaded in the AppDomain (no Location). – Albert Romkes Jul 30 '14 at 15:45
  • Check out the answer to this discussion, the object of Assembly class does not have a way to be saved to disk. http://stackoverflow.com/questions/6856376/how-to-save-object-of-the-assembly-class-to-disk-in-net – fahadash Jul 30 '14 at 15:54
  • Also check this out: http://stackoverflow.com/questions/2830160/c-sharp-referencing-a-type-in-a-dynamically-generated-assembly – fahadash Jul 30 '14 at 16:00
  • 1
    I know an `Assembly` (actually, it's of Type `RuntimeAssembly`) has no method 'Save'. In order to save it, one should use the `AssemblyBuilder`. But a `RuntimeAssembly` cannot be casted to an `AssemblyBuilder`. I don't see how your second comment helps me out. It talks about referencing types in dynamic assemblies. – Albert Romkes Jul 30 '14 at 16:05
  • What I am trying to say is, either your 3rd Party Library should give you reference to `AssemblyBuilder` somehow or look for alternate libraries that might. It won't hurt to ask the vendor of your library if there is a way to save your assembly. – fahadash Jul 30 '14 at 16:33
  • Yes, indeed. But then I'll have to wait until 3022 I guess. Thanks anyway! – Albert Romkes Jul 30 '14 at 17:22