Hiding inherited members

前端 未结 9 1891
北海茫月
北海茫月 2020-12-03 09:52

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 clas

相关标签:
9条回答
  • 2020-12-03 10:19

    You can use an interface

        public static void Main()
        {
            NoRemoveList<string> testList = ListFactory<string>.NewList();
    
            testList.Add(" this is ok ");
    
            // not ok
            //testList.RemoveAt(0);
        }
    
        public interface NoRemoveList<T>
        {
            T this[int index] { get; }
            int Count { get; }
            void Add(T item);
        }
    
        public class ListFactory<T>
        {
            private class HiddenList: List<T>, NoRemoveList<T>
            {
                // no access outside
            }
    
            public static NoRemoveList<T> NewList()
            {
                return new HiddenList();
            }
        }
    
    0 讨论(0)
  • 2020-12-03 10:24

    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).

    Thus:

    public class MyClass : BaseClass
    {
        // Your stuff here
    }
    

    Becomes:

    public class MyClass
    {
        private BaseClass baseClass;
    
        public void ExposeThisMethod()
        {
            baseClass.ExposeThisMethod();
        }
    }
    

    Or:

    public class MyClass
    {
        private BaseClass baseClass;
    
        public BaseClass BaseClass
        {
            get
            {
                return baseClass;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-03 10:27

    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.

    Example:

    public class A
    {
          int var1;
          int var2;
    
          public A(int var1, int var2)
          {
                this.var1 = var1;
                this.var2 = var2;
          }
          public void Method1(int i)
          {
                var1 = i;
          }
          public int Method2()
          {
                return var1+var2;
          }
    }
    

    Now lets say you want a class B to inherit from class A, but want to change some accessibility or even change Method1 entirely

    public class B
    {
          private A parent;
    
          public B(int var1, int var2)
          {
                parent = new A(var1, var2);
          } 
    
          int var1 
          {
                get {return this.parent.var1;}
          }
          int var2 
          {
                get {return this.parent.var2;}
                set {this.parent.var2 = value;}
          }
    
          public Method1(int i)
          {
                this.parent.Method1(i*i);
          }
          private Method2()
          {
                this.parent.Method2();
          }
    
    
          public static implicit operator A(B b)
          {
                return b.parent;
          }
    }
    

    By including the implicit cast at the end, it allows us to treat B objects as As when we need to. It can also be useful to define an implicit cast from A->B.

    The biggest flaw to this approach is that you need to re-write every method/property that you intend to "inherit". There's probably even more flaws to this approach, but I like to use it as a sort of "fake inheritance".

    Note:

    While this allows for changing the accessibility of public properties, it doesn't solve the issue of making protected properties public.

    0 讨论(0)
提交回复
热议问题