如何处理作为参数传递为方法的lambda防爆pressions - C#.NET 3.5 [英] How to Process Lambda Expressions Passed as Argument Into Method - C# .NET 3.5

查看:169
本文介绍了如何处理作为参数传递为方法的lambda防爆pressions - C#.NET 3.5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的LAMBDA EX pressions知识是有点不稳,而我可以写一个使用LAMBDA EX pressions(又名LINQ)code,我试图写我自己的方法,需要一个一些参数是类型的lambda防爆pression。

My knowledge of Lambda expressions is a bit shaky, while I can write code that uses Lambda expressions (aka LINQ), I'm trying to write my own method that takes a few arguments that are of type Lambda Expression.

背景:我试着写,从字面上任何其他对象类型的返回类型TreeItem对象的树集合的方法。我有以下至今:

Background: I'm trying to write a method that returns a Tree Collection of objects of type TreeItem from literally ANY other object type. I have the following so far:

public class TreeItem
{
    public string Id { get; set; }
    public string Text { get; set; }

    public TreeItem Parent { get; protected set; }

    public IList<TreeItem> Children
    {
        get
        {
            // Implementation that returns custom TreeItemCollection type
        }
    }

    public static IList<TreeItem> GetTreeFromObject<T>(IList<T> items,
        Expression<Func<T, string>> id,
        Expression<Func<T, string>> text,
        Expression<Func<T, IList<T>>> childProperty) where T : class
    {
        foreach (T item in items)
        {
           // Errrm!?? What do I do now?
        }

        return null;
    }
}

...它可以通过......被称为

...which can be called via...

IList<TreeItem> treeItems = TreeItem.GetTreeFromObject<Category>(
    categories, c => c.Id, c => c.Name, c => c.ChildCategories);

我可以更换防爆pressions与字符串值,而只是使用反射,但我想避免这种情况,我想使它强类型。

I could replace the Expressions with string values, and just use reflection, but I'm trying to avoid this as I want to make it strongly typed.

我这样做的原因是,我有一个接受的类型TreeItem列表的控制,而我有几十个不同类型的都在树状结构,而不愿意写的每个单独的转换方法式(试图坚持DRY原则)。

My reasons for doing this is that I have a control that accepts a List of type TreeItem, whereas I have dozens of different types that are all in a tree like structure, and don't want to write seperate conversion methods for each type (trying to adhere to the DRY principle).

我要对这个正确的方式?是否有这样做的更好的办法吧?

Am I going about this the right way? Is there a better way of doing this perhaps?

推荐答案

有没有这样的类型为拉姆达EX pression。一个lambda EX pression可以被转换成一个兼容的委托类型,或前pression树

There's no such type as "lambda expression". A lambda expression can either be converted into a compatible delegate type, or an expression tree.

您现有的方法签名使用EX pression树 - 但它不是完全清楚,它真正需要的。尝试委托的形式(有一些参数的名称更改):

Your existing method signature uses expression trees - but it's not at all clear that it really needs to. Try the delegate form (with a few parameter name changes):

public static IList<TreeItem> GetTreeFromObject<T>(IList<T> items,
    Func<T, string> idSelector,
    Func<T, string> textSelector,
    Func<T, IList<T>> childPropertySelector) where T : class

然后,你可以做这样的事情:

Then you can do something like this:

foreach (T item in items)
{
    string id = idSelector(item);
    string text = textSelector(item);
    IList<T> children = childPropertySelector(item);
    // Do whatever you need here
}

这篇关于如何处理作为参数传递为方法的lambda防爆pressions - C#.NET 3.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆