none
How to use 4.0 dll with 3.5 Project RRS feed

  • Question

  • Hi,

    Hi, 

    I have a c# project which is in 3.5 framework. Now i want to use a 4.0 version dll in my project. Bacause of some reasons I dont want the upgrade my project to 4.0. Can any one suggest a solution?Thanks and Regards,

    Prajith.

     


    Thanks and Regards, Prajith
    Monday, December 12, 2011 9:21 AM

Answers

  • The only way to use a .NET 4 DLL from within a .NET 3.5 or older project is to use COM -

     

    You can make your .NET 4 DLL expose itself via COM, and then use the COM wrappings to read this into a .NET 3.5 project.


    However, this is a LOT of work, and will still require .NET 4 to be installed on the machine.  As such, there is typically no reason to do this instead of upgrading your application to .NET 4.0, as it just adds overhead and complexity.

     

    Adavesh: You can't add a .NET 4 assembly as a reference to a .NET 3.5 project - it will fail with an error stating that the framework target is greater than the current project's framework, and fail to load.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Proposed as answer by MusicDemon Monday, December 12, 2011 6:10 PM
    • Marked as answer by Paul Zhou Tuesday, December 20, 2011 8:14 AM
    Monday, December 12, 2011 5:48 PM
    Moderator
  • .NET 3.5/3.0/2.0(using CLR 2.0) applications cannot directly use libraries targets .NET 4.0(using CLR 4.0). Such assemblies will fail to load in 3.5/3.0/2.0 runtime(CLR 2.0).

    However because of in-proc SxS feature in .NET 4.0, you can use a COM object in your .NET 3.5 app which is implemented in .NET 4.0 and it will load both CLR 2.0 and CLR 4.0 into memory and it will work.

    For more info about in-proc SxS, read the thread here: http://social.msdn.microsoft.com/Forums/en-US/netfxappcompatprerelease/thread/a181e9b0-9d67-4d3c-80c4-11529196d3bc


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Paul Zhou Tuesday, December 20, 2011 8:15 AM
    Thursday, December 15, 2011 6:29 AM

All replies

  • It can't. .Net 4 has other core features and contains little patches over the .Net 3.5 core, thus other code which can't be used together.
    Regards, MusicDemon
    Monday, December 12, 2011 9:52 AM
  • It depends on which DLL you want to use. If it is one DLL which doesn't refer other dlls, then it is very simple. Just add the reference. But, if that DLL refers other DLLs,then you need to install all those references in your machine.
    Please mark this post as answer if it solved your problem. Happy Programming!
    Monday, December 12, 2011 11:11 AM
  • The only way to use a .NET 4 DLL from within a .NET 3.5 or older project is to use COM -

     

    You can make your .NET 4 DLL expose itself via COM, and then use the COM wrappings to read this into a .NET 3.5 project.


    However, this is a LOT of work, and will still require .NET 4 to be installed on the machine.  As such, there is typically no reason to do this instead of upgrading your application to .NET 4.0, as it just adds overhead and complexity.

     

    Adavesh: You can't add a .NET 4 assembly as a reference to a .NET 3.5 project - it will fail with an error stating that the framework target is greater than the current project's framework, and fail to load.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Proposed as answer by MusicDemon Monday, December 12, 2011 6:10 PM
    • Marked as answer by Paul Zhou Tuesday, December 20, 2011 8:14 AM
    Monday, December 12, 2011 5:48 PM
    Moderator
  • Not sure, how you are commiting that it is  4.0 dll.? It all depends on "Runtime version" the assembly is target to. You can check this attribute in project properties window of the assembly /  using ildasm, etc.,

    Runtime version for 4.0 is: v4.0.30319

    Runtime version for 3.5 / 3.0 /2.0  are v2.0.50727


    --Krishna
    Monday, December 12, 2011 7:01 PM
  • What is in the 4.0 dll that you wish to use? perhaps there is a 3.5 equivalent.
    Monday, December 12, 2011 10:12 PM
  • Reed Copsey,

    Thanks for the reply. I will try that option.

    but before we go ahead i would like to tell you the actual problem.

    See, We are using Client Object Model to communicate with Sharepoint 2010. Cleint Object Model is not working with SP2010 when the projet is in dotnet 4.0 version. Thats why we wanted to have the project in 3.5 version only. But at the same time we need to use a dll which is from some other team and is in 4.0 version. this is the actul problem.

    Is there anything else left out to try in this case?

     

    alsp please see the below http://community.office365.com/en-us/f/154/t/17356.aspx

    @Krishnav, the dll version is 4.0 only. i have double checked :)

     


    Thanks and Regards, Prajith
    Tuesday, December 13, 2011 6:25 AM
  • .NET 3.5/3.0/2.0(using CLR 2.0) applications cannot directly use libraries targets .NET 4.0(using CLR 4.0). Such assemblies will fail to load in 3.5/3.0/2.0 runtime(CLR 2.0).

    However because of in-proc SxS feature in .NET 4.0, you can use a COM object in your .NET 3.5 app which is implemented in .NET 4.0 and it will load both CLR 2.0 and CLR 4.0 into memory and it will work.

    For more info about in-proc SxS, read the thread here: http://social.msdn.microsoft.com/Forums/en-US/netfxappcompatprerelease/thread/a181e9b0-9d67-4d3c-80c4-11529196d3bc


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Paul Zhou Tuesday, December 20, 2011 8:15 AM
    Thursday, December 15, 2011 6:29 AM