使用Immutable優化複製注意事項

 

這是Orleans中對於序列化檢查類型是否支持Orleans內置的高速序列化時,使用Immutable<>包裝和類型聲明時,有ImmutableAttribute,效果是同樣的。因此無需重複的對已經加了ImmutableAttribute的類型再次調用AsImmutable()this

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;
        }
相關文章
相關標籤/搜索