How can you inherit from a sealed class using reflection in .Net?

后端 未结 5 2037
青春惊慌失措
青春惊慌失措 2021-01-07 18:19

Before you start firing at me, I\'m NOT looking to do this, but someone in another post said it was possible. How is it possible? I\'ve never heard of inheriting from anythi

5条回答
  •  一整个雨季
    2021-01-07 19:06

    Create a new class called GenericKeyValueBase

    put this in it

      public class GenericKeyValueBase
        {
            public TKey Key;
            public TValue Value;
    
            public GenericKeyValueBase(TKey ItemKey, TValue ItemValue)
            {
                Key = ItemKey;
                Value = ItemValue;
            }
        }
    

    And inherit from that plus you can add additional extension methods for Add/Remove (AddAt and RemoveAt) to your new derived class (and make it a collection/dictionary) if you are feeling really cool.

    A simple implentation example where you would use a normal System.Collections.Generic.KeyValuePair for a base, but instead can use the above code

      class GenericCookieItem : GenericKeyValueBase
        {
            public GenericCookieItem(TCookieKey KeyValue, TCookieValue ItemValue) : base(KeyValue, ItemValue)
            {
            }
        }
    

提交回复
热议问题