In C# 4.0, is there any way to make an otherwise private member of one class available only to a specific other class?

前端 未结 9 1894
旧时难觅i
旧时难觅i 2021-01-31 19:27

We\'re creating an object hierarchy where each item has a collection of other items, and each item also has a Parent property pointing to its parent item. Pretty st

9条回答
  •  难免孤独
    2021-01-31 19:45

    Here's a way you can simulate friend in C#:

    Mark your properties internal and then use this attribute to expose them to friend assemblies:

    [assembly: InternalsVisibleTo("Friend1, PublicKey=002400000480000094" + 
                                  "0000000602000000240000525341310004000" +
                                  "001000100bf8c25fcd44838d87e245ab35bf7" +
                                  "3ba2615707feea295709559b3de903fb95a93" +
                                  "3d2729967c3184a97d7b84c7547cd87e435b5" +
                                  "6bdf8621bcb62b59c00c88bd83aa62c4fcdd4" +
                                  "712da72eec2533dc00f8529c3a0bbb4103282" +
                                  "f0d894d5f34e9f0103c473dce9f4b457a5dee" +
                                  "fd8f920d8681ed6dfcb0a81e96bd9b176525a" +
                                  "26e0b3")]
    
    public class MyClass {
       // code...
    }
    

提交回复
热议问题