コード例 #1
0
        public MSILWriter(MethodBase method, ILGenerator ilGenerator)
        {
            var parameters = method.GetParameters();

            _methodHeader = new MethodHeader
            {
                Attributes     = method.CallingConvention,
                DeclaringType  = method.DeclaringType,
                ParameterTypes = method.GetParameters()
                                 .Select <ParameterInfo, Type>(p => p.ParameterType)
                                 .ToArray <Type>(),
                ReturnType = typeof(void)
            };
            _parameters = new SimpleParameter[parameters.Length];
            int positionOffset = method.IsStatic ? 0 : 1;

            for (int i = 0; i < parameters.Length; ++i)
            {
                _parameters[i] = new SimpleParameter(
                    this,
                    parameters[i].Position + positionOffset,
                    parameters[i].ParameterType,
                    null);
            }
            _ilGenerator = ilGenerator;
        }
コード例 #2
0
        /// <summary>
        /// 初始化一个 <see cref="MethodGenerator"/> 类的实例.
        /// </summary>
        /// <param name="writer">指令写入器</param>
        private MethodGenerator(ICILWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            var header = writer.MethodHeader;

            //检查非静态方法是否传入了声明类型
            if (header.DeclaringType == null && NotStaticMethod(header))
            {
                throw new ArgumentException("必须为非静态方法定义一个声明类型(方法所在类的类型)");
            }

            //检查参数是否有null
            for (int i = 0; i < header.ParameterTypes.Length; ++i)
            {
                if (header.ParameterTypes[i] == null)
                {
                    throw new ArgumentException("writer.Header.Parameters 不能包含null值", "writer");
                }
            }

            _writer = writer;
            _header = header;
            //收集参数
            _parameters = LazyIndexer.Init <int, Parameter>(parameterIndex =>
            {
                if (NotStaticMethod(_header))
                {
                    ++parameterIndex;                          //如果不是静态方法,那么将参数索引值+1,因为第0个参数是this(也就是对象本身)
                }
                return(new Parameter(this, _writer.GetParameter(parameterIndex)));
            });
            _currentScope = new Scope(this); //创建代码范围
        }
コード例 #3
0
 /// <summary>
 /// 判断不是静态方法
 /// </summary>
 /// <param name="header"></param>
 /// <returns>true:不是静态方法,false:是静态方法</returns>
 private static bool NotStaticMethod(MethodHeader header)
 {
     return((header.Attributes & CallingConventions.HasThis) == CallingConventions.HasThis);
 }