Get actual string name of a property

  • Thread starter Thread starter ronald.k.king
  • Start date Start date
R

ronald.k.king

I have an object, obj.

It has properties, Property1, Property2, etc.

I need to get the name of the property as a sting, in code.

For example, I could write:

sPropertyName = "Property1"

But what I want is some function where:

sPropertyName = GetPropertyNameAsString(obj.Property1).

I have been all through reflection and everything else I can think of,
but I just can not figue out a way to do this. Does anyone have any
ideas?

Regards
 
Use the System.Type class. Seehttp://msdn2.microsoft.com/en-us/library/system.type_members.aspx

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:http://www.miradyne.net














- Show quoted text -

Thanks for your reply, and I do not mean to be dense, but I do not see
how to do this with the Type class.

I could write obj.GetType.GetProperty("Property1").Name, but that
defeats the purpose. I need to be able to get the name of the
property as a string without writing the name as a string in quotes.

I can also write obj.GetType.GetProperties(), and go through the array
and get a list of all names, but not the current name I am looking
for. I tried to do something with this and compare Property1 with the
PropertyInfo objects created from names in the list, but I could not
make that work as I was just comparing Types, not the properties.

I know this may seem a bit stupid, but I want to be able to use the
type checking to control spelling. I could do this with variables or
contants, but I have to do this in many places, and a nice little
function that gave me back the name as a string would be wonderfull.

Regards
 
You can look at all the properties of an object using
GetType().GetProperties(). This returns a list of PropertyInfo objects
that come from the type itself.

You can also use TypeDescriptor.GetProperties which returns a list of
PropertyDescriptor objects. These may be affected by the current custom
type description provider for the object.

Try these...

foreach(PropertyInfo pi in myType.GetType().GetProperties())
{
Trace.WriteLine(pi.Name);
}

foreach(PropertyDescriptor pd in TypeDescriptor.GetProperties(myObject))
{
Trace.WriteLine(pd.Name);
}

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
You can look at all the properties of an object using
GetType().GetProperties(). This returns a list of PropertyInfo objects
that come from the type itself.

You can also use TypeDescriptor.GetProperties which returns a list of
PropertyDescriptor objects. These may be affected by the current custom
type description provider for the object.

Try these...

foreach(PropertyInfo pi in myType.GetType().GetProperties())
{
Trace.WriteLine(pi.Name);

}

foreach(PropertyDescriptor pd in TypeDescriptor.GetProperties(myObject))
{
Trace.WriteLine(pd.Name);

}

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consultinghttp://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Trickshttp://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQhttp://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.













- Show quoted text -

Once again, thanks for the reply, but I still seem to have a problem.

Using the methods mentioned, I see that I can get the names of all
properties in an object. But what I want is the name of a specifc
property as a string without writing the name out as text. To use the
GetType or TypeDescriptor methods and return the string name of a
specifc property, I need to provide the name as a string or the index
value. I do not have the index value and providing the name as a
string defeats my purpose.

My goal is some function where:

sPropertyName = GetPropertyNameAsString(obj.Property1)

Any other suggestions? I am beginning to thinks its not possible.

Regards
 
Any other suggestions? I am beginning to thinks its not possible.

It is indeed not possible. You can't write a function like that, you
really need a special programming language construct and compiler
support. And it isn't there in C# (but is in C++ for example).


Mattias
 
It is indeed not possible. You can't write a function like that, you
really need a special programming language construct and compiler
support. And it isn't there in C# (but is in C++ for example).

Mattias

Hi,

What's the point of such method if you do pass the name of the
property?? It's not the case where the string you'll get is different
from the property you passed.

I really can't see a reason for doing this... If you know to pass the
object and the property (statically) you will be able to know the
string value of the property (statically).

It's like wanting to know the origin of a passed object...

Anyhow, there is no way of doing this in c#...
Cheers,
Moty
 
(e-mail address removed) schreef:
For example, I could write:
sPropertyName = "Property1"

But what I want is some function where:
sPropertyName = GetPropertyNameAsString(obj.Property1).

I have been all through reflection and everything else I can think of,
but I just can not figue out a way to do this. Does anyone have any
ideas?

As already mentionned, it's not possible.


The only scenario i can imagine is something like implementing
INotifyPropertyChanged where you don't want to make spelling mistakes...
So in the setter you would simply call OnPropertyChanged (not passing
the actual propertyname)

[MethodImpl(MethodImplOptions.NoInlining)]
protected void OnPropertyChanged()
{
this.OnPropertyChanged(new
StackTrace(false).GetFrame(1).GetMethod().Name.Substring(4));
}

[MethodImpl(MethodImplOptions.NoInlining)]
protected virtual void OnPropertyChanged( string propertyName )
{
this.Fire( this.PropertyChanged, new PropertyChangedEventArgs(
propertyName ) );
}
 
(e-mail address removed) schreef:
For example, I could write:
sPropertyName = "Property1"
But what I want is some function where:
sPropertyName = GetPropertyNameAsString(obj.Property1).
I have been all through reflection and everything else I can think of,
but I just can not figue out a way to do this. Does anyone have any
ideas?

As already mentionned, it's not possible.

The only scenario i can imagine is something like implementing
INotifyPropertyChanged where you don't want to make spelling mistakes...
So in the setter you would simply call OnPropertyChanged (not passing
the actual propertyname)

[MethodImpl(MethodImplOptions.NoInlining)]
protected void OnPropertyChanged()
{
this.OnPropertyChanged(new
StackTrace(false).GetFrame(1).GetMethod().Name.Substring(4));

}

[MethodImpl(MethodImplOptions.NoInlining)]
protected virtual void OnPropertyChanged( string propertyName )
{
this.Fire( this.PropertyChanged, new PropertyChangedEventArgs(
propertyName ) );

}

You are correct the goal here is to keep from making spelling
mistakes. The reason this is important is is due to the function I am
calling. I implemented a home grown data structure and function that
sort of serializes an object and all internal sub-objects, recursively
to a data set in a manner similer to the registry or an ini file, with
name-value pairs, where the name is the name of the property. This
was easy to write because I can use the GetProperties() method and
loop through them without knowing the name.

To get the value back I need to pass in the name, and I am doing this
sort of like the registry GetSettings method where you pass in a
default value if the name-value pair is not found. This means that if
I misspell the name, I still get a value back with no error, and its
difficult to track this down. This structure is the main data saving
routine of the application and there are a LOT of GetSetting calls, so
ensuring I had the proper name would go a long way to help debug the
application.

The reason I am doing this wierd thing is bacause in a previous verion
I used pure serialization, which worked great, but for each new
version the data files are not backwards compatible, which is making
customers angery, so this method will allow for future backwards
compatibility, since if the data is not there, the default value can
be used.

I appreciate you replies and after many tries an a few others input, I
now agree this is not possible. I will just have to type carefully
(but if you read this post closely, you will see I suck at typing and
spelling).

Regards
 
Back
Top