MichaelTaylor3D
MichaelTaylor3D

Reputation: 1665

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

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;
    }

}

Upvotes: 7

Views: 18103

Answers (4)

Language Lassi
Language Lassi

Reputation: 2630

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);

Upvotes: 3

FlyingStreudel
FlyingStreudel

Reputation: 4464

You should take a look at msdn and read up about binding flags. In specific:

You must specify Instance or Static along with Public or NonPublic or no members will be returned.

Upvotes: 0

Joshua Marble
Joshua Marble

Reputation: 560

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.

Upvotes: 7

Damian Schenkelman
Damian Schenkelman

Reputation: 3535

When retrieving members, in addition to specifying instance/static you must specify Public/NonPublic:

For example, to retrieve public properties you would use:

GetProperty(propertyName,BindingFlags.Instance | BindingFlags.Public)

To retrieve all properties you must retrive both Public and NonPublic.

Upvotes: 1

Related Questions