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