. NET low version call high version

Source: Internet
Author: User

The previous time in the project conversion (previously created in 2008, now turned into 2010), encountered a problem. The following is a brief description:

Project A (target framework is 3.5, some forms are created with WPF)

Item B (the target framework is 2.0, the class in Project A is called)

In VS2008, when you add a reference to project A in project B, there is no problem and you can add success. However, after turning into VS2010, item B cannot add a reference to project A, and will add an error, presumably meaning. Net

version is too high, The original thought is 2.0 can not call 3.5, and then do their own experiments, found to be able to invoke. But if this 3.5 project adds some technology that doesn't belong to 2.0, that's the problem.

The solution at the time was to change the target framework of project B to 3.5, but because the software was developed very early, the target framework for many projects was 2.0, Project B was a basic component, and many other

Project reference, if you change project B, that many projects have to change (consider safety factors, try not to big move,) since this road does not work, then use reflection, add Project C,c to define some interfaces, a in the implementation

These interfaces, B does not call a directly, but the C (is not very messy ah!) )

Create three items

1 SampleObject35 Target Frame 3.5

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSampleObject2;6 namespaceSampleObject357 {8     Public classSamplecalcute:isamplecalcute9     {Ten         Public voidPrintsum (intAintb) One        { A            varsum = a +b; - Console.WriteLine (sum); -             } the  -     } -}
View Code

2 SampleObject2 Target Frame 2.0

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Text;4 5 namespaceSampleObject26 {7    Public  InterfaceIsamplecalcute8     {9     voidPrintsum (intAintb); Ten     } One}
View Code
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Text;4 usingSystem.Reflection;5 namespaceSampleObject26 {7      Public classcalcuteinstance8     {9          Public Staticisamplecalcute getsampleinstance ()Ten         { One             stringDirectory =AppDomain.CurrentDomain.BaseDirectory; A             stringfilename = Directory +"SampleObject35.dll"; -             if(System.IO.File.Exists (filename)) -             { theAssemblyassembly.loadfile (filename); -                 Objectobj = the. CreateInstance ("Sampleobject35.samplecalcute",true, BindingFlags.Public | BindingFlags.Instance,NULL,NULL,NULL,NULL); -                 if(obj isIsamplecalcute) -                 { +                     return(isamplecalcute) obj; -                 } +             } A             return NULL; at         } -     } -}
View Code

3 SampleInvoke2 Target Frame 2.0

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Text;4 usingSampleObject2;5 namespaceSampleInvoke26 {7    Public  classSample8     {9      Ten        Public Static voidMain (string[] args) One       { AIsamplecalcute Samplecalcute =calcuteinstance.getsampleinstance (); -Samplecalcute.printsum (2,3); - Console.readkey (); the            -       } -     } -}
View Code

Note: You need to place the SampleObject35 in the SampleInvoke2 lib directory

The above situation is only for the cause of the old system of some transformation, if it is a new system, there is no need to use 2.0.

. NET low version call high version

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.