using System.Collections.Generic;
public interface IInterface { }
public class C : IInterface { }
public class D : B, IInterface { }
public class TypeSerializer
public TypeSerializer(Type type) { Type = type; }
public Type Type { get; private set; }
public override string ToString() { return "TypeSerializer for " + Type.Name; }
private static readonly IEnumerable<TypeSerializer> TypeSerializers = new List<TypeSerializer>
new TypeSerializer(typeof(A)),
new TypeSerializer(typeof(B)),
new TypeSerializer(typeof(IInterface))
public static void Main()
var objects = new object[]
foreach (var obj in objects)
Console.WriteLine(obj.GetType().Name + " => " + FindBestTypeSerializer(obj));
private static TypeSerializer FindBestTypeSerializer(object obj)
var runtimeType = obj.GetType();
.Where(ts => ts.Type.IsAssignableFrom(runtimeType))
if (t1.IsAssignableFrom(t2))
if (t2.IsAssignableFrom(t1))
throw new Exception("Conflicting type serializers - both are interfaces");