<p>I'm looking for some way to effectively hide inherited members. I have a library of classes which inherit from common base classes.  Some of the more recent descendant classes inherit dependency properties which have become vestigial and can be a little confusing when using <em>IntelliSense</em> or using the classes in a visual designer.</p>

<p>These classes are all controls that are written to be compiled for either WPF or Silverlight 2.0.  I know about <code>ICustomTypeDescriptor</code> and <code>ICustomPropertyProvider</code>, but I'm pretty certain those can't be used in Silverlight.  </p>

<p>It's not as much a functional issue as a usability issue.  What should I do?</p>

<p><strong>Update</strong></p>

<p>Some of the properties that I would really like to hide come from ancestors that are not my own and because of a specific tool I'm designing for, I can't do member hiding with the <code>new</code> operator.  (I know, it's ridiculous)</p>

[{'answer_id': 1533, 'author': 'Michael Stum', 'author_id': 91, 'author_profile': 'https://Stackoverflow.com/users/91', 'pm_score': 4, 'selected': False, 'text': '<p>While you cannot prevent usage of those inherited members to my knowledge, you should be able to hide them from IntelliSense using the <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.editorbrowsableattribute.aspx" rel="noreferrer">EditorBrowsableAttribute</a>:</p>\n\n<pre><code>Using System.ComponentModel;\n\n[EditorBrowsable(EditorBrowsableState.Never)]\nprivate string MyHiddenString = "Muahahahahahahahaha";\n</code></pre>\n\n<p><em>Edit:</em> Just saw this in the documentation comments, which makes it kinda useless for this purpose:</p>\n\n<blockquote>\n  <p>There is a prominent note that states that this attribute "does not suppress members from a class in the same assembly". That is true but not complete. Actually, the attribute does not suppress members from a class in the same solution.</p>\n</blockquote>\n'}, {'answer_id': 1534, 'author': 'Karl Seguin', 'author_id': 34, 'author_profile': 'https://Stackoverflow.com/users/34', 'pm_score': 3, 'selected': False, 'text': "<p>I think you're best least hackish way is to consider composition as opposed to inheritance.</p>\n\n<p>Or, you could create an interface that has the members you want, have your derived class implement that interface, and program against the interface.</p>\n"}, {'answer_id': 1536, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 4, 'selected': False, 'text': '<p>One potential thing you can do is contain the object rather than extend from the other class.  This will give you the most flexibility in terms of exposing what you want to expose, but if you absolutely need the object to be of that type it is not the ideal solution (however you could expose the object from a getter).</p>\n\n<p>Thus:</p>\n\n<pre><code>public class MyClass : BaseClass\n{\n    // Your stuff here\n}\n</code></pre>\n\n<p>Becomes:</p>\n\n<pre><code>public class MyClass\n{\n    private BaseClass baseClass;\n\n    public void ExposeThisMethod()\n    {\n        baseClass.ExposeThisMethod();\n    }\n}\n</code></pre>\n\n<p>Or:</p>\n\n<pre><code>public class MyClass\n{\n    private BaseClass baseClass;\n\n    public BaseClass BaseClass\n    {\n        get\n        {\n            return baseClass;\n        }\n    }\n}\n</code></pre>\n'}, {'answer_id': 1556, 'author': 'caryden', 'author_id': 313, 'author_profile': 'https://Stackoverflow.com/users/313', 'pm_score': 6, 'selected': True, 'text': '<p>Override them like Michael Suggests <a href="https://stackoverflow.com/questions/1528/#1533">above</a> and to prevent folks from using the overridden (sp?) methods, mark them as obsolete:</p>\n\n<pre><code>[Obsolete("These are not supported in this class.", true)]\npublic override  void dontcallmeanymore()\n{\n}\n</code></pre>\n\n<p>If the second parm is set to true, a compiler error will be generated if anyone tries to  call that method and the string in the first parm is the message.  If parm2 is false only a compiler warning will be generated.</p>\n'}, {'answer_id': 4392227, 'author': 'Nathan Wheeler', 'author_id': 193939, 'author_profile': 'https://Stackoverflow.com/users/193939', 'pm_score': 3, 'selected': False, 'text': '<p>I know there\'s been several answers to this, and it\'s quite old now, but the simplest method to do this is just declare them as <code>new private</code>.</p>\n\n<p>Consider an example I am currently working on, where I have an API that makes available every method in a 3rd party DLL. I have to take their methods, but I want to use a .Net property, instead of a "getThisValue" and "setThisValue" method. So, I build a second class, inherit the first, make a property that uses the get and set methods, and then override the original get and set methods as private. They\'re still available to anyone wanting to build something different on them, but if they just want to use the engine I\'m building, then they\'ll be able to use properties instead of methods.</p>\n\n<p>Using the double class method gets rid of any restrictions on being unable to use the <code>new</code> declaration to hide the members. You simply can\'t use <code>override</code> if the members are marked as virtual.</p>\n\n<pre><code>public class APIClass\n{\n    private static const string DllName = "external.dll";\n\n    [DllImport(DllName)]\n    public extern unsafe uint external_setSomething(int x, uint y);\n\n    [DllImport(DllName)]\n    public extern unsafe uint external_getSomething(int x, uint* y);\n\n    public enum valueEnum\n    {\n        On = 0x01000000;\n        Off = 0x00000000;\n        OnWithOptions = 0x01010000;\n        OffWithOptions = 0x00010000;\n    }\n}\n\npublic class APIUsageClass : APIClass\n{\n    public int Identifier;\n    private APIClass m_internalInstance = new APIClass();\n\n    public valueEnum Something\n    {\n        get\n        {\n            unsafe\n            {\n                valueEnum y;\n                fixed (valueEnum* yPtr = &amp;y)\n                {\n                    m_internalInstance.external_getSomething(Identifier, yPtr);\n                }\n                return y;\n            }\n        }\n        set\n        {\n            m_internalInstance.external_setSomething(Identifier, value);\n        }\n    }\n\n    new private uint external_setSomething(int x, float y) { return 0; }\n    new private unsafe uint external_getSomething(int x, float* y) { return 0; }\n}\n</code></pre>\n\n<p>Now valueEnum is available to both classes, but only the property is visible in the APIUsageClass class. The APIClass class is still available for people who want to extend the original API or use it in a different way, and the APIUsageClass is available for those who want something more simple.</p>\n\n<p>Ultimately, what I\'ll be doing is making the APIClass internal, and only expose my inherited class.</p>\n'}, {'answer_id': 10717931, 'author': 'Pavel Jounda', 'author_id': 1271344, 'author_profile': 'https://Stackoverflow.com/users/1271344', 'pm_score': 1, 'selected': False, 'text': '<p>I tested all of the proposed solutions and they do not really hide new members.</p>\n\n<p>But this one DOES: </p>\n\n<pre><code>[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]\npublic new string MyHiddenProperty\n{ \n    get { return _myHiddenProperty; }\n}\n</code></pre>\n\n<p>But in code-behide it\'s still accessible, so add as well Obsolete Attribute</p>\n\n<pre><code>[Obsolete("This property is not supported in this class", true)]\n[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]\npublic new string MyHiddenProperty\n{ \n    get { return _myHiddenProperty; }\n}\n</code></pre>\n'}, {'answer_id': 17283870, 'author': 'Robert', 'author_id': 2164277, 'author_profile': 'https://Stackoverflow.com/users/2164277', 'pm_score': 2, 'selected': False, 'text': '<p>To fully hide and mark not to use, including intellisense which I believe is what most readers expect</p>\n<pre><code>[Obsolete(&quot;Not applicable in this class.&quot;)] \n[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]\n[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]\n</code></pre>\n'}, {'answer_id': 47255529, 'author': 'acon', 'author_id': 8930048, 'author_profile': 'https://Stackoverflow.com/users/8930048', 'pm_score': 0, 'selected': False, 'text': '<p>You can use an interface</p>\n\n<pre><code>    public static void Main()\n    {\n        NoRemoveList&lt;string&gt; testList = ListFactory&lt;string&gt;.NewList();\n\n        testList.Add(" this is ok ");\n\n        // not ok\n        //testList.RemoveAt(0);\n    }\n\n    public interface NoRemoveList&lt;T&gt;\n    {\n        T this[int index] { get; }\n        int Count { get; }\n        void Add(T item);\n    }\n\n    public class ListFactory&lt;T&gt;\n    {\n        private class HiddenList: List&lt;T&gt;, NoRemoveList&lt;T&gt;\n        {\n            // no access outside\n        }\n\n        public static NoRemoveList&lt;T&gt; NewList()\n        {\n            return new HiddenList();\n        }\n    }\n</code></pre>\n'}, {'answer_id': 58344418, 'author': 'Sam', 'author_id': 11903389, 'author_profile': 'https://Stackoverflow.com/users/11903389', 'pm_score': 1, 'selected': False, 'text': '<p>While clearly stated above that it is not possible in C# to change the access modifiers on inherited methods and properties, I overcame this issue through a sort of "fake inheritance" using implicit casting.</p>\n\n<p><strong>Example:</strong></p>\n\n<pre><code>public class A\n{\n      int var1;\n      int var2;\n\n      public A(int var1, int var2)\n      {\n            this.var1 = var1;\n            this.var2 = var2;\n      }\n      public void Method1(int i)\n      {\n            var1 = i;\n      }\n      public int Method2()\n      {\n            return var1+var2;\n      }\n}\n</code></pre>\n\n<p>Now lets say you want a <code>class B</code> to inherit from <code>class A</code>, but want to change some accessibility or even change Method1 entirely</p>\n\n<pre><code>public class B\n{\n      private A parent;\n\n      public B(int var1, int var2)\n      {\n            parent = new A(var1, var2);\n      } \n\n      int var1 \n      {\n            get {return this.parent.var1;}\n      }\n      int var2 \n      {\n            get {return this.parent.var2;}\n            set {this.parent.var2 = value;}\n      }\n\n      public Method1(int i)\n      {\n            this.parent.Method1(i*i);\n      }\n      private Method2()\n      {\n            this.parent.Method2();\n      }\n\n\n      public static implicit operator A(B b)\n      {\n            return b.parent;\n      }\n}\n</code></pre>\n\n<p>By including the implicit cast at the end, it allows us to treat <code>B</code> objects as <code>A</code>s when we need to. It can also be useful to define an implicit cast from <code>A-&gt;B</code>.</p>\n\n<p>The biggest flaw to this approach is that you need to re-write every method/property that you intend to "inherit".\nThere\'s probably even more flaws to this approach, but I like to use it as a sort of "fake inheritance". </p>\n\n<p><strong>Note:</strong></p>\n\n<p>While this allows for changing the accessibility of <code>public</code> properties, it doesn\'t solve the issue of making <code>protected</code> properties public.</p>\n'}]
['https://Stackoverflow.com/questions/1528', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/93/']