none
4.0 app calling 2.0 engine that is using reflection RRS feed

  • Question

  • Hello,

    We have some engine code that is stuck in .net 2.0. We can call this code and its normal operations from a .net 2.0-4.0 app perfectly. But sometimes the 2.0 code uses reflection to call custom libraries that our clients create(these libraries can be .net 2.0-4.0). This works perfectly in .net 2.0-3.5 apps but if it tries to call this reflection code in 4.0 apps it throws an exception when trying to invoke a method from the custom libraries

    Exception:

    Unhandled Exception: System.InvalidProgramException:Common Language Runtime detected an invalid program. at java.lang.reflect.Method.invoke(Object obj, Object[] args)
    

    It can see all the fields/methods fine but as soon as you try to invoke a method it gives us this exception. A few others online have had this same issue but some google searching did not lead me to a solution to getting around this.

    Any ideas?

    Thanks, Matt

    Tuesday, February 14, 2012 10:12 PM

Answers

  • Open app.config and alter it in such way that "useLegacyV2RuntimeActivationPolicy" is an attribute of "startup" under "configuration":


    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <startup useLegacyV2RuntimeActivationPolicy="true"></startup>
    </configuration>

    (I've built my own plug-in framework and it works like a charm 4.0 -> 2.0)


    Regards, MusicDemon

    Friday, February 24, 2012 1:16 PM

All replies

  • To call into .NET assemblies, the application must be running in the .NET 4.0 runtime.

    Does this happen with any .NET 4.0 code or does it happen with some and with others it doesn't?


    Paulo Morgado

    Wednesday, February 15, 2012 2:27 AM
  • I think, the code in 2.0 library is trying to call a function in 4.0 library which is making use of some functionality which is not available in 2.0 framework. I mean, if a function makes use of optional parameters or dynamics( which are available only in 4.0) is invoked from a 2.0 application, then it will definitely fail. Check it.

    Please mark this post as answer if it solved your problem. Happy Programming!

    Wednesday, February 15, 2012 5:35 AM
  • It's probably not a framework issue, but a 32-bit vs 64-bit issue.

    If you compile to 'Any CPU' then your app will run as a 64-bit process in a 64-bit environment.  If so, then it will not be able to load and reflect upon a 32-bit assembly.  To get around this, compile as 'x86', which will force your app to run as a 32-bit process always.  However, in this case the app will not be able to load and reflect upon 64-bit assemblies.  If you need to do both (load both 32-bit and 64-bit during the same execution run of your app) then you need to have different asssemblies - one to load and reflect upon 32-bit assemlies and one to load and reflect upon 64-bit assemblies - and then communicate this information back to your main app.


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)

    Wednesday, February 15, 2012 3:21 PM
  • Optional arguments (parameters aren't optional) are resolved at compile time. They have no influence (other than its value) at runtime.

    After compilation dynamic objects are just objects. The compiler did genrate calls into objects that are only in .NET 4.0 libraries and thus requires the 4.0 runtime.


    Paulo Morgado

    Wednesday, February 15, 2012 3:32 PM
  • Seems like I didn't explain myself quite clearly, see attached image for a better explanation.

    The reflected methods that are failing are really simple things like returning just an integer or a string no optional paramaters or anything like that.

    @Dave 

    I tried setting everything explicitly to x86 and then everything to x64, no luck same exception.

    Thursday, February 16, 2012 9:19 PM
  • See if this can give you more info: http://support.microsoft.com/kb/312544

    Can you reproduce this with very simple and very small assemblies?


    Paulo Morgado

    Friday, February 17, 2012 1:26 AM
  • Try to add supportedRuntime element in your config file.

    <supportedRuntime version="v2.0.50727" />

    More details, please refer to:

    http://msdn.microsoft.com/en-us/library/w4atty68.aspx


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us

    • Proposed as answer by MusicDemon Friday, February 24, 2012 1:11 PM
    • Unproposed as answer by MusicDemon Friday, February 24, 2012 1:11 PM
    Thursday, February 23, 2012 9:10 AM
  • Don't you mean this:

    <supportedRuntime version="v4.0.30319" />

    or even this:

    <requiredRuntime version="v4.0.30319" />

    The problem seems to be with assemblies that require the 4.0 runtime. Your suggestion is to use only the 2.0 runtime which would make the current working scenario to stop working.

    Am I missing something here?


    Paulo Morgado

    Thursday, February 23, 2012 10:20 AM
  • Does this happen with any .NET 2.0 assembly or with a particular one?

    Could it be that the problem is with the assembly with the reflection code and not the assemblies being reflected upon?

    Can you build a very small very simple program that reproduces this?

    This probably won't help you but you might want to read this: http://support.microsoft.com/kb/312544


    Paulo Morgado

    Thursday, February 23, 2012 10:22 AM
  • Open app.config and alter it in such way that "useLegacyV2RuntimeActivationPolicy" is an attribute of "startup" under "configuration":


    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <startup useLegacyV2RuntimeActivationPolicy="true"></startup>
    </configuration>

    (I've built my own plug-in framework and it works like a charm 4.0 -> 2.0)


    Regards, MusicDemon

    Friday, February 24, 2012 1:16 PM