By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,040 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

Is it possible to retrieve the variable name of a property ?

P: n/a
I have started looking at the nhibernate framework, but do not really
like the string based API which makes it impossible to use automatic
refactoring of a property name without the risk of getting problems
where the property has been refered to as a string...

Therefore I am wondering if it would be even theoretically possible to
improve such a framework with stronger typing, to avoid the usage of
strings.

For example, consider this code example from the NHibernate framework:
IList cats = sess.CreateCriteria(typeof(Cat)).AddOrder(
Order.Asc("Name") ).List();

Here is what I wish could be done instead:
IList cats = sess.CreateCriteria(typeof(Cat)).AddOrder(
Order.Asc(Cat.Name) ).List();

Obviously the above kind of syntax would not even compile unless there
would be a static "Name" property in the Cat class, but perhaps
something like this might be possible:
Cat aCat = new Cat();
IList cats = sess.CreateCriteria(typeof(Cat)).AddOrder(
Order.Asc(aCat.Name) ).List();
But the question is whether it would be at all possible to implement
the "Order.Asc" method
so that its formal parameter could be used to retrieve the string
"Name" from the actual parameter in the invocation ?

/ Tomas

Aug 7 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hello Tomas,

unfortunately you cannot have such a construct as the language
interpretes the <instance>.<propertyas the reference to the property
value not the actual property. So, you cannot retrieve the name of the
property that was passed to the method as there is no such information.

It is a Good Thing that there is such a gap as you cannot create
"property-specific" methods that will introduce complexity and bugs of
cource.

Regards,
Tasos
to*************@bloglines.com wrote:
I have started looking at the nhibernate framework, but do not really
like the string based API which makes it impossible to use automatic
refactoring of a property name without the risk of getting problems
where the property has been refered to as a string...

Therefore I am wondering if it would be even theoretically possible to
improve such a framework with stronger typing, to avoid the usage of
strings.

For example, consider this code example from the NHibernate framework:
IList cats = sess.CreateCriteria(typeof(Cat)).AddOrder(
Order.Asc("Name") ).List();

Here is what I wish could be done instead:
IList cats = sess.CreateCriteria(typeof(Cat)).AddOrder(
Order.Asc(Cat.Name) ).List();

Obviously the above kind of syntax would not even compile unless there
would be a static "Name" property in the Cat class, but perhaps
something like this might be possible:
Cat aCat = new Cat();
IList cats = sess.CreateCriteria(typeof(Cat)).AddOrder(
Order.Asc(aCat.Name) ).List();
But the question is whether it would be at all possible to implement
the "Order.Asc" method
so that its formal parameter could be used to retrieve the string
"Name" from the actual parameter in the invocation ?

/ Tomas
Aug 7 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.