C #4.0 impact of new features on. NET interoperability
When talking about the impact of the new C # version on. NET interoperability, We have to first talk about the new features of C #4.0.
Dynamically typed objects.
Optional and named parameters.
Improved com interoperability.
Safe co-And contra-variance.
Among them, 2nd and 3 are related to interoperability. The optional and named parameters at are not new concepts. The main reason is the support of the compiler. For example, VB. NET already supports optional parameters. C # in recent years #CommunityThe call for this feature is too high, and it seems that it has finally taken effect.
1. Optional parameters
Many com Methods accept optional parameters. When calling this method, you can pass the specified value for the optional parameter as needed, or ignore this parameter and use the default value of this parameter. In use hostingCodeWhen calling the com method, the calling complexity varies according to different. NET languages. Because Visual Basic. Net itself supports optional parameters (OptionalKeyword), which can be used in an optional way. However, if C # is used, the situation will be quite different. Because C # does not support optional parameters, values must be transmitted for each parameter in the method. For example, values can be transmitted for optional parameters.System. type. MissingTo set the default value of this parameter. Because values must be passed for all optional parameters, using C # To call the com method with optional parameters is not as convenient and flexible as Visual Basic. net.
A friend who has used office Pia must have encountered the following example when operating Word documents:
Object Filename = "Test.docx" ;
Object Missing = system. reflection. Missing. value;
Doc. saveas ( Ref Filename,
Ref Missing, Ref Missing, Ref Missing,
Ref Missing, Ref Missing, Ref Missing,
Ref Missing, Ref Missing, Ref Missing,
Ref Missing, Ref Missing, Ref Missing,
Ref Missing, Ref Missing, Ref Missing );
To call the saveas method, you have to enter all unnecessary parameters. This is because the old C # version does not support optional parameters.
When C #4.0 appears, the situation is quite different. For example, the above Code can be written as follows:
Doc. saveas ("Test.docx ");
Note: proficient. in section 6.4.7 "passing optional parameters", the book "net interoperability" describes how to use the optional and defaultparametervalue attributes to support optional parameters for the com method constructed and exported by C.
2. Improvement Support for com interoperability
The 3rd point of the new feature mentions the improvement especially for com interoperability. This includes:
1. the CLR of multiple host versions in the same process. In this way, you can select the runtime version required (during compilation) for the managed com build.
2. You no longer need to use PIA (primary InterOP Assembly) to build interactions on com. In the past, when you published a com build, Microsoft suggested that you release a PIA with the build. This includedProgramSet PIA to be referenced by the hosted application client. In. NET Framework 4.0, Pia will be weakened. The C # and VB compilers determine which part of the com api is used by your program and only package this part into the IA (interoperability assembly ), directly add it to your application assembly.
3. Redefine QueryInterface. You can useSystem. runtime. interopservices.The icustomqueryinterface interface customizes the iunknown: QueryInterface method implemented by the managed code. An application can use it to return specific interfaces.
Iii. New Features of. NET Framework 4.0 on threads
Finally,. NET Framework 4.0 adds the yield () method to the Thread class thread. It allows the calling thread to give the current CPU resource to another prepared thread. The operating system selects a new thread. Giving up only results in processing the cpu Of The Calling thread. The operating system does not switch to another CPU, even if the CPU is in idle state. If there are no other executable threads on the current CPU, the call of the thread. Yield () method returns false.
The thread. Yield () method is equivalent to calling Win32 API using pinvoke.Switchtothread. With this new method, you can avoid the additional overhead introduced by using platform calls and solve the problem that platform calls cannot handle custom thread behaviors.
Refer:
1.[Book] proficient in. Net interoperability: P/invoke, C ++ InterOP and COM InterOP
2. What's new in the. NET framework 4
3. [Video] managed and native code interoperability: Best Practices
Archive in: InterOP, com InterOP, C #4.0,. net From: http://interop123.com/blogs/xcui/archive/2009/05/16/c-4-0-net.aspx