mercredi 10 janvier 2018

Assembly.GetTypes() also gets methods? [duplicate]

This question already has an answer here:

I am trying to unit test a data access layer where each entity is expected to implement an interface that includes Save() and Delete() methods. I would like to loop through each entity model and perform various tests, which seems simple enough using reflection:

[TestMethod]
public void AllDalModelsImplementIMyDALObject()
{
    Assembly dalAssm = typeof(MyDAL.DataModels.MyEntity1).Assembly;
    foreach (Type t in dalAssm.GetTypes().Where(t => t.IsClass && t.Namespace == "MyDAL.DataModels").ToList())
    {
        // Do stuff here
        TestContext.WriteLine(t.Name + " " + t.Namespace);
    }
}

But when I look at the output, in addition to all of the things that seem like Types that "are classes", I am also seeing debug lines written for each implementation of my Save and Delete methods:

TestContext Messages:
MyEntity1 MyDAL.DataModels
MyEntity2 MyDAL.DataModels
MyEntity3 MyDAL.DataModels
MyEntity4 MyDAL.DataModels
<Save>d__8 MyDAL.DataModels   // what are these and how do I filter them out?
<Delete>d__9 MyDAL.DataModels
<Delete>d__8 MyDAL.DataModels
<Save>d__9 MyDAL.DataModels
<Save>d__8 MyDAL.DataModels
<Delete>d__9 MyDAL.DataModels
<Save>d__8 MyDAL.DataModels
<Delete>d__9 MyDAL.DataModels

So my question is what are these? Why is GetTypes() returning them, and why isn't t => t.IsClass filtering them out? I could filter them by seeing if the name starts with <, but is there a better way?





Aucun commentaire:

Enregistrer un commentaire