Alex Rivera | Logout

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

Asked 2008-09-30T21:03:57.567
22

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 anything using reflection. But I've seen some strange things...

Edit
Report

2 Answers

1

It MIGHT (would increase the size if I could). According to the guys on freenode, it would involved modifying the byte code, using Reflection.Emit, and handing the JIT a new set of byte code.

Not that I KNOW how ... it was just what they thought.

answered 2008-09-30T21:15:17.870
-3

Create a new class called GenericKeyValueBase

put this in it

  public class GenericKeyValueBase<TKey,TValue>
    {
        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<TCookieKey, TCookieValue> : GenericKeyValueBase<TCookieKey,TCookieValue>
    {
        public GenericCookieItem(TCookieKey KeyValue, TCookieValue ItemValue) : base(KeyValue, ItemValue)
        {
        }
    }
answered 2009-06-02T01:56:05.900

Your Answer