Using Lambda expressions in PropertyInfos for Generic classes

Using Lambda expressions in PropertyInfos for Generic classes

Old forum URL: forums.lhotka.net/forums/t/8729.aspx


Devman posted on Friday, March 26, 2010

Hi,

Sorry if this is not the correct place to post this.I'm going through our BOs and changing PropertyInfo names from literal strings to using lambda expressions as shown below;

From;

public static PropertyInfo<string> NameProperty = RegisterProperty(new PropertyInfo<string>("Name", PropertyNames.Name));

To;

public static PropertyInfo<string> NameProperty = RegisterProperty<string>(x => x.Name,PropertyNames.Name);

This works fine for none generic classes, however our generic base classes contain properties too. When I try to do this in the same way i get the following compile error

"Cannot convert lambda expression to type 'System.Type' because it is not a delegate type". Is it possible to use Lamba expressions in this scenario?

Regards

David

ajj3085 replied on Friday, March 26, 2010

I don't think its possible to use lambas for this.

Devman replied on Friday, March 26, 2010

Andy

I don't think its possible to use lambas for this.

What is confusing is that the Lambda expressions are working for some generic classes, but not all.  The instances where it doesn't work, I'm only able to point to base class properites (ie x => x.CslaFieldManager), and not properties from the current object if that makes any sense. 

The expressions work in my own class---------  PBusinessBase<T> :BusinessBase<T>........

but do not work for any classes which derive from PBusinessBase<T> 

Entity<T>:PBussinessBase<T> and subsequent derived classes.

Can anyone explain this?

RockfordLhotka replied on Friday, March 26, 2010

In CSLA 3.8 the RegisterProperty() methods have overloads in the generic base classes that allow the use of lambdas. Non-generic base classes like CommandBase and CriteriaBase don't have those overloads.

In CSLA 4 all the base classes are now generic, so they all have the overloads.

However, it is important to remember that the <T> in BusinessBase<T> isn't a real type until you are creating a concrete subclass. And the RegisterProperty() overloads do type inference on T.

So generic base classes (any type other than you non-generic business class itself) can't use the lambda overload, because T will be the wrong type.

If you create MyBusinessBase<T> as a generic base class, that's great. But T will end up being some other type (Customer, Product, etc). So any properties you register in MyBusinessBase<T> would register with the wrong T.

In short, the rules for creating base classes are different from creating business classes, and you can't use the lambda overloads.

Devman replied on Monday, March 29, 2010

Thanks for the replies. There was a problem with some of my class definitions which were constricting the properties I could use in my lambda expressions.

I had the following which was only letting me use properties from MyBusinessBase instead of MyEntity.

public abstract class MyEntity<T> : MyBusinessBase<T> where T : MyBusinessBase<T>....

I've changed this to the following and now I can define all my PropertyInfos with lambda expressions.

public abstract class MyEntity<T> : MyBusinessBase<T> where T : MyEntity<T>....

 

Regards

David

Copyright (c) Marimer LLC