EmitCallConstructor() public method

public EmitCallConstructor ( ConstructorInfo constructor ) : void
constructor System.Reflection.ConstructorInfo
return void
示例#1
0
		public static void EmitConstruction( TracingILGenerator il, LocalBuilder target, Action<TracingILGenerator> initialCountLoadingEmitter )
		{
			Contract.Requires( il != null );
			Contract.Requires( target != null );
			Contract.Requires( initialCountLoadingEmitter != null );

			// TODO: For collection, supports .ctor(IEnumerable<> other)

			if ( target.LocalType.IsArray )
			{
				initialCountLoadingEmitter( il );
				il.EmitNewarr( target.LocalType.GetElementType() );
				il.EmitAnyStloc( target );
				return;
			}

			ConstructorInfo ctor = target.LocalType.GetConstructor( _ctor_Int32_ParameterTypes );
			if ( ctor != null && initialCountLoadingEmitter != null && typeof( IEnumerable ).IsAssignableFrom( target.LocalType ) )
			{
				if ( target.LocalType.IsValueType )
				{
					// Same as general method call
					var capacity = il.DeclareLocal( typeof( int ), "capacity" );
					initialCountLoadingEmitter( il );
					il.EmitAnyStloc( capacity );
					il.EmitAnyLdloca( target );
					il.EmitAnyLdloc( capacity );
					il.EmitCallConstructor( ctor );
				}
				else
				{
					initialCountLoadingEmitter( il );
					il.EmitNewobj( ctor );
					il.EmitAnyStloc( target );
				}
				return;
			}

			if ( target.LocalType.IsValueType )
			{
				// ValueType instance has been initialized by the runtime.
				return;
			}

			ctor = target.LocalType.GetConstructor( Type.EmptyTypes );
			if ( ctor == null )
			{
				throw SerializationExceptions.NewTargetDoesNotHavePublicDefaultConstructorNorInitialCapacity( target.LocalType );
			}

			il.EmitNewobj( ctor );
			il.EmitAnyStloc( target );
		}
		private void EmitMethodEnumConstructor( Type baseType, TracingILGenerator il )
		{
			/*
			 *	.ctor( SerializationContext c, EnumSerializerMethod method ) 
			 *	  : base( c, method )
			 *	{
			 *	}
			 */
			// : base( c, method )
			il.EmitLdarg_0();
			il.EmitLdarg_1();
			il.EmitLdarg_2();

			il.EmitCallConstructor(
				baseType.GetRuntimeConstructor( ContextAndEnumSerializationMethodConstructorParameterTypes )
			);

			il.EmitRet();
		}
		private void Invoke( TracingILGenerator il )
		{
			ConstructorInfo asConsctructor;
			if ( ( asConsctructor = this._method as ConstructorInfo ) != null )
			{
				if ( asConsctructor.DeclaringType.GetIsValueType() )
				{
					this._target.LoadValue( il, true );
					foreach ( var argument in this._arguments )
					{
						argument.LoadValue( il, false );
					}

					il.EmitCallConstructor( asConsctructor );

					// For compatibility to ref type.
					this._target.LoadValue( il, false );
				}
				else
				{
					foreach ( var argument in this._arguments )
					{
						argument.LoadValue( il, false );
					}

					il.EmitNewobj( asConsctructor );
				}
			}
			else
			{
				// method
				if ( !this._method.IsStatic )
				{
					this._target.LoadValue( il, this._target.ContextType.ResolveRuntimeType().GetIsValueType() );
				}

				foreach ( var argument in this._arguments )
				{
					argument.LoadValue( il, false );
				}

				if ( this._method.IsStatic || this._target.ContextType.ResolveRuntimeType().GetIsValueType() )
				{
					il.EmitCall( this._method as MethodInfo );
				}
				else if ( this._interface != null )
				{
					// Explicit interface impl
					il.EmitCallvirt(
						this._interface.GetRuntimeMethod(
							this._method.Name.Substring( this._method.Name.LastIndexOf( '.' ) + 1 ),
							this._method.GetParameterTypes()
						)
					);
				}
				else
				{
					il.EmitCallvirt( this._method as MethodInfo );
				}
			}
		}
		private void EmitDefaultEnumConstructor( ConstructorBuilder methodConstructor, TracingILGenerator il )
		{
			/*
			 *	.ctor( SerializationContext c ) 
			 *	  : this( c, DEFAULT_METHOD )
			 *	{
			 *	}
			 */
			// : this( c, DEFAULT_METHOD )
			il.EmitLdarg_0();
			il.EmitLdarg_1();
			il.EmitAnyLdc_I4( ( int )this._defaultEnumSerializationMethod );

			il.EmitCallConstructor( methodConstructor );

			il.EmitRet();
		}
示例#5
0
		private void Invoke( TracingILGenerator il )
		{
			ConstructorInfo asConsctructor;
			if ( ( asConsctructor = this._method as ConstructorInfo ) != null )
			{
				if ( asConsctructor.DeclaringType.GetIsValueType() )
				{
					this._target.LoadValue( il, true );
					foreach ( var argument in this._arguments )
					{
						argument.LoadValue( il, false );
					}

					il.EmitCallConstructor( asConsctructor );

					// For compatibility to ref type.
					this._target.LoadValue( il, false );
				}
				else
				{
					foreach ( var argument in this._arguments )
					{
						argument.LoadValue( il, false );
					}

					il.EmitNewobj( asConsctructor );
				}
			}
			else
			{
				// method
				if ( !this._method.IsStatic )
				{
					this._target.LoadValue( il, this._target.ContextType.GetIsValueType() );
				}

				foreach ( var argument in this._arguments )
				{
					argument.LoadValue( il, false );
				}

				if ( this._method.IsStatic || this._target.ContextType.GetIsValueType() )
				{
					il.EmitCall( this._method as MethodInfo );
				}
				else
				{
					il.EmitCallvirt( this._method as MethodInfo );
				}
			}
		}