---
title: "Check if a property exist in a class"  
description: "Check if a property exist in a class"  
author: "Anonymous User"  
published: 2013-03-12  
updated: 2013-03-12  
canonical: https://www.mindstick.com/forum/651/check-if-a-property-exist-in-a-class  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Check if a property exist in a class

Hi [Expert](https://www.mindstick.com/articles/13120/an-expert-financial-advice-will-improve-your-finances)!

I try to know if a [property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp) exist in a [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp), I tried this :

public [static](https://www.mindstick.com/articles/12098/the-static-keyword-the-static-methods) bool HasProperty(this object obj, [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) propertyName)\
{\
return obj.GetType().GetProperty(propertyName) != null;\
}\
I don't understand why the first test [method](https://www.mindstick.com/forum/166/webservice-method) does not pass ?

[TestMethod]\
[public void](https://www.mindstick.com/interview/2715/can-we-write-static-public-void-instead-of-public-static-void) Test_HasProperty_True()\
{\
var res = typeof(MyClass).HasProperty("[Label](https://www.mindstick.com/articles/12835/print-label-tracking-how-do-these-features-make-auspost-woocommerce-plugin-better)");\
Assert.IsTrue(res);\
}

[TestMethod]\
public void Test_HasProperty_False()\
{\
var res = typeof(MyClass).HasProperty("Lab");\
Assert.IsFalse(res);\
}

Thanks in [advance](https://www.mindstick.com/blog/33258/jee-mains-and-jee-advance-exams)!

## Replies

### Reply by AVADHESH PATEL

Hi Ben!

Your method looks as following!

[public](https://www.mindstick.com/blog/11097/java-access-modifiers-the-public-and-the-private-modifiers) static bool HasProperty(this object obj, string propertyName)\
{\
return obj.GetType().GetProperty(propertyName) != null;\
}\
This adds an extension onto object - the base class of everything. When you call this extension you're passing it a Type:

var res = typeof(MyClass).HasProperty("Label");\
Your method expects an instance of a class, not a Type. Otherwise you're essentially doing

typeof(MyClass) - this gives an instanceof `System.Type`. \
Then

type.GetType() - this gives `System.Type`\
Getproperty('xxx') - whatever you provide as xxx is unlikely to be on `System.Type`\
As @PeterRitchie correctly points out, at this point your code is looking for property Label on System.Type. That property does not exist.

The solution is either

a) Provide an instance of MyClass to the extension:

var myInstance = new MyClass()\
myInstance.HasProperty("Label")\
b) Put the extension on System.Type

public static bool HasProperty(this Type obj, string propertyName)\
{\
return obj.GetProperty(propertyName) != null;\
}\
and

typeof(MyClass).HasProperty("Label");


---

Original Source: https://www.mindstick.com/forum/651/check-if-a-property-exist-in-a-class

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
