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


C# TypeBuilder.DefineProperty方法代码示例

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


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

示例1: CreateAutoImplementedProperty

 public static void CreateAutoImplementedProperty(TypeBuilder builder, string propertyName, Type propertyType)
 {
     const string PrivateFieldPrefix = "m_";
     const string GetterPrefix = "get_";
     const string SetterPrefix = "set_";
     // Generate the field.
     FieldBuilder fieldBuilder = builder.DefineField(string.Concat(PrivateFieldPrefix, propertyName), propertyType, FieldAttributes.Private);
     // Generate the property
     PropertyBuilder propertyBuilder = builder.DefineProperty(propertyName, System.Reflection.PropertyAttributes.HasDefault, propertyType, null);
     // Property getter and setter attributes.
     MethodAttributes propertyMethodAttributes = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
     // Define the getter method.
     MethodBuilder getterMethod = builder.DefineMethod(string.Concat(GetterPrefix, propertyName), propertyMethodAttributes, propertyType, Type.EmptyTypes);
     // Emit the IL code.
     // ldarg.0
     // ldfld,_field
     // ret
     ILGenerator getterILCode = getterMethod.GetILGenerator();
     getterILCode.Emit(OpCodes.Ldarg_0);
     getterILCode.Emit(OpCodes.Ldfld, fieldBuilder);
     getterILCode.Emit(OpCodes.Ret);
     // Define the setter method.
     MethodBuilder setterMethod = builder.DefineMethod(string.Concat(SetterPrefix, propertyName), propertyMethodAttributes, null, new Type[] { propertyType });
     // Emit the IL code.
     // ldarg.0
     // ldarg.1
     // stfld,_field
     // ret
     ILGenerator setterILCode = setterMethod.GetILGenerator();
     setterILCode.Emit(OpCodes.Ldarg_0);
     setterILCode.Emit(OpCodes.Ldarg_1);
     setterILCode.Emit(OpCodes.Stfld, fieldBuilder);
     setterILCode.Emit(OpCodes.Ret);
     propertyBuilder.SetGetMethod(getterMethod);
     propertyBuilder.SetSetMethod(setterMethod);
 }
开发者ID:yangningyuan,项目名称:webs_ShuSW,代码行数:36,代码来源:DatabaseExtensions.cs

示例2: CreateProperty

            private static void CreateProperty(TypeBuilder tb, string propertyName, Type propertyType)
            {
                FieldBuilder fieldBuilder = tb.DefineField("_" + propertyName, propertyType, FieldAttributes.Private);

                PropertyBuilder propertyBuilder = tb.DefineProperty(propertyName, PropertyAttributes.HasDefault, propertyType, null);
                MethodBuilder getPropMthdBldr = tb.DefineMethod("get_" + propertyName, MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig, propertyType, Type.EmptyTypes);
                ILGenerator getIl = getPropMthdBldr.GetILGenerator();

                getIl.Emit(OpCodes.Ldarg_0);
                getIl.Emit(OpCodes.Ldfld, fieldBuilder);
                getIl.Emit(OpCodes.Ret);

                MethodBuilder setPropMthdBldr =
                tb.DefineMethod("set_" + propertyName,
                  MethodAttributes.Public |
                  MethodAttributes.SpecialName |
                  MethodAttributes.HideBySig,
                  null, new[] { propertyType });

                ILGenerator setIl = setPropMthdBldr.GetILGenerator();
                Label modifyProperty = setIl.DefineLabel();
                Label exitSet = setIl.DefineLabel();

                setIl.MarkLabel(modifyProperty);
                setIl.Emit(OpCodes.Ldarg_0);
                setIl.Emit(OpCodes.Ldarg_1);
                setIl.Emit(OpCodes.Stfld, fieldBuilder);

                setIl.Emit(OpCodes.Nop);
                setIl.MarkLabel(exitSet);
                setIl.Emit(OpCodes.Ret);

                propertyBuilder.SetGetMethod(getPropMthdBldr);
                propertyBuilder.SetSetMethod(setPropMthdBldr);
            }
开发者ID:siarheimilkevich,项目名称:VSSync,代码行数:35,代码来源:TypeBuilder.cs

示例3: DoLink

		internal void DoLink(TypeBuilder tb)
		{
			if(getter != null)
			{
				getter.Link();
			}
			if(setter != null)
			{
				setter.Link();
			}
			pb = tb.DefineProperty(this.Name, PropertyAttributes.None, this.FieldTypeWrapper.TypeAsSignatureType, Type.EmptyTypes);
			if(getter != null)
			{
				pb.SetGetMethod((MethodBuilder)getter.GetMethod());
			}
			if(setter != null)
			{
				pb.SetSetMethod((MethodBuilder)setter.GetMethod());
			}
#if STATIC_COMPILER
			AttributeHelper.SetModifiers(pb, this.Modifiers, this.IsInternal);
#endif
		}
开发者ID:jira-sarec,项目名称:ICSE-2012-TraceLab,代码行数:23,代码来源:MemberWrapper.cs

示例4: Emit

    public void Emit(TypeBuilder Builder)
    {
        if (CLRType != null)
        {
            try
            {
                // see: http://msdn.microsoft.com/en-us/library/System.Reflection.Emit.PropertyBuilder.aspx for more info
                Type StorageType = (Type != ReflectedType.ARRAY) ? CLRType.MakeArrayType() : CLRType;
                Type PropertyType = (ArrayDim > 1) ? CLRType.MakeArrayType() : CLRType;

                // Add the private field, named _<Name>
                FieldBuilder Field = Builder.DefineField(FieldName, StorageType, FieldAttributes.Private);

                // Now add a public property to wrap the field, <Name>
                PropertyBuilder Prop = Builder.DefineProperty(Name, System.Reflection.PropertyAttributes.HasDefault, PropertyType, null);

                // Put the property in a category
                ConstructorInfo CategoryCtor = typeof(CategoryAttribute).GetConstructor(new System.Type[] { typeof(string) });
                CustomAttributeBuilder CategoryAttr = new CustomAttributeBuilder(CategoryCtor, new object[] {Category});
                Prop.SetCustomAttribute(CategoryAttr);

                MethodInfo GetValueMethod = null;
                MethodInfo SetValueMethod = null;
                Type GetReturnType = CLRType;
                Type SetArgumentType = CLRType;
                MethodAttributes GetSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
                BindingFlags MethodSearchFlags = BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.Public;
                if (Type == ReflectedType.ARRAY)
                {
                    GetReturnType = SetArgumentType = CLRType.GetGenericArguments()[0];
                    GetValueMethod = StorageType.GetMethod("get_Item", MethodSearchFlags, null, new Type[] { typeof(Int32) }, null);
                    SetValueMethod = StorageType.GetMethod("set_Item", MethodSearchFlags, null, new Type[] { typeof(Int32), SetArgumentType }, null);
                }
                else
                {
                    GetValueMethod = StorageType.GetMethod("Get", MethodSearchFlags, null, new Type[] { typeof(Int32) }, null);
                    SetValueMethod = StorageType.GetMethod("Set", MethodSearchFlags, null, new Type[] { typeof(Int32), CLRType }, null);
                }

                //Build direct access for single values, basically just calls GetValue(0) or SetValue(0, value)
                if (ArrayDim == 1 && Type != ReflectedType.ARRAY)
                {
                    // Build get method
                    Debug.Assert(GetValueMethod != null);
                    MethodBuilder GetMethod = Builder.DefineMethod("get_" + Name, GetSetAttr, GetReturnType, System.Type.EmptyTypes);
                    ILGenerator GetOpCodes = GetMethod.GetILGenerator();
                    GetOpCodes.Emit(OpCodes.Ldarg_0);               // push this
                    GetOpCodes.Emit(OpCodes.Ldfld, Field);          // push field
                    GetOpCodes.Emit(OpCodes.Ldc_I4, 0);             // push index of 0
                    GetOpCodes.Emit(OpCodes.Call, GetValueMethod);  // call this.GetValue(0)
                    GetOpCodes.Emit(OpCodes.Ret);                   // return whatever that left on the stack
                    Prop.SetGetMethod(GetMethod);

                    // Build set method
                    Debug.Assert(SetValueMethod != null);
                    MethodBuilder SetMethod = Builder.DefineMethod("set_" + Name, GetSetAttr, null, new Type[] { SetArgumentType });
                    ILGenerator SetOpCodes = SetMethod.GetILGenerator();
                    SetOpCodes.Emit(OpCodes.Ldarg_0);               // push this
                    SetOpCodes.Emit(OpCodes.Ldfld, Field);          // push field
                    SetOpCodes.Emit(OpCodes.Ldc_I4, 0);             // push index of 0
                    SetOpCodes.Emit(OpCodes.Ldarg_1);               // push value
                    SetOpCodes.Emit(OpCodes.Call, SetValueMethod);  // call this.SetValue(0, value)
                    SetOpCodes.Emit(OpCodes.Ret);                   // return
                    Prop.SetSetMethod(SetMethod);
                }
                else // Build array/indexed accessors
                {
                    // Build get method
                    Debug.Assert(GetValueMethod != null);
                    MethodBuilder GetMethod = Builder.DefineMethod("get_" + Name, GetSetAttr, GetReturnType, new Type[] { typeof(int) });
                    ILGenerator GetOpCodes = GetMethod.GetILGenerator();
                    GetOpCodes.Emit(OpCodes.Ldarg_0);               // push this
                    GetOpCodes.Emit(OpCodes.Ldfld, Field);          // push field
                    GetOpCodes.Emit(OpCodes.Ldarg_1);               // push index
                    GetOpCodes.Emit(OpCodes.Call, GetValueMethod);  // call this.GetValue(index)
                    GetOpCodes.Emit(OpCodes.Ret);                   // return
                    Prop.SetGetMethod(GetMethod);

                    // Build set method
                    Debug.Assert(SetValueMethod != null);
                    MethodBuilder SetMethod = Builder.DefineMethod("set_" + Name, GetSetAttr, null, new Type[] { typeof(int), SetArgumentType });
                    ILGenerator SetOpCodes = SetMethod.GetILGenerator();
                    SetOpCodes.Emit(OpCodes.Ldarg_0);               // push this
                    SetOpCodes.Emit(OpCodes.Ldfld, Field);          // push field
                    SetOpCodes.Emit(OpCodes.Ldarg_1);               // push index
                    SetOpCodes.Emit(OpCodes.Ldarg_2);               // push value
                    SetOpCodes.Emit(OpCodes.Call, SetValueMethod);  // call this.SetValue(index, value)
                    SetOpCodes.Emit(OpCodes.Ret);                   // return
                    Prop.SetSetMethod(SetMethod);
                }
            }
            catch (System.Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }
    }
开发者ID:justinboswell,项目名称:CLRTypes,代码行数:97,代码来源:ReflectedProperty.cs

示例5: GenerateInterfaceMethod

    void GenerateInterfaceMethod(TypeBuilder tb,
                                 TypeInfo.MethodDescriptor desc)
    {
        if (desc == null || !desc.IsVisible())
            return;

        MethodAttributes methodAttrs = METHOD_ATTRS;

        String methodName = desc.name;
        if (desc.IsGetter()) {
            methodName = "get_" + desc.name;
            methodAttrs |= MethodAttributes.SpecialName;
        } else if (desc.IsSetter()) {
            methodName = "set_" + desc.name;
            methodAttrs |= MethodAttributes.SpecialName;
        }

        // Fix up interface types in parameters
        Type ret = desc.resultType;
        if (ret == typeof(object))
            ret = FixupInterfaceType(desc.args[desc.args.Length - 1]);
        Type[] argTypes = (Type[])desc.argTypes.Clone();
        for (int i = 0; i < argTypes.Length; i++) {
            if (argTypes[i] == typeof(object))
                argTypes[i] = FixupInterfaceType(desc.args[i]);
        }
        MethodBuilder meth = tb.DefineMethod(methodName, methodAttrs, ret,
                                             argTypes);

        if (desc.IsSetter()) {
            if (lastProperty != null && lastProperty.Name == desc.name) {
                lastProperty.SetSetMethod(meth);
            } else {
                tb.DefineProperty(desc.name, PROPERTY_ATTRS, ret, new Type[0])
                    .SetSetMethod(meth);
            }
            lastProperty = null;
        } else if (desc.IsGetter()) {
            lastProperty = tb.DefineProperty(desc.name, PROPERTY_ATTRS, ret,
                                             new Type[0]);
            lastProperty.SetGetMethod(meth);
        } else {
            lastProperty = null;
        }
    }
开发者ID:nfan,项目名称:Jaxer,代码行数:45,代码来源:generate-interfaces.cs


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