当前位置: 首页>>代码示例>>C#>>正文


C# ILGenerator.EmitBestLdArg方法代码示例

本文整理汇总了C#中System.Reflection.Emit.ILGenerator.EmitBestLdArg方法的典型用法代码示例。如果您正苦于以下问题:C# ILGenerator.EmitBestLdArg方法的具体用法?C# ILGenerator.EmitBestLdArg怎么用?C# ILGenerator.EmitBestLdArg使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Reflection.Emit.ILGenerator的用法示例。


在下文中一共展示了ILGenerator.EmitBestLdArg方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GenerateMethod

        public virtual void GenerateMethod(PropertyInfo pi, MethodInfo mi, ILGenerator gen)
        {
            // Set default values for out parameters
            foreach (var par in mi.GetParameters().Where(p => p.IsOut))
            {
                var defaultAttr = par.GetCustomAttributes(typeof(SafeDefaultAttribute), true)
                    .Cast<SafeDefaultAttribute>().FirstOrDefault();

                var type = par.ParameterType.GetElementType();
                gen.EmitBestLdArg((ushort)(par.Position + 1));
                if (defaultAttr!=null)
                    defaultAttr.PushValueAction(gen);
                else
                    PushDefaultReturnValue(gen, type);
                gen.EmitStoreToRef(type);
            }

            // Set default value for return types
            if (mi.ReturnType!=typeof(void))
            {
                MemberInfo attrSource = mi;
                if (pi!=null)
                    attrSource = pi;

                var defaultAttr = attrSource.GetCustomAttributes(typeof(SafeDefaultAttribute), true)
                    .Cast<SafeDefaultAttribute>().FirstOrDefault();
                if (defaultAttr!=null)
                    defaultAttr.PushValueAction(gen);
                else
                    PushDefaultReturnValue(gen, mi.ReturnType);
            }
            gen.Emit(OpCodes.Ret);
        }
开发者ID:davidvmckay,项目名称:ProxyFoo,代码行数:33,代码来源:SafeNullProxySubjectCoder.cs

示例2: GenerateMethod

 public virtual void GenerateMethod(PropertyInfo pi, MethodInfo mi, ILGenerator gen)
 {
     _rsmc.PutRealSubjectOnStack(gen);
     var pars = mi.GetParameters();
     for (ushort i = 1; i <= pars.Length; ++i)
         gen.EmitBestLdArg(i);
     gen.Emit(OpCodes.Callvirt, mi);
     gen.Emit(OpCodes.Ret);
 }
开发者ID:davidvmckay,项目名称:ProxyFoo,代码行数:9,代码来源:DirectProxySubjectCoder.cs

示例3: GenerateCall

        public override void GenerateCall(IProxyModuleCoderAccess proxyModule, ILGenerator gen)
        {
            var pars = _adaptee.GetParameters();
            var targetPars = _target.GetParameters();

            var tokens = new object[pars.Length];

            // Generate in conversions
            for (ushort i = 0; i < pars.Length; ++i)
            {
                // ReSharper disable once AccessToModifiedClosure
                tokens[i] = _paramBindings[i].GenerateInConversion(() => gen.EmitBestLdArg((ushort)(i + 1)), proxyModule, gen);
            }

            gen.Emit(OpCodes.Callvirt, _target);

            // Generate out conversions
            for (ushort i = 0; i < pars.Length; ++i)
            {
                // ReSharper disable once AccessToModifiedClosure
                _paramBindings[i].GenerateOutConversion(tokens[i], () => gen.EmitBestLdArg((ushort)(i + 1)), proxyModule, gen);
            }

            _retValBinding.GenerateConversion(proxyModule, gen);
        }
开发者ID:davidvmckay,项目名称:ProxyFoo,代码行数:25,代码来源:StandardMethodBinding.cs

示例4: GenerateMethod

        public virtual void GenerateMethod(PropertyInfo pi, MethodInfo mi, ILGenerator gen)
        {
            var smeType = typeof(ISubjectMethodExists<>).MakeGenericType(_subjectType);
            var getSmeMethod = typeof(IMethodExistsProxyMeta).GetMethod("GetSubjectMethodExists").MakeGenericMethod(new[] {_subjectType});
            var dmeMethod = smeType.GetMethod("DoesMethodExist", new[] {typeof(int)});

            // Locals
            var methodExistsProxyMetaLocal = gen.DeclareLocal(typeof(IMethodExistsProxyMeta));
            var subjectMethodExistsLocal = gen.DeclareLocal(smeType);
            var makingSafeCallLocal = gen.DeclareLocal(typeof(bool));

            // Labels
            var callOnRealSubjectLabel = gen.DefineLabel();
            var makeInnerSubjectCall = gen.DefineLabel();
            var retLabel = gen.DefineLabel();

            // Check to see if the real subject supports method exists testing
            _rsmc.PutRealSubjectOnStack(gen);
            gen.Emit(OpCodes.Isinst, typeof(IMethodExistsProxyMeta));
            gen.Emit(OpCodes.Dup);
            gen.Emit(OpCodes.Stloc, methodExistsProxyMetaLocal);
            gen.Emit(OpCodes.Brfalse, callOnRealSubjectLabel);
            gen.Emit(OpCodes.Ldloc, methodExistsProxyMetaLocal);

            // It does - see if this method exists
            gen.Emit(OpCodes.Callvirt, getSmeMethod);
            gen.Emit(OpCodes.Dup);
            gen.Emit(OpCodes.Stloc, subjectMethodExistsLocal);
            gen.Emit(OpCodes.Brfalse_S, callOnRealSubjectLabel);
            gen.Emit(OpCodes.Ldloc, subjectMethodExistsLocal);
            gen.Emit(OpCodes.Ldc_I4, _methodIndex);
            gen.Emit(OpCodes.Callvirt, dmeMethod);
            gen.Emit(OpCodes.Ldc_I4_0);
            gen.Emit(OpCodes.Ceq, 0);
            gen.Emit(OpCodes.Stloc, makingSafeCallLocal);
            gen.Emit(OpCodes.Ldloc, makingSafeCallLocal);
            gen.Emit(OpCodes.Brfalse_S, callOnRealSubjectLabel);
            EmitCtorForSafeNullProxy(gen, _subjectType);
            gen.Emit(OpCodes.Br_S, makeInnerSubjectCall);

            // Prepare to call on the real subject
            gen.MarkLabel(callOnRealSubjectLabel);
            _rsmc.PutRealSubjectOnStack(gen);

            // Push arguments & call method on the inner subject (real or null proxy)
            gen.MarkLabel(makeInnerSubjectCall);
            var pars = mi.GetParameters();
            for (ushort i = 1; i <= pars.Length; ++i)
                gen.EmitBestLdArg(i);
            gen.Emit(OpCodes.Callvirt, mi);

            // If the method does not exist then the result is already safe and we can return
            gen.Emit(OpCodes.Ldloc, makingSafeCallLocal);
            gen.Emit(OpCodes.Brtrue_S, retLabel);

            // If the return type needs to be made safe then do so
            if (mi.ReturnType!=typeof(void) && mi.ReturnType.IsInterface)
            {
                var notNullLabel = gen.DefineLabel();
                gen.Emit(OpCodes.Dup); // duplicate the return value on the stack
                gen.Emit(OpCodes.Brtrue_S, notNullLabel); // check it for null
                gen.Emit(OpCodes.Pop); // We don't need it - it's always null
                EmitCtorForSafeNullProxy(gen, mi.ReturnType);
                gen.Emit(OpCodes.Br_S, retLabel);
                gen.MarkLabel(notNullLabel);
                EmitCtorForSafeDirectProxy(gen, mi.ReturnType);
            }

            // Return
            gen.MarkLabel(retLabel);
            gen.Emit(OpCodes.Ret);

            ++_methodIndex;
        }
开发者ID:davidvmckay,项目名称:ProxyFoo,代码行数:74,代码来源:SafeDirectProxySubjectCoder.cs


注:本文中的System.Reflection.Emit.ILGenerator.EmitBestLdArg方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。