Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search if an object has a property with a value C#

Tags:

c#

reflection

I would like to create a function where I can pass in an arbitrary object and check to see if it has a specific property with a specific value. Im attempting to do this using reflection, but reflection still confuses me a little bit. I was hoping that someone might be able to point me in the right direction.

here is the code that im trying but obviously it doesnt work:

    public static bool PropertyHasValue(object obj, string propertyName, string propertyValue)
{
    try
    {
        if(obj.GetType().GetProperty(propertyName,BindingFlags.Instance).GetValue(obj, null).ToString() == propertyValue)
        {
            Debug.Log (obj.GetType().FullName + "Has the Value" + propertyValue);
            return true;    
        }

        Debug.Log ("No property with this value");
        return false;
    }
    catch
    {
        Debug.Log ("This object doesnt have this property");
        return false;
    }

}
like image 510
MichaelTaylor3D Avatar asked Oct 02 '12 20:10

MichaelTaylor3D


People also ask

How do you check if an object contains a property?

The hasOwnProperty() method will check if an object contains a direct property and will return true or false if it exists or not. The hasOwnProperty() method will only return true for direct properties and not inherited properties from the prototype chain.

How do you check if a property is present in an object C#?

if(typeof(ModelName). GetProperty("Name of Property") !=

How do you check if a value is in an object?

You can use Object. values(): The Object. values() method returns an array of a given object's own enumerable property values, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).


2 Answers

You will want to specify more BindingFlags in the Type.GetProperty method call. You do this with a | character and the other flags, such as BindingFlags.Public. Other issues are not checking for null obj parameter or null result from your PropertyInfo.GetValue call.

To be more explicit in your method, you could write it like this and collapse down where you see fit.

public static bool PropertyHasValue(object obj, string propertyName, string propertyValue)
{
    try
    {
        if(obj != null)
        {
            PropertyInfo prop = obj.GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public);
            if(prop != null)
            {
                object val = prop.GetValue(obj,null);
                string sVal = Convert.ToString(val);
                if(sVal == propertyValue)
                {
                    Debug.Log (obj.GetType().FullName + "Has the Value" + propertyValue);
                    return true;    
                }
            }
        }

        Debug.Log ("No property with this value");
        return false;
    }
    catch
    {
        Debug.Log ("An error occurred.");
        return false;
    }
}

In my opinion you should accept propertyValue as an object and compare the objects equally, but that would exhibit a different behavior than your original sample.

like image 96
Joshua Marble Avatar answered Oct 07 '22 20:10

Joshua Marble


Its too late to answer this question here. But I was searching for the same problem and solved it in a cleaner way with LINQ and Reflection. So if you are open to LINQ. You can get it like this.

String propertyValue = "Value_to_be_compared";

Bool Flag = YourObject.GetType().GetProperties().Any(t => t.GetValue(objEmailGUID, null).ToString().Contains(propertyValue));

if(Flag)
{
  //spread love if true
}

Code will check if any of the property of you object Contains the Value_to_be_compared

If you want to match exact value then you can go for:

Bool Flag = YourObject.GetType().GetProperties().Any(t => t.GetValue(objEmailGUID, null).ToString() == propertyValue);
like image 30
Shri Avatar answered Oct 07 '22 20:10

Shri