Internalsvisibletoattribute: Friend assemblies

Source: Internet
Author: User

When weProgramWhen testing a set write unit, you often encounter a problem: it is used for writing unit tests.CodeCannot directly access the internal type and members in the tested assembly. We usually use the reflection method at this time, but this is not efficient and time-consuming and laborious :(

In. NET Framework 2.0, a new item is called "friend assemblies ). This is similar to friend in C ++. In. net, the internal type or method can be accessed by other assemblies (private is still not accessible) through the method of user meta assembly ).
With the so-called "friend assembly", it is much more convenient for us to write unit tests (what Ms can create a "family assembly" and you can directly access private is better, haha ).
For example, we have two assemblies: system. xml. dll and system. Data. SQLXML. dll. To allow system. XML to access the internal content in system. Data. SQLXML, we can useInternalsvisibletoattributeAttribute:

 
[Assembly: internalsvisibleto ("system. xml")]

If system. XML has a strong name, write the publickey attribute in internalsvisibleto. For example:

 
[Assembly: internalsvisibleto ("system. XML, publickey = 0024000004800000940000000602...")]

In general, the content of the publickey attribute will be long (I use 320 characters -- 160 bytes ). There are two ways to obtain this publickey(use the strong name tool sn.exe ):

    • You can get it directly from the compiled system. xml. dll (of course, internal members and types in system. Data. SQLXML are not used yet ):
      Sn.exe-TP system. xml. dll
    • Read from Keyfile signed for system. xml:
      Sn.exe-P Keyfile. SNK temp. PK// Write the public key from Keyfile. SNK to temp. PK.
      Sn.exe-TP temp. PK// Display the Public Key

For more information, see the following two webpages. Or Google:

    • Strong name tool: http://msdn2.microsoft.com/en-us/library/k5b5tt23 (vs.80). aspx
    • Friend assemblies: http://msdn2.microsoft.com/en-us/library/0tke9fxk (vs.80). aspx

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.