如何使用FastMember设置嵌套属性值

时间:2016-10-28 12:57:34

标签: c# reflection fastmember nested-properties

当我尝试使用FastMember设置嵌套成员属性时出现异常。例如,当有这些类时

public class A
{
    public B First { get; set; }
}

public class B
{
    public string Second { get; set; }
}

我希望将实例的First.Second设置为"hello"

var b = new B{ Second = "some value here" };
var a = new A{ First = b };
var accessor = ObjectAccessor.Create(a);
accessor["First.Second"] = value; // this does not work and gives ArgumentOutOfRangeException

我无法将其拆分为[“First”] [“Second”],因为此时我不知道深度。是否有嵌套属性的神奇访问权限,还是我必须自己拆分层次结构?

3 个答案:

答案 0 :(得分:3)

我以这种方式使用扩展方法递归地解决了这个问题:

public static class FastMemberExtensions
{
    public static void AssignValueToProperty(this ObjectAccessor accessor, string propertyName, object value)
    {
        var index = propertyName.IndexOf('.');

        if (index == -1)
        {
            accessor[propertyName] = value;
        }
        else
        {
            accessor = ObjectAccessor.Create(accessor[propertyName.Substring(0, index)]);
            AssignValueToProperty(accessor, propertyName.Substring(index + 1), value);
        }
    }        
}

...这开始如下:

ObjectAccessor.Create(a).AssignValueToProperty("First.Second", "hello")

答案 1 :(得分:2)

您需要使用多个ObjectAccessor实例遍历对象图。

public static void UseFastMember()
{
    var b = new B { Second = "some value here" };
    var a = new A { First = b };
    var value = "hello";
    var a_accessor = ObjectAccessor.Create(a);
    var first = a_accessor["First"];
    var b_accessor = ObjectAccessor.Create(first);
    b_accessor["Second"] = value;
}

答案 2 :(得分:1)

向@Beachwalker致敬。但是,如果你使用TypeAccessor而不是ObjectAccessor这是一种扩展方法,我已经取得了很大的成功:

public static class TypeAccessorExtensions
{
    public static void AssignValue<T>(this TypeAccessor accessor, T t, MemberSet members, string fieldName, object fieldValue)
    {
        var index = fieldName.IndexOf('.');

        if (index == -1)
        {
            if (members.Any(m => string.Equals(m.Name, fieldName, StringComparison.OrdinalIgnoreCase)))
                accessor[t, fieldName] = fieldValue;
        }
        else
        {
            string fieldNameNested = fieldName.Substring(0, index);
            var member = members.FirstOrDefault(m => string.Equals(m.Name, fieldNameNested, StringComparison.OrdinalIgnoreCase));

            if (member != null)
            {
                var nestedAccesor = TypeAccessor.Create(member.Type);
                var tNested = accessor[t, fieldNameNested];

                if (tNested == null)
                {
                    tNested = Activator.CreateInstance(member.Type);
                    accessor[t, fieldNameNested] = tNested;
                }

                nestedAccesor.AssignValue(tNested, nestedAccesor.GetMembers(), fieldName.Substring(index + 1), fieldValue);
            }

        }
    }
}