i know this seems strange, but...

  • Thread starter Thread starter Sam Martin
  • Start date Start date
S

Sam Martin

I want to get a property's name from the property.

e.g. not this....
this.txtField.DataBindings.Add("Text", myObject,"MyFieldOrProperty");

but this....
this.txtEmail.DataBindings.Add("Text", myObject,<someway of getting name of
MyObject.MyFieldOrProperty without using literal>);

TIA

Sam Martin
 
Sam,

You need some sort of reference to the property that you want to use.
Basically, you would have to use reflection to find the property that you
want to use, and then pass the Name property of the associated PropertyInfo
instance.

Hope this helps.
 
yeah, kind of

I know I'll need to use reflection to get to it, but i need to be able to
get almost like a reference to a Property from a property.
It is possible to get from a MyClass.Property to a PropertyInfo for the
property. Without having to loop through the properties for a type and
checking the PI.Name against a literal. I don't mind doing the loop, but I
don't want to use a literal.

Any ideas Nick?

Sam


Nicholas Paldino said:
Sam,

You need some sort of reference to the property that you want to use.
Basically, you would have to use reflection to find the property that you
want to use, and then pass the Name property of the associated
PropertyInfo instance.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Sam Martin said:
I want to get a property's name from the property.

e.g. not this....
this.txtField.DataBindings.Add("Text", myObject,"MyFieldOrProperty");

but this....
this.txtEmail.DataBindings.Add("Text", myObject,<someway of getting name
of MyObject.MyFieldOrProperty without using literal>);

TIA

Sam Martin
 
Sam,

The only way I can think of doing this would be to get the call stack,
and parse the name of the method in the MethodInfo instance attached to each
StackFrame. For a property, you will have to remove the get_ and set_
prefixes.

However, I can't recommend doing this, because it would be a huge
performance killer.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Sam Martin said:
yeah, kind of

I know I'll need to use reflection to get to it, but i need to be able to
get almost like a reference to a Property from a property.
It is possible to get from a MyClass.Property to a PropertyInfo for the
property. Without having to loop through the properties for a type and
checking the PI.Name against a literal. I don't mind doing the loop, but
I don't want to use a literal.

Any ideas Nick?

Sam


Nicholas Paldino said:
Sam,

You need some sort of reference to the property that you want to use.
Basically, you would have to use reflection to find the property that you
want to use, and then pass the Name property of the associated
PropertyInfo instance.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Sam Martin said:
I want to get a property's name from the property.

e.g. not this....
this.txtField.DataBindings.Add("Text", myObject,"MyFieldOrProperty");

but this....
this.txtEmail.DataBindings.Add("Text", myObject,<someway of getting name
of MyObject.MyFieldOrProperty without using literal>);

TIA

Sam Martin
 
Thanks nick,

we've opted for keeping a constant string name for each property for each
component. a bit of an overkill, but works.

cheers

sam

Nicholas Paldino said:
Sam,

The only way I can think of doing this would be to get the call stack,
and parse the name of the method in the MethodInfo instance attached to
each StackFrame. For a property, you will have to remove the get_ and
set_ prefixes.

However, I can't recommend doing this, because it would be a huge
performance killer.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Sam Martin said:
yeah, kind of

I know I'll need to use reflection to get to it, but i need to be able to
get almost like a reference to a Property from a property.
It is possible to get from a MyClass.Property to a PropertyInfo for the
property. Without having to loop through the properties for a type and
checking the PI.Name against a literal. I don't mind doing the loop, but
I don't want to use a literal.

Any ideas Nick?

Sam


Nicholas Paldino said:
Sam,

You need some sort of reference to the property that you want to use.
Basically, you would have to use reflection to find the property that
you want to use, and then pass the Name property of the associated
PropertyInfo instance.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I want to get a property's name from the property.

e.g. not this....
this.txtField.DataBindings.Add("Text", myObject,"MyFieldOrProperty");

but this....
this.txtEmail.DataBindings.Add("Text", myObject,<someway of getting
name of MyObject.MyFieldOrProperty without using literal>);

TIA

Sam Martin
 
Back
Top