vendredi 12 juillet 2019

Get name of property with Reflection/StackTrace (or whatever)?

i.e. If you use this, it can trigger Object reference not set to an instance of an object:

try 
{
  Dictionary<int,int> ZZZ; 
  ZZZ[1]= ..;
}
catch(Exception e)
{    
    var stackTrace = new StackTrace(e);
    var frames = stackTrace.GetFrames();
    var method = frames[0].GetMethod(); 
    Console.WriteLine(method.Name);
}

However with StackTrace we can get the name of method, where this error happens.

But, how we can get the name of property which triggered error? In other languages like php, there is way to find (using debug_backtrace) the method which happened last time.

Is there anything similar, so it can find that last time was called i.e. set_ZZZ() (autosetter) method, or if it can directly say that ZZZ property caused issue?





Aucun commentaire:

Enregistrer un commentaire