Prev | Current Page 385 | Next

Jon Skeet

"C# in Depth: What you need to master C# 2 and 3"

)
The only downside to this is that the name of your test assembly lives on in your
production assembly. In theory this could represent a security attack vector if your
assemblies aren??™t signed, and if your code normally operates under a restricted set of
permissions. (Anyone with full trust could use reflection to access the members in the
first place. You could do that yourself for unit tests, but it??™s much nastier.) If this ever
ends up as a genuine issue for anyone, I??™ll be very surprised. It does, however, bring
the option of signing assemblies into the picture. Just when you thought this was a
nice, simple little feature??¦
7.7.3 InternalsVisibleTo and signed assemblies
If a friend assembly is signed, the source assembly needs to specify the public key of the
friend assembly, to make sure it??™s trusting the right code. Contrary to a lot of documentation,
it isn??™t the public key token that is required but the public key itself. For instance,
consider the following command line and output (rewrapped and modified slightly for
formatting) used to discover the public key of a signed FriendAssembly.dll:
c:\Users\Jon\Test>sn -Tp FriendAssembly.dll
Microsoft (R) .NET Framework Strong Name Utility Version 3.5.21022.8
Copyright (c) Microsoft Corporation. All rights reserved.
Public key is
0024000004800000940000000602000000240000525341310004000001
000100a51372c81ccfb8fba9c5fb84180c4129e50f0facdce932cf31fe
563d0fe3cb6b1d5129e28326060a3a539f287aaf59affc5aabc4d8f981
e1a82479ab795f410eab22e3266033c633400463ee7513378bb4ef41fc
0cae5fb03986d133677c82a865b278c48d99dc251201b9c43edd7bedef
d4b5306efd0dec7787ec6b664471c2
Public key token is 647b99330b7f792c
The source code for the Source class would now need to have this as the attribute:
[assembly:InternalsVisibleTo("FriendAssembly,PublicKey="+
"0024000004800000940000000602000000240000525341310004000001"+
"000100a51372c81ccfb8fba9c5fb84180c4129e50f0facdce932cf31fe"+
"563d0fe3cb6b1d5129e28326060a3a539f287aaf59affc5aabc4d8f981"+
"e1a82479ab795f410eab22e3266033c633400463ee7513378bb4ef41fc"+
"0cae5fb03986d133677c82a865b278c48d99dc251201b9c43edd7bedef"+
"d4b5306efd0dec7787ec6b664471c2")]
Unfortunately, you have to either have the public key on one line or use string concatenation
??”whitespace in the public key will cause a compilation failure.


Pages:
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397