如何判断class是否继承Dictionary<,>

How to determine if class inherits Dictionary<,>

我有

public class SerializableDictionary<TKey, TValue>
    : Dictionary<TKey, TValue>, IXmlSerializable

我想检查一个对象是否是 SerializeableDictionary(任何泛型类型)。

为此我尝试了:

type == typeof(SerializableDictionary<,>)
or type.isSubclass()
or typeof(SerializableDictionary<,>).isAssigneableFrom(type)

没有任何效果。 我如何判断类型是 SerializableDictionary 还是任何类型?

谢谢!

var obj = new List<int>(); // new SerializableDictionary<string, int>();
var type = obj.GetType();

var dictType = typeof(SerializableDictionary<,>);
bool b = type.IsGenericType && 
         dictType.GetGenericArguments().Length == type.GetGenericArguments().Length &&
         type == dictType.MakeGenericType(type.GetGenericArguments());

我可能会创建一个接口 ISerializableDictionary 并让 SerializableDictionary<TKey, TValue> 继承它。

public interface ISerializableDictionary : IDictionary
{
}

public class SerializableDictionary<TKey, TValue>
    : Dictionary<TKey, TValue>, IXmlSerializable, ISerializableDictionary

然后:

var res = dic is ISerializableDictionary;