1

In MVC you can say:

Html.TextBoxFor(m => m.FirstName)

This means you're passing the model property as the parameter (not the value), so MVC can get metadata and so on.

I'm trying to do a similar thing in a C# WinForms project and can't work out how. Basically I have a set of bool properties in a User Control, and I'd like to enumerate them in a dictionary for easier access:

public bool ShowView { get; set; }
public bool ShowEdit { get; set; }
public bool ShowAdd { get; set; }
public bool ShowDelete { get; set; }
public bool ShowCancel { get; set; }
public bool ShowArchive { get; set; }
public bool ShowPrint { get; set; }

Somehow I'd like to define a Dictionary object with Enum Actions as the key, and the property as the value:

public Dictionary<Actions, ***Lambda magic***> ShowActionProperties = new Dictionary<Actions,***Lambda magic***> () {
    { Actions.View, () => this.ShowView }
    { Actions.Edit, () => this.ShowEdit }
    { Actions.Add, () => this.ShowAdd}
    { Actions.Delete, () => this.ShowDelete }
    { Actions.Archive, () => this.ShowArchive }
    { Actions.Cancel, () => this.ShowCancel }
    { Actions.Print, () => this.ShowPrint }
}

I need to be passing the property, not the property value, into the dictionary as they may change at runtime.

Ideas?

-Brendan

2 Answers 2

3

All your examples have no input parameters and return a bool, so you can just use:

Dictionary<Actions, Func<bool>>

You can then evaluate the lambdas to get the runtime values of the properties:

Func<bool> fn = ShowActionProperties[ Actions.View ];
bool show = fn();
Sign up to request clarification or add additional context in comments.

1 Comment

This is far easier a solution to the stated problem - +1. I still stand by the idea that everyone should learn about expression trees, but this is a "better" answer. :)
2

Ever heard of Expression Trees? Charlie Calvert's Intro on Expression Trees

Let's say you want to define a method that takes a reference to a string property; one way you could do this would be to have a method:

public string TakeAProperty(Expression<Func<string>> stringReturningExpression)
{
    Func<string> func = stringReturningExpression.Compile();
    return func();
}

Which you could then call via:

void Main()
{
    var foo = new Foo() { StringProperty = "Hello!" };
    Console.WriteLine(TakeAProperty(() => foo.StringProperty));
}

public class Foo
{
    public string StringProperty {get; set;}
}

Expression Trees let you do FAR FAR more than this, however; I heartily recommend doing some research there. :)

EDIT: another example

public Func<Foo,string> FetchAProperty(Expression<Func<Foo,string>> expression)
{
    // of course, this is the simplest use case possible
    return expression.Compile();
}

void Main()
{
    var foo = new Foo() { StringProperty = "Hello!" };
    Func<Foo,string> fetcher = FetchAProperty(f => f.StringProperty);
    Console.WriteLine(fetcher(foo));
}

More reference links:

Expression trees and lambda decomposition

A CodeProject tutorial on Expression Trees

Using Expression Trees in your API

The Amazing Bart de Smet on Expression Trees

2 Comments

Thanks for that - that's what I was really looking for! Nicholas's answer was probably good enough for this particular circumstance but I always wanted to know how to do this, cheers.
@BrendanHill Expression Trees are one of the awesomest additions to .NET, in my opinion - a huge leap towards the "code as data, data as code" concepts popularized by Lisp and others. Side benefit: groking Expression Trees dramatically increases your .NET knowledge at a low-level, as well!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.