(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