Witte

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 

这是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;
        }
posted on 2015-03-16 23:53  Witte  阅读(585)  评论(1编辑  收藏  举报