Source/NFX/Serialization/SerializationUtils.cs
/// <summary>
/// Create new object instance for type, calling default ctor
/// </summary>
//20150201 Started using cached lambda for .ctor, inling gives 2360 -> 2415 ops/sec in given test
Create new object instance for type, calling default ctor
Create new object instance for type calling default ctor
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
        public static object MakeNewObjectInstance(Type type)
        {
           Func<object> f;
           if (!s_CreateFuncCache.TryGetValue(type, out f))
           {
             var ctorEmpty = type.GetConstructor(BindingFlags.NonPublic|BindingFlags.Public|BindingFlags.Instance,
                                             null,
                                             Type.EmptyTypes,
                                             null);
             var skipAttr = ctorEmpty!=null ?
                              ctorEmpty.GetCustomAttributes<Slim.SlimDeserializationCtorSkipAttribute>(false).FirstOrDefault()
                              : null;
             var ctorSer = GetISerializableCtorInfo(type);
             if (ctorEmpty!=null && skipAttr==null && ctorSer==null)
               f = Expression.Lambda<Func<object>>(Expression.New(type)).Compile();
             else
               f = () => FormatterServices.GetUninitializedObject(type);
             var cache = new Dictionary<Type, Func<object>>( s_CreateFuncCache );
             cache[type] = f;
             s_CreateFuncCache = cache;
           }
           return f();
        }
[ System . Runtime . CompilerServices . MethodImpl ( System . Runtime . CompilerServices . MethodImplOptions . AggressiveInlining ) ] public static object MakeNewObjectInstance ( Type type ) { Func < object > f ; if ( ! s_CreateFuncCache . TryGetValue ( type , out f ) ) { var ctorEmpty = type . GetConstructor ( BindingFlags . NonPublic | BindingFlags . Public | BindingFlags . Instance , null , Type . EmptyTypes , null ) ; var skipAttr = ctorEmpty != null ? ctorEmpty . GetCustomAttributes < Slim . SlimDeserializationCtorSkipAttribute > ( false ) . FirstOrDefault ( ) : null ; var ctorSer = GetISerializableCtorInfo ( type ) ; if ( ctorEmpty != null && skipAttr == null && ctorSer == null ) f = Expression . Lambda < Func < object > > ( Expression . New ( type ) ) . Compile ( ) ; else f = ( ) => FormatterServices . GetUninitializedObject ( type ) ; var cache = new Dictionary < Type , Func < object > > ( s_CreateFuncCache ) ; cache [ type ] = f ; s_CreateFuncCache = cache ; } return f ( ) ; }
Create new object instance for type, calling default ctor
Create new object instance for type calling default ctor
//20150717 DKh added SlimDeserializationCtorSkipAttribute //20150715 DKh look for ISerializable .ctor //20150715 DKh, the empty .ctor SHOULD NOT be called for types that have SERIALIZABLE .ctor which is called later(after object init)
type
Type