Lambdas in TypeDescriptor/TypeExtensions

Topics: Developer Forum
Coordinator
Apr 22, 2008 at 8:10 PM
Edited Apr 22, 2008 at 8: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.
Coordinator
Apr 23, 2008 at 1:35 AM
Dane

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.

Regards
Simon
Coordinator
Apr 23, 2008 at 1:44 AM
Also

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
            {
                get;
                set;
            }
        }
 
  [Test]
        public void AddRuleProgrammaticallyUsingGeneric()
        {
            TypeCache.Clear();
            Car car = new Car();
 
            //Create a PropertyValidationManager for Car
            var manager = new PropertyValidationManager(car);
 
            Assert.IsTrue(manager.IsValid);
 
 
            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);
            propertyDescriptor.Rules.Add(rangeRule);
 
            // 0 is an invalid NumberOfDoors 
            manager.ValidateAll();
            Assert.IsFalse(manager.IsValid);
 
            // 0 is an invalid NumberOfDoors 
            car.NumberOfDoors = 1;
            manager.ValidateAll();
            Assert.IsFalse(manager.IsValid);
 
            // 0 is an invalid NumberOfDoors 
            car.NumberOfDoors = 2;
            manager.ValidateAll();
            Assert.IsTrue(manager.IsValid);
 
 
            // 6 is an invalid NumberOfDoors 
            car.NumberOfDoors = 6;
            manager.ValidateAll();
            Assert.IsFalse(manager.IsValid);
 
            typeDescriptor.RemovePropertyDescriptor(propertyDescriptor);
 
 
            // 6 is now a valid NumberOfDoors because the validation rule has been removed.
            //Since there are no rules will have to use TryValidate
            manager.TryValidateAll();
            Assert.IsTrue(manager.IsValid);
            TypeCache.Clear();
        }
 
Coordinator
Apr 23, 2008 at 5: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.
Coordinator
Apr 23, 2008 at 4:13 PM
Ok, just sent over an email with more details and my proposed solution. Let me know what you think.