I try to know if a property exist in a class, I tried this :
public static bool HasProperty(this object obj, string propertyName)
{
return obj.GetType().GetProperty(propertyName) != null;
}
I don't understand why the first test method does not pass ?
[TestMethod]
public void Test_HasProperty_True()
{
var res = typeof(MyClass).HasProperty("Label");
Assert.IsTrue(res);
}
[TestMethod]
public void Test_HasProperty_False()
{
var res = typeof(MyClass).HasProperty("Lab");
Assert.IsFalse(res);
}
Your method looks like this:
This adds an extension onto
object
- the base class of everything. When you call this extension you're passing it aType
:Your method expects an instance of a class, not a
Type
. Otherwise you're essentially doingThen
As @PeterRitchie correctly points out, at this point your code is looking for property
Label
onSystem.Type
. That property does not exist.The solution is either
a) Provide an instance of MyClass to the extension:
b) Put the extension on
System.Type
and
I got this error: "Type does not contain a definition for GetProperty" when tying the accepted answer.
This is what i ended up with:
I'm unsure of the context on why this was needed, so this may not return enough information for you but this is what I was able to do:
In my case I'm running through properties from a form submission and also have default values to use if the entry is left blank - so I needed to know if the there was a value to use - I prefixed all my default values in the model with Default so all I needed to do is check if there was a property that started with that.
If you are binding like I was:
This answers a different question:
If trying to figure out if an OBJECT (not class) has a property,
returns true if (but not only if) the property exists.
In my case, I was in an ASP.NET MVC Partial View and wanted to render something if either the property did not exist, or the property (boolean) was true.
helped me here.
Edit: Nowadays, it's probably smart to use the
nameof
operator instead of the stringified property name.There are 2 possibilities.
You really don't have
Label
property.You need to call appropriate GetProperty overload and pass the correct binding flags, e.g.
BindingFlags.Public | BindingFlags.Instance
If your property is not public, you will need to use
BindingFlags.NonPublic
or some other combination of flags which fits your use case. Read the referenced API docs to find the details.EDIT:
ooops, just noticed you call
GetProperty
ontypeof(MyClass)
.typeof(MyClass)
isType
which for sure has noLabel
property.