473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Get name of property

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
Oct 5 '06 #1
8 1615
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
Oct 5 '06 #2
Pascal Berger wrote:
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?

--
Hope this helps,
Tom Spink

Google first, ask later.
Oct 5 '06 #3
Tom Spink wrote:
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
Oct 5 '06 #4
Hello, Pascal!

PBTom Spink wrote:
>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?
PBNo. I need to to know the name of one single property. In the sample
PBI
PBprovided above I need to know the name of MyClass.MyProperty (as a
PBstring) from another class (in the example TestClass).

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

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

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Oct 5 '06 #5
Vadym Stetsyak wrote:
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
Oct 5 '06 #6
Vadym Stetsyak wrote:
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
Oct 6 '06 #7
Vadym Stetsyak wrote:
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
Oct 6 '06 #8
Pascal Berger <pa***********@zeitag.chwrote:
Vadym Stetsyak wrote:
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
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 6 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Altman | last post by:
Ok I have a control that is inherited from another class. In the child I put msgbox(me.name) in the load event. What always pops up is the name of the parent class and not the name of the...
7
by: JohnR | last post by:
I have a user defined class that contains about 20 private variables with their associated properties and get-sets. I have named the properties with very user readable names. I would like to...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
11
by: Alexander Walker | last post by:
Hello I would like to write a method that allows me to pass a reference to an instance of a class, the name of a property of that class and a value to set that property to, the method would then...
12
by: Eric | last post by:
I have a custom component that can be dragged from the ToolBox onto a Windows Form (which means I can't modify it's constructor to add a name parameter). VS assigns it an instance name, but the...
3
by: TomislaW | last post by:
I am sending word document with e-mail from asp.net 2.0 application. I read doc file like this:FileStream fs = System.IO.File.Open(docPath,FileMode.Open,FileAccess.Read,FileShare.Read); Then...
7
by: Nemisis | last post by:
Hi everyone, Can anyone tell me if it is possible to pass in a property of an object into a sub, and within that sub, find out the name of the item that was passed along with the property name??...
12
by: -Lost | last post by:
What in the world is functionName.name good for? That is: function functionName() { return functionName.name; } I mean, I already had to write out the function's name so it seems that it is...
10
by: Rob | last post by:
I am reading a book that says that the "name" property can be altered only at design time and cannot be modified at runtime. Please explain this given the code below... If you click Button3......
3
by: Peter Gast | last post by:
Hi, I need as a parameter for a control the names of my properties as a string. How can I get the name of the property as a string during runtime Example: Private _MyVar As Double Public...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.