2

I have the following abstract Class

abstract public class FooActivator<T> where T : class
{
    protected static Expression<Func<Foo, Bar<T>>> ChosenProperty

    public void Activate(T Paramater)
    {
         using (var foo = new Foo)
         {
            foo.ChosenProperty.Method(Paramater) //Obviously wrong, How do you do this?
         }
    }
}

The Idea is that I can Inherit this base class, assign a new value to ChosenProperty in order to tell Activate Which property to call the method on.

Public Class MyClassFooActivator : FooActivator<MyClass>
{
    new static Expression<Func<Foo, Bar<MyClass>>> ChosenProperty = x => x.PropertyOfTypeBarMyClass
}

and now Activate(MyClass Paramater) is ready to be called, and it will call it using

foo.PropertyOfTypeBarMyClass.Method(Paramater)

because that is the Property contained in the Expression.

Obviously I can't do foo.ChosenProperty.Method(Paramater), because ChosenProperty is a Variable of type Expression, but what would be the syntax for selecting whatever property of foo that has been selected by the Expression?

JHixson
  • 1,512
  • 2
  • 14
  • 29

1 Answers1

3

I think what you're looking for is probably something like this:

abstract class FooActivator<T> where T : class
{
    protected abstract Func<Foo, Bar<T>> ChosenProperty { get; }

    public void Activate(T param)
    {
        var foo = new Foo();
        ChosenProperty(foo).Method(param);
    }
}

class MyClassFooActivator : FooActivator<MyClass>
{
    protected override Func<Foo, Bar<MyClass>> ChosenProperty
    {
        get { return x => x.SomeBarMyClassProperty; }
    }
}
Adi Lester
  • 24,731
  • 12
  • 95
  • 110
  • Yes! This is exactly what I was looking for. Needed to Drop the `Expression` and just use `Func`. Thank you! – JHixson Oct 10 '13 at 17:02