How to declare a friend assembly?

前端 未结 3 1098
青春惊慌失措
青春惊慌失措 2020-11-30 17:29

I have 2 projects in my solution:

  1. Assembly (Basic Library)
  2. Test Assembly (NUnit)

I had declared the test assembly as friends assembly i

相关标签:
3条回答
  • 2020-11-30 18:01

    You need to sign both assemblies, because effectively both assemblies reference each other.

    You have to put the public key in the InternalsVisibleTo attribute. For example, in Protocol Buffers I use:

    [assembly:InternalsVisibleTo("Google.ProtocolBuffers.Test,PublicKey="+
    "00240000048000009400000006020000002400005253413100040000010001008179f2dd31a648"+
    "2a2359dbe33e53701167a888e7c369a9ae3210b64f93861d8a7d286447e58bc167e3d99483beda"+
    "72f738140072bb69990bc4f98a21365de2c105e848974a3d210e938b0a56103c0662901efd6b78"+
    "0ee6dbe977923d46a8fda18fb25c65dd73b149a5cd9f3100668b56649932dadd8cf5be52eb1dce"+
    "ad5cedbf")]
    

    The public key is retrieved by running

    sn -Tp path\to\test\assembly.dll
    

    Alternatively, get it from the .snk file:

    sn -p MyStrongnameKey.snk public.pk
    sn -tp public.pk
    
    0 讨论(0)
  • 2020-11-30 18:01

    I think you need to put in the strong name, which would be something like "Company.Product.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=17135d9fcba0119f". I assume Company.Product.Tests is your assembly name and 17135d9fcba0119f is the public key.

    Another way to resolve this problem would be not to use separate assemblies. I usually put the source code and the testing code in the same assembly. I don't know if you have any special concern that you must separate them.

    0 讨论(0)
  • 2020-11-30 18:15

    You can directrly get publicKey from assembly which you interest, without magic with sn.exe

    <!-- language: c# -->
    var assemblyName = Assembly.GetExecutingAssembly().GetName();
    Console.WriteLine("{0}, PublicKey={1}",
        assemblyName.Name,
    string.Join("", assemblyName.GetPublicKey().Select(m => string.Format("{0:x2}", m))));
    
    0 讨论(0)
提交回复
热议问题