mercredi 7 juillet 2021

mono framework reflection invoke throws OverFlowException

In dotnet framework or mono framework, invoke a method with reflection, the exception during the invoke would be wrapped with a TargetInvocationException. But I find that the OverFlowException shows different behavior in mono, it throws out directly without any wrapping, is that a bug of mono framework?

the sample code:

public class Demo
{
   private void Test()
   {
      throw new OverflowException();
   }
}

class Program
{
   static void Main(string[] args)
   {
       Demo demo = new Demo();
       MethodInfo method = typeof(Demo).GetMethod("Test",BindingFlags.Instance | BindingFlags.NonPublick);
       
       try
       {
          method.Invoke(demo,new object[0]);
       }
       catch(OverflowException)
       {
          //for mono framework, throws OverflowException
       }
       cathc(TargetInvocationException)
       {
          //for dotnet framework, throws TargetInvocationException
       }
   }
}

the same code, executed differently in mono and dotnet, and only OverflowException shows this difference, can anybody explain why?





Aucun commentaire:

Enregistrer un commentaire