我想创建一个函数,我可以在其中传入任意对象并检查它是否具有具有特定值的特定属性。我试图使用反射来做到这一点,但反射仍然让我有点困惑。我希望有人能指出我正确的方向。
这是我尝试的代码,但显然它不起作用:
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;
}
}