C # debug - looking for a specific value without knowing what property it has

My question is this: if I know that the property of the object has the value "example", how can I find out what this property is without checking all the possible properties of the object when debugging?

I think I'm a little obscure. For example, I have an ImagePart object. When I debug, I want to see the value of TargetName. To do this, I must move from the mouse to the object, and then to non-public members. But, if the meaning that I want to see is much deeper, it is difficult for me to find it.

+4
source share
3 answers

If I understand correctly, you have an object with a lot of properties, then you can make a method in this class that will "scan" all properties using C # reflection.

Create such a method in the class of the object you want to parse:

string PropertyThatHasCertainValue(object Value) { Type myType = this.GetType(); while(myType != typeof(object)) { foreach (PropertyInfo property_info in myType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { if (object.Equals(property_info.GetValue(this, null), Value)) { return property_info.Name; } } myType = myType.BaseType; } return "No property has this value"; } 

Then in the watch add the following hours:

MyObjectInstance.PropertyThatHasCertainValue(ValueYouAreLookingFor)

Please note that you can use something else, but object as a parameter to simplify input in hours, but VS watch Window you can easily enter not only numbers and strings, but also enumerations. Visual Studio watches are extremely powerful, they will almost always correctly evaluate expression.

I added a while loop to jump recursively through all parents. BindingFlags.NonPublic will return all private and protected methods of the class, but not private methods of the base classes. Moving through all the base classes until it hits the object will solve this.

+4
source

With VS 2010 you can bind a property. Therefore, the next time you click on a debug point, the corresponding value will be automatically highlighted. For more: http://weblogs.asp.net/pawanmishra/archive/2009/12/26/another-vs-2010-feature-pin-up.aspx

-1
source

A similar question is asked here . Please see my answer : the search function I talked about works for property values ​​the same way it does for property names.

-1
source

Source: https://habr.com/ru/post/1379950/


All Articles