zoukankan      html  css  js  c++  java
  • 数据类型转换函数

    Background

    Two popular solutions to the problem of type conversion are to use System.Convert.ChangeType, or to obtain System.ComponentModel.TypeConverter and call its ConvertFrom method. The first method breaks when you try converting a value type T to System.Nullable<T>; the second one breaks when you try converting different numeric types, for example, float to double. These limitations appear especially frustrating, because the CLR has built-in capabilities to perform both types of conversion.

    One way of using these type casting capabilities is to build a LINQ lambda expressioncompile it into a Func<object,object>, and then use the compiled delegate every time we need to convert between two types.

    public static class TypeCast {
    // This is the method exposing the rest of the functionality
    public static object Cast(this Type type, object obj) {
    return GetConverter(type, obj)(obj);
    }
    private static readonly IDictionary&lt;PairOfTypes,Func&lt;object,object>> converters =
    new Dictionary&lt;PairOfTypes,Func&lt;object,object>>();
    private static readonly ParameterExpression convParameter =
    Expression.Parameter(typeof(object), "val");
    // This is the method with the "guts" of the implementation
    [MethodImpl(MethodImplOptions.Synchronized)]
    private static Func&lt;object,object> GetConverter(Type targetType, object val) {
    var fromType = val != null ? val.GetType() : typeof(object);
    var key = new PairOfTypes(fromType, targetType);
    Func&lt;object,object> res;
    if (converters.TryGetValue(key, out res)) {
    return res;
    }
    res = (Func&lt;object,object>)Expression.Lambda(
    Expression.Convert(
    Expression.Convert(
    Expression.Convert(
    convParameter
    , fromType
    )
    , targetType
    )
    , typeof(object)
    )
    , convParameter
    ).Compile();
    converters.Add(key, res);
    return res;
    }
    // This class provides Equals and GetHashCode
    // for a pair of System.Type objects.
    private class PairOfTypes {
    private readonly Type first;
    private readonly Type second;
    public PairOfTypes(Type first, Type second) {
    this.first = first;
    this.second = second;
    }
    public override int GetHashCode() {
    return 31*first.GetHashCode() + second.GetHashCode();
    }
    public override bool Equals(object obj) {
    if (obj == this) {
    return true;
    }
    var other = obj as PairOfTypes;
    if (other == null) {
    return false;
    }
    return first.Equals(other.first)
    && second.Equals(other.second);
    }
    }
    }

    Now, you can use the Cast method like this:

    double? x = typeof(double?).Cast(1.0);
    int y = typeof(int).Cast(1.2345);

  • 相关阅读:
    Web2.0技能评测
    [收藏]流程设计和优化原则
    [读书笔记1] 卓有成效的管理者(彼得.德鲁克)
    [读书笔记3] 卓有成效的管理者聚焦贡献
    [读书笔记2] 卓有成效的管理者管理时间
    动态生成的Web软件 应该如何设计???
    Logs
    JQuery推荐插件(200+)
    Spring AOP 实例
    《JavaScript凌厉开发Ext详解与实践》一书说了些什么
  • 原文地址:https://www.cnblogs.com/sjqq/p/8277761.html
Copyright © 2011-2022 走看看