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


C# PropertyBuilder.GetSetMethod方法代码示例

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


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

示例1: OnGeneratePropertyMethods

        protected override void OnGeneratePropertyMethods(PropertyInfo targetProp,
                                                          PropertyDefinition proxyProp,
                                                          TypeReference proxyBaseType,
                                                          TypeReference proxyTargetType,
                                                          TypeReference rootProxyTargetType)
        {
            var propWrapperTypeRef = this.propertyWrapperType.MakeGenericType(proxyTargetType, proxyProp.PropertyType);
            var proxyType = (TypeDefinition)proxyProp.DeclaringType;
            if (proxyType == null)
                throw new InvalidOperationException($"{proxyProp} has no declaring type.");

            var propWrapperCtor = propWrapperTypeRef.GetConstructor(typeof(string));
            var propertyWrapperField = proxyType.DefineField("_pwrap_" + targetProp.Name,
                                                             propWrapperTypeRef,
                                                             FieldAttributes.Private | FieldAttributes.Static);

            this.initPropertyWrapperIlAction.Add(il =>
            {
                il.Emit(OpCodes.Ldstr, targetProp.Name);
                il.Emit(OpCodes.Newobj, propWrapperCtor);
                il.Emit(OpCodes.Stsfld, propertyWrapperField);
            });

            /*
            var initPropertyWrappersMethod = GetInitPropertyWrappersMethod(proxyProp);

            var initIl = initPropertyWrappersMethod.Body.GetILProcessor();
            var lastInstruction = initPropertyWrappersMethod.Body.Instructions.Last();
            if (lastInstruction.OpCode != OpCodes.Ret)
                throw new InvalidOperationException("Expected to find ret instruction as last instruction");

            initIl.InsertBefore(lastInstruction, Instruction.Create(OpCodes.Ldstr, targetProp.Name));
            initIl.InsertBefore(lastInstruction, Instruction.Create(OpCodes.Newobj, propWrapperCtor));
            initIl.InsertBefore(lastInstruction, Instruction.Create(OpCodes.Stsfld, propertyWrapperField));
            */

            var baseDef = proxyBaseType;
            var proxyOnGetMethod = baseDef.GetGenericInstanceMethod("OnGet", proxyTargetType, proxyProp.PropertyType);
            var proxyOnGetMethodInstance = proxyOnGetMethod.MakeGenericMethod(proxyTargetType, proxyProp.PropertyType);
            var proxyOnSetMethod = baseDef.GetGenericInstanceMethod("OnSet", proxyTargetType, proxyProp.PropertyType);
            var proxyOnSetMethodInstance = proxyOnSetMethod.MakeGenericMethod(proxyTargetType, proxyProp.PropertyType);

            var getMethod = (MethodDefinition)proxyProp.GetGetMethod(true);
            if (getMethod != null)
            {
                var getIl = getMethod.GetILGenerator();

                getIl.Emit(OpCodes.Ldarg_0);
                getIl.Emit(OpCodes.Ldsfld, propertyWrapperField);
                getIl.Emit(OpCodes.Callvirt, proxyOnGetMethodInstance);
                getIl.Emit(OpCodes.Ret);
            }

            var setMethod = (MethodDefinition)proxyProp.GetSetMethod(true);
            if (setMethod != null)
            {
                var setIl = setMethod.GetILGenerator();
                setIl.Emit(OpCodes.Ldarg_0);
                setIl.Emit(OpCodes.Ldsfld, propertyWrapperField);
                setIl.Emit(OpCodes.Ldarg_1);
                setIl.Emit(OpCodes.Callvirt, proxyOnSetMethodInstance);
                setIl.Emit(OpCodes.Ret);
            }
        }
开发者ID:Pomona,项目名称:Pomona,代码行数:64,代码来源:WrappedPropertyProxyBuilder.cs

示例2: EmitFieldExportStubs

		public void EmitFieldExportStubs(PhpField/*!*/ field, PropertyBuilder/*!*/ property)
		{
			Debug.Assert(field != null && property != null);

			MethodBuilder getter = (MethodBuilder)property.GetGetMethod(true);
			MethodBuilder setter = (MethodBuilder)property.GetSetMethod(true);

			// emit getter:
			if (getter != null) EmitFieldExportGetter(field, property, getter);

			// emit setter:
			if (setter != null) EmitFieldExportSetter(field, property, setter);
		}
开发者ID:dw4dev,项目名称:Phalanger,代码行数:13,代码来源:CodeGenerator.cs

示例3: OnGeneratePropertyMethods

        protected override void OnGeneratePropertyMethods(
            PropertyInfo targetProp,
            PropertyDefinition proxyProp,
            TypeReference proxyBaseType,
            TypeReference proxyTargetType,
            TypeReference rootProxyTargetType)
        {
            var propWrapperTypeDef = propertyWrapperType;
            var propWrapperTypeRef = propertyWrapperType.MakeGenericType(proxyTargetType, proxyProp.PropertyType);
            var proxyType = (TypeDefinition) proxyProp.DeclaringType;

            var propWrapperCtor =
                propWrapperTypeRef.GetConstructors().First(x => x.GetParameters().Length == 1 &&
                                                                x.GetParameters()[0].ParameterType == typeof (string));

            var propertyWrapperField = proxyType.DefineField(
                "_pwrap_" + targetProp.Name, propWrapperTypeRef, FieldAttributes.Private | FieldAttributes.Static);

            initPropertyWrapperIlAction.Add(il =>
                {
                    il.Emit(OpCodes.Ldstr, targetProp.Name);
                    il.Emit(OpCodes.Newobj, propWrapperCtor);
                    il.Emit(OpCodes.Stsfld, propertyWrapperField);
                });

            /*
            var initPropertyWrappersMethod = GetInitPropertyWrappersMethod(proxyProp);

            var initIl = initPropertyWrappersMethod.Body.GetILProcessor();
            var lastInstruction = initPropertyWrappersMethod.Body.Instructions.Last();
            if (lastInstruction.OpCode != OpCodes.Ret)
                throw new InvalidOperationException("Expected to find ret instruction as last instruction");

            initIl.InsertBefore(lastInstruction, Instruction.Create(OpCodes.Ldstr, targetProp.Name));
            initIl.InsertBefore(lastInstruction, Instruction.Create(OpCodes.Newobj, propWrapperCtor));
            initIl.InsertBefore(lastInstruction, Instruction.Create(OpCodes.Stsfld, propertyWrapperField));
            */

            var baseDef = proxyBaseType;
            var proxyOnGetMethod =
                baseDef.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
                       .First(x => x.Name == "OnGet");
            if (proxyOnGetMethod.GetGenericArguments().Length != 2)
            {
                throw new InvalidOperationException(
                    "OnGet method of base class is required to have two generic parameters.");
            }

            var proxyOnGetMethodInstance = proxyOnGetMethod.MakeGenericMethod(proxyTargetType, proxyProp.PropertyType);

            var proxyOnSetMethod =
                baseDef.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
                       .First(x => x.Name == "OnSet");
            ;
            if (proxyOnGetMethod.GetGenericArguments().Length != 2)
            {
                throw new InvalidOperationException(
                    "OnSet method of base class is required to have two generic parameters.");
            }

            var proxyOnSetMethodInstance = proxyOnSetMethod.MakeGenericMethod(proxyTargetType, proxyProp.PropertyType);

            var getMethod = (MethodDefinition) proxyProp.GetGetMethod(true);
            if (getMethod != null)
            {
                var getIl = getMethod.GetILGenerator();

                getIl.Emit(OpCodes.Ldarg_0);
                getIl.Emit(OpCodes.Ldsfld, propertyWrapperField);
                getIl.Emit(OpCodes.Callvirt, proxyOnGetMethodInstance);
                getIl.Emit(OpCodes.Ret);
            }

            var setMethod = (MethodDefinition) proxyProp.GetSetMethod(true);
            if (setMethod != null)
            {
                var setIl = setMethod.GetILGenerator();
                setIl.Emit(OpCodes.Ldarg_0);
                setIl.Emit(OpCodes.Ldsfld, propertyWrapperField);
                setIl.Emit(OpCodes.Ldarg_1);
                setIl.Emit(OpCodes.Callvirt, proxyOnSetMethodInstance);
                setIl.Emit(OpCodes.Ret);
            }
        }
开发者ID:BeeWarloc,项目名称:Pomona,代码行数:84,代码来源:WrappedPropertyProxyBuilder.cs


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