I am new to C#, and I have a problem for which in C++ I would normally use the friend
identifier. Now I know the friend
keyword doesn't exist in C#, but I don't have any experience with how to work around this (except for making all class variables public properties, which I want to avoid if I can).
I have the following scenario:
public class A
{
public string Info { get; set; }
/* much more data */
}
public class B
{
private A m_instanceOfA;
public B(A a) { m_instanceOfA = a; }
public Info { get return A.info; set A.Info = value; }
/* And some more data of its own*/
}
public class C
{
private A m_instanceOfA;
// I need a constructor of C, which needs to set C.m_instanceOfA
// to the same value as b.m_instanceOfA.
public C(B b) { m_instanceOfA = b.m_instanceOfA ; } // <--- Not allowed!
/* And some more data of its own*/
}
Is there any other clever way, without making B.m_instanceOfA
public, to give C
access to this variable (only in the constructor)?
You can use the trick shown below to create a method FriendRecieveMessageFromAlice
on Bob that can only be called by Alice
. An evil class, Eve
, won't be able to call that method without using reflection on private members.
I'm curious to know if this or another solution have been suggested before by other people. I've been looking for a solution to that problem for months, and I never saw one that ensured real friend
semantics provided that reflection isn't used (you can circumvent nearly anything with it).
public interface IKey { }
public class Alice
{
// Alice, Bob and Carol must only have private constructors, so only nested classes can subclass them.
private Alice() { }
public static Alice Create() { return new Alice(); }
private class AlicePrivateKey : Alice, IKey { }
public void PublicSendMessageToBob() {
Bob.Create().FriendRecieveMessageFromAlice<AlicePrivateKey>(42);
}
public void FriendRecieveMessageFromBob<TKey>(int message) where TKey : Bob, IKey {
System.Console.WriteLine("Alice: I recieved message {0} from my friend Bob.", message);
}
}
public class Bob
{
private Bob() { }
public static Bob Create() { return new Bob(); }
private class BobPrivateKey : Bob, IKey { }
public void PublicSendMessageToAlice() {
Alice.Create().FriendRecieveMessageFromBob<BobPrivateKey>(1337);
}
public void FriendRecieveMessageFromAlice<TKey>(int message) where TKey : Alice, IKey {
System.Console.WriteLine("Bob: I recieved message {0} from my friend Alice.", message);
}
}
class Program
{
static void Main(string[] args) {
Alice.Create().PublicSendMessageToBob();
Bob.Create().PublicSendMessageToAlice();
}
}
public class Eve
{
// Eve can't write that, it won't compile:
// 'Alice.Alice()' is inaccessible due to its protection level
private class EvePrivateKey : Alice, IKey { }
public void PublicSendMesssageToBob() {
// Eve can't write that either:
// 'Alice.AlicePrivateKey' is inaccessible due to its protection level
Bob.Create().FriendRecieveMessageFromAlice<Alice.AlicePrivateKey>(42);
}
}
The trick is that the method Bob.FriendRecieveMessageFromAlice
requires a (dummy) generic type parameter which serves as a token. That generic type must inherit from both Alice
, and from a dummy interface IKey
.
Since Alice
does not implement IKey
itself, the caller needs to provide some subclass of Alice
which does implement IKey
. However, Alice
only has private constructors, so it can only be subclassed by nested classes, and not by classes declared elsewhere.
This means that only a class nested in Alice
can subclass it to implement IKey
. That's what AlicePrivateKey
does, and since it is declared private, only Alice
can pass it as the generic argument to the Bob.FriendRecieveMessageFromAlice
, so only Alice
can call that method.
We then do the same thing the other way round so that only Bob
can call Alice.FriendRecieveMessageFromBob
.
It is worth noting that, when called, Bob.FriendRecieveMessageFromAlice
has access to the TKey
generic type parameter, and could use it to spoof a call from Alice
on another method OtherClass.OtherMethod<OtherTkey>
accepting a OtherTKey : Alice, IKey
. It would therefore be safer to make the keys inherit from distinct interfaces: Alice, IBobKey
for the first, and Alice, IOtherKey
for the second.
Bob
itself can't call its own method Bob.FriendRecieveMessageFromAlice
.Bob can have multiple friends with distinct friend methods:
// Can only be called by Alice, not by Carol or Bob itself
Bob.FriendRecieveMessageFromAlice <TKey>(int message) where TKey : Alice, IKey { }
// Can only be called by Carol, not by Alice or Bob itself
Bob.FriendRecieveMessageFromCarol <TKey>(int message) where TKey : Carol, IKey { }
I'd be interested to know if there is some way to find tricks like this in a more efficient way than brute-force trial and error. Some kind of "algebra of C#'s type system", that tells us what restrictions can be enforced and what can't, but I haven't seen any discussion on that kind of topic.
You can use the internal keyword. Your type (or type member) will then only be visible to other types within the same assembly; And also:
If you need your internal types to be visible from other assemblies, you can use the InternalsVisibleToAttribute. This attribute targets your whole assembly and is usually written in the AssemblyInfo.cs file.
PS: Friend keyword doesn't exists in C# but the concept of friendship exists (not exactly the same as the one in C++), it is described on the Friend Assemblies article from the MSDN. Note also that a friend keyword exists in VB.NET and has the exact same behaviour than the C# internal keyword.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With