2017-04-27 52 views
1

要的CodeDOM创建支持字段属性,我们可以使用一个建筑像这样:C#CodeDOM的新属性,它返回一个字典元素

CodeMemberProperty property = new CodeMemberProperty; 
      property.GetStatements.Add(
       new CodeMethodReturnStatement(
        new CodeFieldReferenceExpression(
         new CodeThisReferenceExpression(), 
                 fieldName))); 

但必须是什么建筑内部“CodeMethodReturnStatement”像一个结果:

public string SomeProp { get { return _someDict[_someKey]; } } 

其中:

_someDict = new Dictionary<string, string>(); 
_someKey = "someKey"; 

_someDict和_someKey是SA成员我生成类。

+0

我认为你需要CodeIndexerExpression类。 – john

+0

您不应编辑您的问题以包含答案。相反,你应该把它作为答案发布,然后你可以接受它。 – svick

回答

0

感谢评论我要解决这样的问题:

CodeIndexerExpression indexerExpression = 
      new CodeIndexerExpression(
       new CodeFieldReferenceExpression(
        new CodeTypeReferenceExpression(typeof(Class1)), 
        "dict"), 
       new CodeFieldReferenceExpression(
        new CodeTypeReferenceExpression(typeof(Class1)), 
        "key")); 

请注意,我的重点领域和字典场都是静态的,但对于局部变量的想法是一样的。 只需使用CodeThisReferenceExpression代替CodeTypeReferenceExpression即可。

public static string Prop 
    { 
     get 
     { 
      return Class1.dict[Class1.key]; 
     } 
    } 
相关问题