简体   繁体   中英

Using AppDomain to load assembly

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.

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

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