如何使用PropertyBuilder创建私有属性

时间:2015-11-12 15:04:25

标签: c# .net reflection properties reflection.emit

在C#中,我们可以通过执行以下操作来创建私有属性:

PropertyBuilder prop = typeBuilder.DefineProperty("Name", PropertyAttributes.None, CallingConvention.HasThis, typeof(string), Type.EmptyTypes);

但是,假设我们正在使用Reflection.Emit.PropertyBuilder创建属性。

以下代码将创建 public 属性(尚未定义的getter和setter方法):

FieldAttributes.Private

如何使用 私有 可见性来定义此相同的属性?

Reflection.Emit.FieldBuilder可以指定为PropertyBuilder,但C00427@C17PN92CG8WN ~/s/c/ui> mvn clean install [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building ACS.org CQ5 --> PARENT --> UI 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ ui --- [INFO] Deleting /Users/C00427/sites/cq5/ui/target [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.180 s [INFO] Finished at: 2015-11-12T09:59:46-05:00 [INFO] Final Memory: 15M/309M [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.5:clean (default-clean) on project ui: Failed to clean project: Failed to delete /Users/C00427/sites/cq5/ui/target/org.acs.cq-code-1.0-SNAPSHOT.jar -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException C00427@C17PN92CG8WN ~/s/c/ui> 似乎不会提供类似的内容。

这可以用Reflection.Emit吗?

1 个答案:

答案 0 :(得分:3)

构建属性时,您可以设置私有get / set属性,如下所示。您不能将Property本身设置为private。

the documentation开始,相关代码如下所示。

PropertyBuilder custNamePropBldr = myTypeBuilder.DefineProperty("CustomerName",
                                                     PropertyAttributes.HasDefault,
                                                     typeof(string),
                                                     null);

    // The property set and property get methods require a special
    // set of attributes.
    MethodAttributes getSetAttr = 
        MethodAttributes.Public | MethodAttributes.SpecialName |
            MethodAttributes.HideBySig;

    // Define the "get" accessor method for CustomerName.
    MethodBuilder custNameGetPropMthdBldr = 
        myTypeBuilder.DefineMethod("get_CustomerName",
                                   getSetAttr,        
                                   typeof(string),
                                   Type.EmptyTypes);

ILGenerator custNameGetIL = custNameGetPropMthdBldr.GetILGenerator();

    custNameGetIL.Emit(OpCodes.Ldarg_0);
    custNameGetIL.Emit(OpCodes.Ldfld, customerNameBldr);
    custNameGetIL.Emit(OpCodes.Ret);

    // Define the "set" accessor method for CustomerName.
    MethodBuilder custNameSetPropMthdBldr = 
        myTypeBuilder.DefineMethod("set_CustomerName",
                                   getSetAttr,     
                                   null,
                                   new Type[] { typeof(string) });

    ILGenerator custNameSetIL = custNameSetPropMthdBldr.GetILGenerator();

    custNameSetIL.Emit(OpCodes.Ldarg_0);
    custNameSetIL.Emit(OpCodes.Ldarg_1);
    custNameSetIL.Emit(OpCodes.Stfld, customerNameBldr);
    custNameSetIL.Emit(OpCodes.Ret);

    // Last, we must map the two methods created above to our PropertyBuilder to 
    // their corresponding behaviors, "get" and "set" respectively. 
    custNamePropBldr.SetGetMethod(custNameGetPropMthdBldr);
    custNamePropBldr.SetSetMethod(custNameSetPropMthdBldr);