Exemplo n.º 1
0
        /// <summary>
        /// DefineDerivedMethodSignature 定义动态类中方法的签名,支持泛型方法。
        /// </summary>
        public static MethodBuilder DefineDerivedMethodSignature(TypeBuilder typeBuilder, MethodInfo baseMethod)
        {
            Type[]        argTypes      = EmitHelper.GetParametersType(baseMethod);
            MethodBuilder methodBuilder = typeBuilder.DefineMethod(baseMethod.Name, baseMethod.Attributes & ~MethodAttributes.Abstract, baseMethod.ReturnType, argTypes);

            #region GenericMethod
            if (baseMethod.IsGenericMethod)
            {
                Type[]   genericParaTypes = baseMethod.GetGenericArguments();
                string[] genericParaNames = EmitHelper.GetGenericParameterNames(baseMethod);
                GenericTypeParameterBuilder[] genericTypeParameterBuilders = methodBuilder.DefineGenericParameters(genericParaNames);
                for (int i = 0; i < genericTypeParameterBuilders.Length; i++)
                {
                    genericTypeParameterBuilders[i].SetInterfaceConstraints(genericParaTypes[i].GetGenericParameterConstraints());
                }
            }
            #endregion

            return(methodBuilder);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Stind 间接存储(即存储[type类型]的对象地址)。将间接存储。不支持decimal类型
        /// </summary>
        public static void Stind(ILGenerator ilGenerator, Type type)
        {
            if (!type.IsValueType)
            {
                ilGenerator.Emit(OpCodes.Stind_Ref);
                return;
            }

            if (type.IsEnum)
            {
                Type underType = Enum.GetUnderlyingType(type);
                EmitHelper.Stind(ilGenerator, underType);
                return;
            }

            if (type == typeof(Int64))
            {
                ilGenerator.Emit(OpCodes.Stind_I8);
                return;
            }

            if (type == typeof(Int32))
            {
                ilGenerator.Emit(OpCodes.Stind_I4);
                return;
            }

            if (type == typeof(Int16))
            {
                ilGenerator.Emit(OpCodes.Stind_I2);
                return;
            }

            if (type == typeof(Byte))
            {
                ilGenerator.Emit(OpCodes.Stind_I1);
                return;
            }

            if (type == typeof(SByte))
            {
                ilGenerator.Emit(OpCodes.Stind_I1);
                return;
            }

            if (type == typeof(Boolean))
            {
                ilGenerator.Emit(OpCodes.Stind_I1);
                return;
            }

            if (type == typeof(UInt64))
            {
                ilGenerator.Emit(OpCodes.Stind_I8);
                return;
            }

            if (type == typeof(UInt32))
            {
                ilGenerator.Emit(OpCodes.Stind_I4);
                return;
            }

            if (type == typeof(UInt16))
            {
                ilGenerator.Emit(OpCodes.Stind_I2);
                return;
            }

            if (type == typeof(Single))
            {
                ilGenerator.Emit(OpCodes.Stind_R4);
                return;
            }

            if (type == typeof(Double))
            {
                ilGenerator.Emit(OpCodes.Stind_R8);
                return;
            }

            if (type == typeof(System.IntPtr))
            {
                ilGenerator.Emit(OpCodes.Stind_I4);
                return;
            }

            if (type == typeof(System.UIntPtr))
            {
                ilGenerator.Emit(OpCodes.Stind_I4);
                return;
            }

            throw new Exception(string.Format("The target type:{0} is not supported by EmitHelper.Stind_ForValueType()", type));
        }