What are the "get_..." methods all about?

  • Thread starter Thread starter PseudoBill
  • Start date Start date
P

PseudoBill

I'm using reflection to get all public methods for a type, and I get
all these methods starting with "get_". What's that all about? They
aren't viewable from the object browser.
 
Ahh - thanks!

Is there an elegant way to retrieve just methods, without properties?

Currently, I'm using:
MethodInfo[] oMethods =
asmType.GetMethods(BindingFlags.Public|BindingFlags.Instance|BindingFlags.Static|BindingFlags.DeclaredOnly);

Is there a binding flag or something to omit properties?
 
PseudoBill said:
Ahh - thanks!

Is there an elegant way to retrieve just methods, without properties?

Currently, I'm using:
MethodInfo[] oMethods =
asmType.GetMethods(BindingFlags.Public|BindingFlags.Instance|BindingF
lags.Static|BindingFlags.DeclaredOnly);

Is there a binding flag or something to omit properties?

I don't think there's a binding flag to do it, but from a MethodInfo
you can use IsSpecialName which will detect properties and the like.
 
Thanks Jon! That definitely works.

I must say though, that this aspect of reflection is not well thought
out by Microsoft. There should be some more intuitive way to filter
out properties from methods. I know that you can get just properties
easily (via GetProperties), but this non-intuitive way of getting
methods minus properties sucks.

Also, the fact that properties are returned as "get_X" when using
"GetMethods" is just a terrible hack - there is no "get_X" available
to anyone outside of the assembly - it's only internally regarded as
"get_X". They should be returned using the same name that they are
available to anyone coding to the assembly.
 
PseudoBill said:
Thanks Jon! That definitely works.

I must say though, that this aspect of reflection is not well thought
out by Microsoft. There should be some more intuitive way to filter
out properties from methods. I know that you can get just properties
easily (via GetProperties), but this non-intuitive way of getting
methods minus properties sucks.

Properties are just syntactic sugar C# or VB.net offer to you; in MC++ for
example, you have to code them as methods. The JIT also treats properties
like methods, so why should they get much special attention in IL/metadata?
Also, the fact that properties are returned as "get_X" when using
"GetMethods" is just a terrible hack -

It's not a hack. It's syntetic sugar. Overriden methods in C++ were
implemented similarly, using unreadable cryptic internal function names.
This kind of coding isn't uncommon at all, the compiler builders simply
chose to reuse the "method" code they already had for properties.
there is no "get_X" available
to anyone outside of the assembly - it's only internally regarded as
"get_X".

Think MC++, or any other .net language that doesn't have native support for
properties.
They should be returned using the same name that they are
available to anyone coding to the assembly.

Then how would you distinguish the getter from the setter?

Niki
 
PseudoBill,
"get_X". They should be returned using the same name that they are
available to anyone coding to the assembly.
They Are!!

J# and other languages that do not have specific support for Properties use
the get_X method.

Similar to overloaded operators, VB.NET does not support overloaded
operators, while C# does. C# is able to use the overloaded operator, while
VB.NET needs to use a special named method such as op_Addition.

Hope this helps
Jay
 
Jon Skeet said:
PseudoBill said:
Ahh - thanks!

Is there an elegant way to retrieve just methods, without properties?

Currently, I'm using:
MethodInfo[] oMethods =
asmType.GetMethods(BindingFlags.Public|BindingFlags.Instance|BindingF
lags.Static|BindingFlags.DeclaredOnly);

Is there a binding flag or something to omit properties?

I don't think there's a binding flag to do it, but from a MethodInfo
you can use IsSpecialName which will detect properties and the like.

BindingFlags.GetProperty, BindingFlags.SetProperty
Looks interesting, but I'm not sure whether they are what you want here.

jliu - www.ssw.com.au
 
Back
Top