现在的位置: 首页 > 综合 > 正文

C# 2.0 中的 Friend Assemblies

2019年10月06日 ⁄ 综合 ⁄ 共 3175字 ⁄ 字号 评论关闭
C# 2.0 中的 Friend Assemblies
C# 2.0 allows you to share non-public types and their members with other assemblies. The assemblies to which access is granted are called friend assemblies. There are two ways you can grant friend assembly access to types and members: 1) by declaring an InternalsVisibleTo assembly attribute for the assembly you want to allow friend assembly access to, which grants access to all of the assembly's types and members that are defined with the internal access modifier, and 2) by declaring an StrongNameIdentityPermission attribute for each type you want to allow friend assembly access to. Please note that the StrongNameIdentityPermission method only allows access to types and NOT to individual members; whereas the InternalsVisibleTo method allows accessibility to types and members (so long they're defined with the internal access modifier).

InternalsVisibleTo Access

The following tutorial shows you the steps necessary to make an assembly a friend of another. Note that only types and members declared with the internal access modifier will be accessible from a friend assembly. Private, protected and other non-public access modifiers will NOT be accessible from friend assemblies. So, remember, you MUST declare a type or member as internal when using the InternalsVisibleTo method. This will cause ALL of the types and its members to become "friendly" (not just some). In order to make this happen, you need to do two things:

  1. The assembly you want to make a "friend" (the assembly that will be allowed access) has to be strong-named. To strong name the friend assembly:
    1. Go the Microsoft .NET Framework SDK v2.0 SDK Command Prompt.
    2. Create the strong name key file and link it ot the friend assembly:
      For .Net SDK compilations:

      1. Navigate to the friend assembly's project directory and then type and execute the following command: sn -k MyKey.snk
      2. Adding the following line to the AssemblyInfo.cs file of the friend assembly: [assembly:AssemblyKeyFileAttribute(@"../../MyKey.snk")]
      3. Linking the friend assembly with the strong name key file by typing and executing the following command: al /out:MyFriendAssembly.dll /keyfile:MyKey.snk

      For VS.NET 2005 compilations:

      1. Right-click on the friend assembly's project and then click on Properties.
      2. Click on the Signing tab in the left-hand pane and then click on the Sign the assembly checkbox.
      3. Click on the Choose a strong name assembly key file: dropdown list and then click on New....
      4. Enter a name for the key file in the key file name: text box.
      5. Uncheck the Protect my key file with a password and then click on the OK button.
    3. Compile the friend assembly.
  2. The assembly containing the internal types and members you want to allow access to must also be strong named. Perform the same steps above but with this assembly instead. In other words, both assemblies must be strong named.
  3. The assembly that is going to allow its types and members to be "friendly" to the friend class needs to have the following line placed in its AssemblyInfo.cs file: [assembly: InternalsVisibleTo("ClassLibrary2, PublicKeyToken=4dc529a74920bd96")]. In this example, assembly ClassLibrary1 is allowing access to assembly ClassLibrary2. To obtain the public key token of the friend assembly, open up a Microsoft .NET Framework SDK v2.0 SDK Command Prompt and then type and execute sn -T FriendAssembly.dll (in this example it would be ClassLibrary2.dll) in the directory where the friend assembly is located.
  4. You can now compile the solution and now have the ability to access non-public types and members in the sharing assembly from the friend assembly.

StrongNameIdentityPermission Access

(Coming soon...)
原文地址: http://www.dotnetfun.com/articles/csharp/2.0/WhatsNew20FriendAssemblies.aspx
 

抱歉!评论已关闭.