1

I have the situation that i have two classes living in different assemblies. Assembly B references Assembly A and Class B needs access to an internal method of Class A (internal so that other users of Class A dont have access, not for security, just to prevent misuse). I control both assemblies, so InternalsVisibleTo is possible to only grant Class B access.

Assembly A

[assembly: InternalsVisibleTo("B")]
public Class A {
...
    internal String secretKey { get; set;}
}

Assembly B

public Class B {
...
    // use A.secretKey which works just fine
}

Actually, was possible because i now need to reference Class A via a newly added (actually autogenerated by Visual Studio) Interface IA.

Assembly A

public interface IA {
    // nothing gets generated for secretKey because its internal
}

[assembly: InternalsVisibleTo("B")]
public Class A : IA {
...
    internal String secretKey { get; set;}
}

Assembly B

public Class B {
...
    // how to use IA.secretKey 
}

So the question is, how can i still grant access to the internal method of Class A to only Class B but not expose it in the interface IA? Is this a nogo or what is best practise?

Is reflection an option and how would i apply it? This is not a unittest/mock scenario, but would PrivateObject fit nevertheless? Dynamic assembly loading?

Totally different approaches welcome!

Rand Random
  • 7,300
  • 10
  • 40
  • 88
BNT
  • 936
  • 10
  • 27
  • You'd just cast to `A` within `B` - the same as you'd have to do even within assembly A. Basically, you want access to a member which is declared on a class, not on the interface, so you need to cast to the class type. – Jon Skeet Nov 16 '17 at 10:05
  • something like (B as A).secretKey? – BNT Nov 16 '17 at 10:07
  • 1
    No, I'd use a cast rather than `as`, but it's not clear what you're trying to cast here. You'd need an instance - you haven't shown that in any of your code. But if you had `IA instance = ...;` you'd use `((A) instance).secretKey;`. Note that this is fragile - if anything else implements your interface, the code will break. – Jon Skeet Nov 16 '17 at 10:09
  • 1
    note that `internal` provides absolutely no degree of security, if that's what you have in mind.. you should encrypt your key. (internal member can be easily accessed by reflection, and can be found in memory by hackers.) – M.kazem Akhgary Nov 16 '17 at 10:09
  • @M.kazemAkhgary thanks, security is not the reason, i updated the question. Can you show me how to get to the value via reflection? This might be a possible solution. – BNT Nov 16 '17 at 10:11
  • Why would you use reflection when casting provides more compile-time safety? – Jon Skeet Nov 16 '17 at 10:12
  • @JonSkeet thanks, i will give casting a try, i dont prefer reflection over your suggestion, i just like to keep as many options as possible open – BNT Nov 16 '17 at 10:13
  • 3
    Note that `InternalsVisibleTo` grants access to all internal members of all classes in assembly A, to all members of assembly B, not just "internal method of Class A to only Class B". – Evk Nov 16 '17 at 10:14

0 Answers0