Eric Anastas
Eric Anastas

Reputation: 22213

get_PropertyName()/set_PropertyName() vs PropertyName?

I'm using reflection on the assembly of a public API I am working with along with System.CodeDOM to generate some code that will extract information through the API.

In part of my auto-generated code I am referencing the values of a number of properties of types in the API assembly. However, I keep ending up with references to properties that don't actualy exist in my generated code. I used Type.GetProperties() which from what I understand should only return public properties.

I looked into it further and found that when I had a missing property, say called SampleProperty there were instead two methods in the class called get_SampleProperty and set_SampleProperty but no actual SampleProperty property.

What's going on here? Why does intellisense treat these methods as separate methods, but when when returned through reflection they show up as a property?

Upvotes: 1

Views: 594

Answers (1)

Hans Passant
Hans Passant

Reputation: 941257

I used PropertyInfo.GetProperties() which from what I understand should only return public properties.

That might be your first hang-up, the PropertyInfo class doesn't have a GetProperties method. The Type class does. Your question otherwise indicates that you are actually using Type.GetMethods(). Yes, that returns the get_Blah and set_Blah property accessor methods for a property. Under the hood, properties are actually implemented as methods.

Use Type.GetProperties() to reflect properties.

Upvotes: 5

Related Questions