简体   繁体   中英

accessing .net2.0 dll in a .net 4.0 dll

I want to load a .NET 2.0 dll in my.Net4.0 dll . I did search for the solution and found the 'useLegacyV2RuntimeActivationPolicy' attribute that I can add to my app.config as in this url: Mixed mode assembly is built against version 'v1.1.4322'

But the problem for me is that, in my case, I am trying to use the .net2.0 dll in my 4.0 DLL and not in an 4.0 exe . The reason is this 4.0 dll is then used in Excel as an automation add-in.

Currently, when I acccess my .NET 4.0 API (which loads the 2.0 dll for a feature ) from Excel, I get the 'mixed mode 2.0 dll cannot be loaded in 4.0 runtime' which is understandable since COM has loaded the 4.0 CLR and hence cannot load the 2.0 dll.

Somehow I need to make my 4.0 DLL to load the 2.0 runtime. I tried adding a app.config to the 4.0 dll but I guess only.exe reads the app.config by default to load the clr. This is what I have in my app.config in the project of 4.0 dll but no use.

    <?xml version="1.0"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
  <runtime>
    <NetFx40_LegacySecurityPolicy enabled="true"/>
  </runtime>
</configuration>

How do I solve this problem to enable my .NET 4.0 DLL to use .NET 2.0 dll?

Thanks, Mani

The answer is already done by Hans Passant but he has posted as a comment. Hence, answering this separately in here.

The config has to be called excel.exe.config and should be in the same office directory as excel.exe for this scenario to work.

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