Looking for NameOf() type of functionality.

  • Thread starter Thread starter ESPNSTI
  • Start date Start date
E

ESPNSTI

Hi,

I'm looking some way to get the name of a specific property of an object.

For example instead of having to specify the property name as a literal like
this:


MyObject obj = New MyObject();
DoSomethingWithAProperty(obj, "MyProperty");


I would like to be able to look up the property name by doing somehing like
this:


MyObject obj = New MyObject();
DoSomethingWithAProperty(obj, NameOf(obj.MyProperty));

The NameOf() method (which doesn't exist) is what I'm looking for.

The main reason I don't want to use literals is to reduce bugs if a
property's name is changed.

Thanks,
Erik
 
ESPNSTI said:
Hi,

I'm looking some way to get the name of a specific property of an object.

For example instead of having to specify the property name as a literal like
this:


MyObject obj = New MyObject();
DoSomethingWithAProperty(obj, "MyProperty");


I would like to be able to look up the property name by doing somehing like
this:


MyObject obj = New MyObject();
DoSomethingWithAProperty(obj, NameOf(obj.MyProperty));

The NameOf() method (which doesn't exist) is what I'm looking for.

The main reason I don't want to use literals is to reduce bugs if a
property's name is changed.

Thanks,
Erik
ermm
Dont change the property name without fully refactoring all your code
(and anyone else's that depends on yours)?

You could do this with reflection but what would be the point?
(reflection is slow)
You would have to have some way to indicate which property you wanted
and then what?

What does your dosomethingwithaproperty method do?
Maybe an interface would be appropriate?

JB
Puzzled
 
ESPNSTI said:
For example instead of having to specify the property name as a literal
like this:

DoSomethingWithAProperty(obj, "MyProperty");

I would like to be able to look up the property name by doing somehing
like this:

DoSomethingWithAProperty(obj, NameOf(obj.MyProperty));

The NameOf() method (which doesn't exist) is what I'm looking for.

We are too (so far without success).

Everywhere in reflection where you want to access a specific member or
field, you have to identify it with a string. Some keyword, like NameOf(),
would be nice to have. I hate it when things aren't checked for errors at
compile time.

Max
 
Hi,

I don't think that that is a good idea, you should not change the interface
of a class, if so you can break any code that use it.


Cheers,
 
Back
Top