fork(1) download
  1. using System;
  2.  
  3. public interface IFriendKey { object Id {get; set;} }
  4.  
  5. class Friend<TFriend>
  6. {
  7. protected void FriendAssert(IFriendKey key)
  8. {
  9. if ( key == null || key.Id == null || key.Id.GetType() != typeof(TFriend) )
  10. throw new Exception("No right to execute the called method.");
  11. }
  12. }
  13.  
  14. class A : Friend<B>
  15. {
  16. public void f(IFriendKey key)
  17. {
  18. FriendAssert(key);
  19. Console.WriteLine("ONLY class B can execute this method successfully, even though it is declared public.");
  20. }
  21. }
  22.  
  23. class B
  24. {
  25. private class AFriendKey : IFriendKey
  26. {
  27. public object Id {get; set;}
  28. }
  29.  
  30. IFriendKey Key { get { return new AFriendKey() {Id = this}; } }
  31.  
  32. public void g()
  33. {
  34. new A().f(this.Key);
  35. }
  36. }
  37.  
  38. public class Test
  39. {
  40. public static void Main()
  41. {
  42. new B().g();
  43. }
  44. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
error CS5001: Program `prog.exe' does not contain a static `Main' method suitable for an entry point
Compilation failed: 1 error(s), 0 warnings
stdout
Standard output is empty