mercredi 24 mai 2017

How can I force a ReflectionTypeLoadException? (C#)

I am trying to test an assembly extension method which is basically a wrapper around Assembly.GetTypes() which handles cases when a ReflectionTypeLoadException is thrown because for whatever reason one or more Types in the loaded Assembly could not be loaded.

I have tried explicitly throwing TypeLoadExceptions from the static & instance constructors, and also trying to instantiate an instance of this dummy class and calling the DoStuff() method to break it before trying to load the types, but Assembly.GetTypes() is always returning the Type and not throwing an ReflectionTypeLoadException.

How can I force this behaviour?

    public class WillNotInitiliase
    {
        public WillNotInitiliase()
        {
            BreakType();
        }

       static WillNotInitiliase()
       {
           BreakType();
       }

       public static  void DoStuff()
       {
       }

       private static void BreakType() => throw new TypeLoadException( "I can never be loaded" );
    }





Aucun commentaire:

Enregistrer un commentaire