1

Answer to my question my already be on stackoverflow however, I just can't find the right one/not get it to work.

What I'm trying to do is, load the .dll to my Windows Form, use reflection to retrieve method/class names. I have got it to work using Assembly.LoadFrom however it's doing exactly what I wanted as this locks the .dll file.

I have read around and found out that AppDomain can do the trick however, I just can't get it to do the trick.

Below is the code I have tried however, in CreateInstanceFromAndUnwrap, I don't know which typeName it's looking for.

var executableFilePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

AppDomain domain = AppDomain.CreateDomain(
               DllPath,
                null,
                new AppDomainSetup
                {
                    ApplicationBase = Path.GetPathRoot(executableFilePath),
                    PrivateBinPathProbe = string.Empty,

                    PrivateBinPath = string.Join(";", DllPath, executableFilePath),
                    ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile,
                });



      var temp = domain.CreateInstanceFromAndUnwrap(DllPath, typeof(????).FullName, false,
            BindingFlags.Default, null, null, null, null);

The .dll I will load at runtime will not be referenced into my Windows Form application.

Yagnesh Cangi
  • 393
  • 3
  • 17

1 Answers1

0

Try to look this thread: How to Load an Assembly to AppDomain with all references recursively?

Method CreateInstanceFromAndUnwrap returns you transparent proxy from your typeof(????).FullName. So you should:

  1. create some class which inherited from MarshalByRefObject in your importing dll
  2. use typeof(%yourNewMarshalByRefClassNameFrom1%).FullName
Community
  • 1
  • 1
  • I tried the top answer on that question and kept gaving me and error when I do `value.GetAssembly(ReferenceDll)`.... Could not load file or assembly `Gamers_V1_0_0, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified`. – Yagnesh Cangi Mar 26 '15 at 11:17
  • Try to check your local directory path it maybe different. `var currentDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); var assemblyFullPath = Path.Combine(currentDir, "assembly name");` – bazarniy Mar 26 '15 at 11:24
  • I have tried `if(File.Exists` however it's passes that and gives me a same error – Yagnesh Cangi Mar 26 '15 at 11:26
  • Maybe assembly has got some dependencies and they didn't copy to build folder? – bazarniy Mar 26 '15 at 11:33