zoukankan      html  css  js  c++  java
  • 使用Immutable优化复制注意事项

    这是Orleans中对于序列化检查类型是否支持Orleans内置的高速序列化时,使用Immutable<>包装和类型声明时,有ImmutableAttribute,效果是一样的。所以无需重复的对已经加了ImmutableAttribute的类型再次调用AsImmutable()

    internal static bool IsOrleansShallowCopyable(this Type t)
            {
                if (t.IsPrimitive || t.IsEnum || t == typeof (string) || t == typeof (DateTime) || t == typeof (Decimal) ||
                    t == typeof (Immutable<>))
                    return true;
    
                if (t.GetCustomAttributes(typeof (ImmutableAttribute), false).Length > 0)
                    return true;
    
                if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof (Immutable<>))
                    return true;
    
                if (t.IsValueType && !t.IsGenericType && !t.IsGenericTypeDefinition)
                {
                    bool result;
                    lock (shallowCopyableValueTypes)
                    {
                        if (shallowCopyableValueTypes.TryGetValue(t.TypeHandle, out result))
                            return result;
                    }
                    result = t.GetFields().All(f => !(f.FieldType == t) && f.FieldType.IsOrleansShallowCopyable());
                    lock (shallowCopyableValueTypes)
                    {
                        shallowCopyableValueTypes[t.TypeHandle] = result;
                    }
                    return result;
                }
    
                return false;
            }
    关注开源 国内最早的Orleans群--174511582,欢迎大家加入
  • 相关阅读:
    python基础十一之装饰器进阶
    python基础十之装饰器
    python基础九之函数
    python基础八之文件操作
    python基础七之copy
    python基础七之集合
    python基础数据类型汇总
    python基础六之编码
    synchronized关键字的内存语义
    对于this和当前线程的一些理解
  • 原文地址:https://www.cnblogs.com/liwt/p/4343362.html
Copyright © 2011-2022 走看看