如何设置属性和嵌套属性与表达特性 [英] How to set properties and nested property's properties with expression

查看:206
本文介绍了如何设置属性和嵌套属性与表达特性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我GOOGLE了propblem也搜索左右。有一吨,所有的这些(我发现)的未完成的解决方案。你能帮我请,设置一个类的属性和它的嵌套属性的属性,通过选择了一个的λ,使用反射

 公共类父
{
公共类儿童
{
公INT标识{搞定;组; }
}

公共字符串名称{;私人集; }
公众诠释号码{搞定;私人集; }
公共儿童嵌套{搞定;组; }

公共父()
{
嵌套=新的儿童();
}

公共测试仪< TValue>(表达式来; Func键< ???> FUNC,TValue值)
{
//找到属性名表达式
//设置按值
返回该财产;
}
}

和消费者,我希望能够为:

 父T =新的父(); 
t.Set< INT>(T => t.Number,6)
.SET<串>(T => t.Name,东西)
。设置< INT>(T => t.Nested.Id,25);


解决方案

这样的事情应该工作:

 公共类父
{
公共父集< TValue>(表达式来; Func键<家长,TValue>> FUNC, TValue值)
{
MemberExpression MEX = func.Body为MemberExpression;
如果(MEX == NULL)抛出新的ArgumentException();

变种PI = mex.Member为的PropertyInfo;
如果(PI == NULL)抛出新的ArgumentException();

对象目标= getTarget方法(mex.Expression);
pi.SetValue(目标,价值,NULL);
返回这一点;
}

私有对象了getTarget(表达式表达式)
{
开关(expr.NodeType)
{
情况下ExpressionType.Parameter:
返回这一点;
情况下ExpressionType.MemberAccess:
MemberExpression MEX =(MemberExpression)EXPR;
的PropertyInfo PI = mex.Member为的PropertyInfo;
如果(PI == NULL)抛出新的ArgumentException();
对象目标= getTarget方法(mex.Expression);
返回pi.GetValue(目标,NULL);
默认:
抛出新的InvalidOperationException异常();
}
}
}


I googled the propblem and also search the SO. There is a ton of solutions that all of them (that I found) are not completed. Can you help me please, to set a class's properties and its nested property's properties, choosen by a lambda, using Reflection?

public class Parent
{
    public class Child
    { 
        public int Id { get; set; }
    }

    public string Name { get; private set; }
    public int Number {get; private set; }
    public Child Nested { get; set; }

    public Parent()
    {
        Nested = new Child();
    }

    public Test Set<TValue>(Expression<Func<???> func, TValue value)
    {
        // find the property name from expression
        // set the property by value
        return this;
    }
}

and in consumer, I want be able to:

Parent t = new Parent();
t.Set<int>(t => t.Number, 6)
 .set<string>(t => t.Name, "something")
 .Set<int>(t => t.Nested.Id, 25);

解决方案

Something like this should work:

public class Parent
{
    public Parent Set<TValue>(Expression<Func<Parent, TValue>> func, TValue value)
    {
        MemberExpression mex = func.Body as MemberExpression;
        if(mex == null) throw new ArgumentException();

        var pi = mex.Member as PropertyInfo;
        if(pi == null) throw new ArgumentException();

        object target = GetTarget(mex.Expression);
        pi.SetValue(target, value, null);
        return this;
    }

    private object GetTarget(Expression expr)
    {
        switch (expr.NodeType)
        {
            case ExpressionType.Parameter:
                return this;
            case ExpressionType.MemberAccess:
                MemberExpression mex = (MemberExpression)expr;
                PropertyInfo pi = mex.Member as PropertyInfo;
                if(pi == null) throw new ArgumentException();
                object target = GetTarget(mex.Expression);
                return pi.GetValue(target, null);
            default:
                throw new InvalidOperationException();
        }
    }
}

这篇关于如何设置属性和嵌套属性与表达特性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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