mardi 14 août 2018

c# class with members using an enum is not found with reflection

We have a project referenced in our MSTest project, which includes enumertions. These enums we use in one test class (e.g. TestMSTest) for members (e.g. _EnumMember). The MSTest project has several classes. If we call this MSTest project with reflection, we get only the classes, which doesn’t use enums as members. The classes, which were found by reflection, use also the enums from referenced project, but not as members but just in test methods itself. The class TestMSTest with the enum member I got the exception, that my project dll, which includes the enums, wasn’t found.

I played along and find out, that if I use properties instead of members, the class is found with reflection.

Here is a sample code:

[TestClass]
public class TestMSTest
{
    private EnumWhichINeed _EnumProp
    {
         get { return EnumWhichINeed.HeyHo; }
    }
    //_EnumMember creates my problem:
    // public EnumWhichINeed _EnumMember = EnumWhichINeed.HeyHo;
}

other project, use reflection:

private Type[] GetTypes(Assembly assembly)
{
   Type[] types;

   try
   {
       types = assembly.GetTypes();
   }
   catch (ReflectionTypeLoadException e)
   {
       types = e.Types.Where(t => t != null).ToArray();
   }
   return types;
}

Does anyone know, why I can't use enums from referenced project as members with reflection?

Thanks, dani





Aucun commentaire:

Enregistrer un commentaire