Lambdas in TypeDescriptor/TypeExtensions

Topics: Developer Forum
Apr 22, 2008 at 9:10 PM
Edited Apr 22, 2008 at 9:47 PM
This framework looks great. I'm just now climbing the learning curve but I'm excited.

Quick question though:
I'm trying to use what looks like support for lambdas when declaring properties programmatically. What is the expected syntax to use this feature? I'm trying to do something like:

PropertyDescriptor propertyDescriptor = typeDescriptor.GetOrCreatePropertyDescriptor<MyClass>(x => x.MyProperty);

That syntax is giving me a compile error though. It looks like the generic expression is expecting a func<TResult>. From my understanding the expression should expect a func<TProperty, object> thus passing the generic type as the first argument of the function and allowing for any result. For the specific method I'm talking about it would look like this:

public PropertyDescriptor GetOrCreatePropertyDescriptor<TProperty>(Expression<Func<TProperty, object>> expression) { ... };

This change stops me from getting the compile error and gives me expected intellisense support.

Looks as though this change will force a change to the TypeExtensions and cascade throughout the source a bit since the func<TResult> structure is used a bunch. As I'm new to this framework I'm not totally confident how much of an impact the change would have, or if its even needed, so i figured I'd post here first.

Am I using the incorrect syntax? Is there another way to do this?

Thanks tons for this framework. So much better than everything I've looked at. It will save me tons of time.
Apr 23, 2008 at 2:35 AM

I am interested in what you are trying to achieve. I have not had to do this yet and hence the support for lambdas is patchy at best
If you could post up a more complete example of what you are trying to achieve that would be great.
Or, alternatively, you can email me directly.

Apr 23, 2008 at 2:44 AM

Here is my unit test for the existing "GetOrCreatePropertyDescriptor<TProperty>(Expression<Func<TProperty>> expression)" syntax.
Does this help?

BTW: not checked in yet :)
   public class Car
            //Assume for the purposes of this sample that cars can have between 2 and 5 doors.
            public int NumberOfDoors
        public void AddRuleProgrammaticallyUsingGeneric()
            Car car = new Car();
            //Create a PropertyValidationManager for Car
            var manager = new PropertyValidationManager(car);
            RangeRule<int> rangeRule = new RangeRule<int>(null, null, false, 2, 5, true, true);
            //create a PropertyDescriptor for NumberOfDoors and add a RangeRule to it.  
            TypeDescriptor typeDescriptor = TypeCache.GetType<Car>();
            PropertyDescriptor propertyDescriptor = typeDescriptor.GetOrCreatePropertyDescriptor(() => car.NumberOfDoors);
            // 0 is an invalid NumberOfDoors 
            // 0 is an invalid NumberOfDoors 
            car.NumberOfDoors = 1;
            // 0 is an invalid NumberOfDoors 
            car.NumberOfDoors = 2;
            // 6 is an invalid NumberOfDoors 
            car.NumberOfDoors = 6;
            // 6 is now a valid NumberOfDoors because the validation rule has been removed.
            //Since there are no rules will have to use TryValidate
Apr 23, 2008 at 6:38 AM
The unit test was definitley helpful in showing me the intended usage. I'll post a more complete example of what I'm talking about when I'm back at my main desktop tommorow.
Apr 23, 2008 at 5:13 PM
Ok, just sent over an email with more details and my proposed solution. Let me know what you think.