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;
}
}
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.
if(typeof(ModelName). GetProperty("Name of Property") !=
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).
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With