score:3

Accepted answer

Unfortunatly I'm not aware of any other way (I also had to write such code more than once).

The one thing you can do is to make the Where in the first method a bit nicer:

private List<Type> GetListOfGenericSerializers()
{
  Type interfaceGenricType = typeof(ISerializeDeserialize<>);

  var serializers =
    from assembly in AppDomain.CurrentDomain.GetAssemblies()
    from genericType in assembly.GetTypes()
    from interfaceType in genericType.GetInterfaces()
    where genericType.IsGenericTypeDefinition &&
          interfaceType.IsGeneric &&
          interfaceType.GetGenericTypeDefinition() == interfaceGenericType
    select genericType;

  return serializers.ToList();
}

Related Articles