赋值给Nullable<使用FastMember

本文关键字:使用 FastMember Nullable 赋值 | 更新日期: 2023-09-27 18:15:27

我已经成功地使用这个函数为属性和嵌套属性赋值

private static void AssignValueToProperty(ObjectAccessor accessor, object value, string propertyLambdaString)
{
    var index =  propertyLambdaString.IndexOf('.');
    if (index == -1)
    {
        accessor[propertyLambdaString] = value;
        // problem above: throws Exception if assigning value to Nullable<T>
    }
    else
    {
        var property = propertyLambdaString.Substring(0, index);
        accessor = ObjectAccessor.Create(accessor[property]);
        AssignValueToProperty(accessor, value, propertyLambdaString.Substring(index + 1));
    }
}
然而,赋值会抛出InvalidCastException异常。如何分配空值,而不是使用FastMember?例如
public class A
{
  public double? SomeValue {get; set;}
}
...
var a = new A();
var accessor = ObjectAccessor.Create(a);
accessor["SomeValue"] = 100; // throws Exception, when assigning 100.0 it works???

赋值给Nullable<使用FastMember

FastMember与它的工具箱内的类型转换无关,所以这是我提出的解决方案,作为FastMember ObjectAccessor的扩展方法:

public static class FastMemberExtensions
{
    public static void AssignValueToProperty(this ObjectAccessor accessor, string propertyName, object value)
    {
        var index = propertyName.IndexOf('.');
        if (index == -1)
        {
            var targetType = Expression.Parameter(accessor.Target.GetType());
            var property = Expression.Property(targetType, propertyName);
            var type = property.Type;
            type = Nullable.GetUnderlyingType(type) ?? type;
            value = value == null ? GetDefault(type) : Convert.ChangeType(value, type);
            accessor[propertyName] = value;
        }
        else
        {
            accessor = ObjectAccessor.Create(accessor[propertyName.Substring(0, index)]);
            AssignValueToProperty(accessor, propertyName.Substring(index + 1), value);
        }
    }
    private static object GetDefault(Type type)
    {
        return type.IsValueType ? Activator.CreateInstance(type) : null;
    }
}

可以这样调用:

var accessor = ObjectAccessor.Create(t); // t is instance of SomeType
accessor.AssignValueToProperty("Nested.Property", value); // t.Nested.Property = value

FastMember不会为您转换类型。100是int字面值,但目标属性是decimal?类型。没有从整型到十进制的隐式转换?(或小数)。100.0是隐式转换为十进制的Double字面值。,因此赋值将成功。

public class A
{
    public double? SomeValue { get; set; }
}
public static class Sample
{
    public static void Go()
    {
        var a = new A();
        var accessor = ObjectAccessor.Create(a);
        accessor["SomeValue"] = 100.0; // succeeds
        accessor["SomeValue"] = 100M; // succeeds
        accessor["SomeValue"] = null; // succeeds
        accessor["SomeValue"] = 100; // throws, can't convert from int to decimal?
    }
}

如果没有隐式转换,则必须在代码中执行必要的转换。

隐式转换:https://msdn.microsoft.com/en-us/library/y5b434w4.aspx

显式转换:https://msdn.microsoft.com/en-us/library/yht2cx7b.aspx