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

  • Thread starter tomjbr.32022025
  • Start date
T

tomjbr.32022025

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
 
T

Tasos Vogiatzoglou

Hello Tomas,

unfortunately you cannot have such a construct as the language
interpretes the <instance>.<property> as 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top