Get name of property

P

Pascal Berger

I need to know the name of a certain known static property. I need to
have something like this:

public classe MyClass {
public static int MyProperty {
get { return 0 }
}
}

public class TestClass {
public string GetNameOfProperty(...?...) {
...?...
}

public void Foo() {
string s = GetNameOfProperty(MyClass.MyProperty);
// s should now contain the string "MyProperty"
}
}

Is there a way to achieve this?

Thanks in advance
Pascal
 
P

Pascal Berger

Hello Vadym,
Thank you for the answer. I can change the call
GetNameOfProperty(MyClass.MyProperty), this is more or less just a
placeholder for explaining my problem. I event don't know what
parameters I need in GetNameOfProperty to get this working.

The solution with the attributes doesn't work for me, since the class
which contains the property is autogenerated.

Thank you
pascal
 
T

Tom Spink

Pascal said:
Hello Vadym,
Thank you for the answer. I can change the call
GetNameOfProperty(MyClass.MyProperty), this is more or less just a
placeholder for explaining my problem. I event don't know what
parameters I need in GetNameOfProperty to get this working.

The solution with the attributes doesn't work for me, since the class
which contains the property is autogenerated.

Thank you
pascal

Hi Pascal,

It is possible to loop through all the static properties in a given class,
and extract the names, if this is what you're after?
 
P

Pascal Berger

Tom said:
It is possible to loop through all the static properties in a given class,
and extract the names, if this is what you're after?
No. I need to to know the name of one single property. In the sample I
provided above I need to know the name of MyClass.MyProperty (as a
string) from another class (in the example TestClass).

(i know this is quite a special requirement. but it makes sense in my
use case ;-)

thanks!
pascal
 
V

Vadym Stetsyak

Hello, Pascal!

PB> No. I need to to know the name of one single property. In the sample
PB> I
PB> provided above I need to know the name of MyClass.MyProperty (as a
PB> string) from another class (in the example TestClass).

PB> (i know this is quite a special requirement. but it makes sense in
PB> my
PB> use case ;-)

Maybe you will describe your use case, so we can try to give some suggestions?

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
P

Pascal Berger

Vadym said:
Maybe you will describe your use case, so we can try to give some suggestions?
I can try but it's quite complicated to understand ;-) I've a project
with some images in a resource class. The resource class has a property
for each image in the resource file.
At some point in my code I create an ImageList with all the Images from
the resource file.
At different places in the code I know need to get a specific Image from
this image list. I can do this by using IndexOfKey("MyImageName"). This
works fine as long as the names of the images in the resource file
aren't changed. I know try to at least get to the point where I get a
compiler error if I the name of the image in the resource file has changed.
Unfortunately the names of the images aren't available in the resource
class.

I hope it's a little bit clearer now what I want to achieve

Thanks
Pascal
 
P

Pascal Berger

Vadym said:
You can obtain names of all resources in the resource file by
using Assembly.GetManifestResourceNames().
Thank you for your response. But I don't see how this can help me. I
still need to hardcode the resource name on accessing it in the created
ImageList, or even worse use the Index in the array returned by
GetManifestResourceNames() which can change quite easely.

Thanks
Pascal
 
P

Pascal Berger

Vadym said:
Do you control the change of images? if so why can't you put these names into config file.
Thus if names are in the config file, in your code you'll will have to put only indeces
that correspond to config file settings.
Yes, more or less ;-) There are only developers who can change the
images. But since we're a team of several developers the chance that
someone changes an image and forgets to update the configfile is quite
high. Therefore I would prefer a solution where the compiler will spit
out an error.

Thanks
Pascal
 
J

Jon Skeet [C# MVP]

Pascal Berger said:
I can try but it's quite complicated to understand ;-) I've a project
with some images in a resource class. The resource class has a property
for each image in the resource file.
At some point in my code I create an ImageList with all the Images from
the resource file.
At different places in the code I know need to get a specific Image from
this image list. I can do this by using IndexOfKey("MyImageName"). This
works fine as long as the names of the images in the resource file
aren't changed. I know try to at least get to the point where I get a
compiler error if I the name of the image in the resource file has changed.
Unfortunately the names of the images aren't available in the resource
class.

I hope it's a little bit clearer now what I want to achieve

Here's a slightly off the wall suggestion:

1) Create an enum with the names of all the images
2) Use the enum as the parameter type for appropriate methods
3) Write unit tests to ensure that all the enum values have valid
resources associated with them

Would that get the safety you need?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top