mardi 21 mars 2017

Is reflection giving contradiction in it's answers or have done something wrong?

I'm trying to get all types that implement a particular interface.

Apparently using types.Where(t => typeof(Interface).IsAssignableFrom(t)) was not working so I broke out the problem in to a foreach loop and examined the interfaces for each type using type.GetInterfaces().

So I wrote some code ...

var types = GetSomeTypesFromCurrentAppDomain()
    .SelectMany(a => a.GetTypes())
    .Where(t => t.Name.Contains("DataContext"));

var typesWithInterfaces = types.Select(t => new { Type = t, Interfaces = t.GetInterfaces() });
var returnTypes = new List<Type>();

foreach (var t in typesWithInterfaces)
{
    if (!t.Type.IsInterface && !t.Type.IsAbstract && t.Interfaces.Contains(typeof(IDataContext)))
    {
        returnTypes.Add(t.Type);
    }
}

Then I put a few values in the watch window ...

enter image description here

Then I sat there and scratched me head.

Any ideas?





Aucun commentaire:

Enregistrer un commentaire