This article is a mirror article of machine translation, please click here to jump to the original article.

View: 21086|Reply: 0

[Source] InternalsVisibleToAttribute exposes the internal member to the specified friend assembly

[Copy link]
Posted on 2/6/2017 2:27:59 PM | | |
Introduction to Youyuan Assembly
We know that members of a class that are defined as internal (including types, methods, properties, variables, events) can only be accessed in the same assembly (of course, I'm talking about the normal way here, not including access via reflection). This rule is in . .NET 2.0 is slightly broken, allowing us to set an assembly's internal members to be accessed by specific members. We call the assembly that contains these internal members the source assembly, and the assembly set is called the friend assembly.

Friend assembly use
This Attribute is used at the assembly level.


This way, after the FriendAssembly program references the assembly, it can access the internal Name property.
Generally speaking, this assembly:InternalsVisibleTo("FriendAssembly") is better placed in the AssemblyInfo.cs, after all, it is at the assembly level.

When to use InternalsToVisibleTo
In fact, this attribute is not used in general applications, and we should not abuse this attribute. Because in general, a well-designed assembly does not need to be open to the outside world.
But unit testing scenarios can take advantage of this feature. With this feature, we can specify that the code for unit tests has access to those internal members so that we can unit test those members. However, this requires writing the name of the friend assembly dead in the source assembly, which causes some security issues. Security can be guaranteed by introducing signature assemblies.

InternalsToVisibleTo and the signature assembly
You can directly specify the name and public key of the friend meta assembly to InternalsVisibleTo.
Use the VS developer command line to generate the public key for FriendAssembly:


Specify the name of the friend assembly and the public key to the source assembly:


In fact, since the friend assembly references the source assembly, and the signed assembly references an unsigned assembly, and the signed assembly cannot specify an unsigned friend assembly, any of them are signed, then they need to be signed.





Previous:Automatically jump to the Tenpay payment transfer page and fill in the information automatically
Next:Use InternalsVisibleTo to add "friend assembly" to assembly
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com