Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting value by key from Exception.Data c#

Tags:

c#

exception

I want to use the Data property to store information I want to throw in a new exception and I've figured out how I can loop through each of the DictionaryEntries with this snippet

foreach (DictionaryEntry de in ex.Data)

But what I'd prefer to do is get the value by its key name because I have different object types in that Data object. Its easier to cast them to the right kind of object if I can get the value by its key name.

Anyone know how to get the Exception.Data dictionary entries by key name?

like image 771
Bastyon Avatar asked Sep 25 '15 20:09

Bastyon


1 Answers

If you know the key it would be much more efficient to use the Dictionary for what it is good for:

var yourObj = ex.Data["your_key"];
like image 171
Crowcoder Avatar answered Oct 05 '22 21:10

Crowcoder