コード例 #1
0
        /// <summary>
        /// Constructs a new instance of UltimaPacketDefinition.
        /// </summary>
        /// <param name="type">Packet type.</param>
        /// <param name="attribute">Ultima packet attribute.</param>
        public UltimaPacketDefinition( Type type, UltimaPacketAttribute attribute )
            : base(type)
        {
            _Attribute = attribute;
            _IsDefault = attribute == null;

            // Construct constructor delegate
            ConstructorInfo constructor = type.GetConstructor( new Type[] { } );

            if ( constructor == null )
                throw new SpyException( "Type '{0}' does not have a constructor with no parameters", type );

            DynamicMethod dynamicMethod = new DynamicMethod( "CreateInstance", type, null );
            ILGenerator generator = dynamicMethod.GetILGenerator();
            generator.Emit( OpCodes.Newobj, constructor );
            generator.Emit( OpCodes.Ret );

            _Constructor = (UltimaPacketConstructor) dynamicMethod.CreateDelegate( typeof( UltimaPacketConstructor ) );
        }
コード例 #2
0
        /// <summary>
        /// Constructs a new instance of UltimaPacketDefinition.
        /// </summary>
        /// <param name="type">Packet type.</param>
        /// <param name="attribute">Ultima packet attribute.</param>
        public UltimaPacketDefinition(Type type, UltimaPacketAttribute attribute) : base(type)
        {
            _Attribute = attribute;
            _IsDefault = attribute == null;

            // Construct constructor delegate
            ConstructorInfo constructor = type.GetConstructor(new Type[] { });

            if (constructor == null)
            {
                throw new SpyException("Type '{0}' does not have a constructor with no parameters", type);
            }

            DynamicMethod dynamicMethod = new DynamicMethod("CreateInstance", type, null);
            ILGenerator   generator     = dynamicMethod.GetILGenerator();

            generator.Emit(OpCodes.Newobj, constructor);
            generator.Emit(OpCodes.Ret);

            _Constructor = (UltimaPacketConstructor)dynamicMethod.CreateDelegate(typeof(UltimaPacketConstructor));
        }