一聚教程网:一个值得你收藏的教程网站

热门教程

asp.net C# 反射设置未知类型字段的值

时间:2022-06-25 08:04:19 编辑:袖梨 来源:一聚教程网

只需要使用Convert.ChangeType即可

C#

var eval = new DataTable();
var objType = order.GetType();
var propertyVal = objType.GetProperty(propertyName[1]);
if (propertyVal != null)
{
    var val = propertyVal.GetValue(order, null).ToString();
    //TODO: 神奇的方法
    var result = eval.Compute(
        $"{val} {action["Expression"].Value()} {action["Value"].Value()}",
        "");
    propertyVal.SetValue(order, Convert.ChangeType(result, propertyVal.PropertyType), null);
}

想知道ChangeType是怎么实现的?其实就是封装了一堆判断hhhh
C#


public static object ChangeType(object value, TypeCode typeCode, IFormatProvider provider)
{
    if (value == null && (typeCode == TypeCode.Empty || typeCode == TypeCode.String || typeCode == TypeCode.Object))
    {
        return null;
    }
    IConvertible convertible = value as IConvertible;
    if (convertible == null)
    {
        throw new InvalidCastException(Environment.GetResourceString("InvalidCast_IConvertible"));
    }
    switch (typeCode)
    {
        case TypeCode.Empty:
        {
            throw new InvalidCastException(Environment.GetResourceString("InvalidCast_Empty"));
        }
        case TypeCode.Object:
        {
            return value;
        }
        case TypeCode.DBNull:
        {
            throw new InvalidCastException(Environment.GetResourceString("InvalidCast_DBNull"));
        }
        case TypeCode.Boolean:
        {
            return convertible.ToBoolean(provider);
        }
        case TypeCode.Char:
        {
            return convertible.ToChar(provider);
        }
        case TypeCode.SByte:
        {
            return convertible.ToSByte(provider);
        }
        case TypeCode.Byte:
        {
            return convertible.ToByte(provider);
        }
        case TypeCode.Int16:
        {
            return convertible.ToInt16(provider);
        }
        case TypeCode.UInt16:
        {
            return convertible.ToUInt16(provider);
        }
        case TypeCode.Int32:
        {
            return convertible.ToInt32(provider);
        }
        case TypeCode.UInt32:
        {
            return convertible.ToUInt32(provider);
        }
        case TypeCode.Int64:
        {
            return convertible.ToInt64(provider);
        }
        case TypeCode.UInt64:
        {
            return convertible.ToUInt64(provider);
        }
        case TypeCode.Single:
        {
            return convertible.ToSingle(provider);
        }
        case TypeCode.Double:
        {
            return convertible.ToDouble(provider);
        }
        case TypeCode.Decimal:
        {
            return convertible.ToDecimal(provider);
        }
        case TypeCode.DateTime:
        {
            return convertible.ToDateTime(provider);
        }
        case TypeCode.Object | TypeCode.DateTime:
        {
            throw new ArgumentException(Environment.GetResourceString("Arg_UnknownTypeCode"));
        }
        case TypeCode.String:
        {
            return convertible.ToString(provider);
        }
        default:
        {
            throw new ArgumentException(Environment.GetResourceString("Arg_UnknownTypeCode"));
        }
    }
}
上面的代码不知道各位朋友有没有看懂哦,做起来其实是非常的简单了就一个条件判断了。

热门栏目