Users Pricing

forum

Home Forums Check if a property exist in a class – MindStick

Check if a property exist in a class

Anonymous User 2675 12 Mar 2013

Hi Expert!

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

Thanks in advance!


1 Answers

Markdown for AI

A clean, structured version of this page for AI assistants and LLMs.

Open .md