表达式树 linq 获取参数值?

发布于 2024-07-29 22:14:39 字数 516 浏览 1 评论 0原文

AddOptional<tblObject>(x =>x.Title, objectToSend.SupplementaryData);

private static void AddOptional<TType>(Expression<Func<TType,string>> expr, Dictionary<string, string> dictionary)
{
    string propertyName;
    string propertyValue;

    Expression expression = (Expression)expr;
    while (expression.NodeType == ExpressionType.Lambda)
    {
        expression = ((LambdaExpression)expression).Body;
    }
}

在上面的代码中,我想获取属性标题的实际值,而不仅仅是属性名称,这可能吗?

AddOptional<tblObject>(x =>x.Title, objectToSend.SupplementaryData);

private static void AddOptional<TType>(Expression<Func<TType,string>> expr, Dictionary<string, string> dictionary)
{
    string propertyName;
    string propertyValue;

    Expression expression = (Expression)expr;
    while (expression.NodeType == ExpressionType.Lambda)
    {
        expression = ((LambdaExpression)expression).Body;
    }
}

In Above code i would like to get actual value of property title, not ony propert name , is it possible ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

椒妓 2024-08-05 22:14:43
private static void Main(string[] args)
{
    CompileAndGetValue<tblObject>(x => x.Title, new tblObject() { Title =  "test" });
}

private static void CompileAndGetValue<TType>(
    Expression<Func<TType, string>> expr,
    TType obj)
{
    // you can still get name here

    Func<TType, string> func = expr.Compile();
    string propretyValue = func(obj);
    Console.WriteLine(propretyValue);
}

但是,您必须意识到这可能会非常慢。 您应该衡量它在您的情况下的表现。

如果你不喜欢传递你的对象:

    private static void Main(string[] args)
    {
        var yourObject = new tblObject {Title = "test"};
        CompileAndGetValue(() => yourObject.Title);
    }


    private static void CompileAndGetValue(
        Expression<Func<string>> expr)
    {
        // you can still get name here

        var func = expr.Compile();
        string propretyValue = func();
        Console.WriteLine(propretyValue);
    }
private static void Main(string[] args)
{
    CompileAndGetValue<tblObject>(x => x.Title, new tblObject() { Title =  "test" });
}

private static void CompileAndGetValue<TType>(
    Expression<Func<TType, string>> expr,
    TType obj)
{
    // you can still get name here

    Func<TType, string> func = expr.Compile();
    string propretyValue = func(obj);
    Console.WriteLine(propretyValue);
}

However, you must be aware that this can be quite slow. You should measure how it performs in your case.

If you don't like to pass your object:

    private static void Main(string[] args)
    {
        var yourObject = new tblObject {Title = "test"};
        CompileAndGetValue(() => yourObject.Title);
    }


    private static void CompileAndGetValue(
        Expression<Func<string>> expr)
    {
        // you can still get name here

        var func = expr.Compile();
        string propretyValue = func();
        Console.WriteLine(propretyValue);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文