如何使用CodeDom生成器进行这种表达? [英] How to make this kind of expression using CodeDom generator?

查看:100
本文介绍了如何使用CodeDom生成器进行这种表达?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好



我正在尝试使用CodeDom生成这个表达式,我设法制作更简单的表达式,所以如果有可能,我会徘徊吗?



  var  codeGenerator =  new  CSharpCodeProvider()。CreateGenerator(); 
var csp = new CSharpCodeProvider( new Dictionary< string,string> {{ CompilerVersion v4.0}});
codeGenerator = csp.CreateGenerator();

var compileUnit = new CodeCompileUnit();
var codeNamespace = new CodeNamespace( );
codeNamespace.Imports.Add( new CodeNamespaceImport( 系统));
compileUnit.Namespaces.Add(codeNamespace);

var resourceClass = new CodeTypeDeclaration( 测试){Attributes = MemberAttributes.Public};
codeNamespace.Types.Add(resourceClass);

var rootField = new CodeMemberField( typeof string ), );
rootField.InitExpression = new CodePrimitiveExpression( Domain.TestResources)。
resourceClass.Members.Add(rootField);

var field = new CodeMemberField( typeof string ), 标题
{
Attributes = MemberAttributes.Public | MemberAttributes.Static,
InitExpression = new CodePrimitiveExpression( 域。 + dictResource.Key + Resources。 + resource.Value )
};
resourceClass.Members.Add(field);





使用此代码我得到:



 命名空间域{
使用系统;


public class 标题{

private string root = < span class =code-string> Domain.TitleResources。;

public static string ITEM_0 = Domain.TitleResources.SOMETITLE;
}
}





我需要的是:

 命名空间域{
使用系统;


public class 标题{

private string root = < span class =code-string> Domain.TitleResources。;

public static string ITEM_0 = root + SOMETITLE;
}
}





有没有办法生成这样的字段?

提前谢谢,Mirza:)

解决方案

你好,



我想你必须修改生成第二个赋值语句的代码,如下所示。

 InitExpression =  new  CodeBinaryOperatorExpression( new  CodeVariableReferenceExpression(  root),CodeBinaryOperatorType.Add, new  CodePrimitiveExpression(resource.Value)); 



问候,


Hello

I am trying to generate this expression with CodeDom, I managed to make simpler ones, so I'm wandering if this is even possible to do?

var codeGenerator = new CSharpCodeProvider().CreateGenerator();
var csp = new CSharpCodeProvider(new Dictionary<string, string> {{"CompilerVersion", "v4.0"}});
codeGenerator = csp.CreateGenerator();

var compileUnit = new CodeCompileUnit();
var codeNamespace = new CodeNamespace("Domain");
codeNamespace.Imports.Add(new CodeNamespaceImport("System"));
compileUnit.Namespaces.Add(codeNamespace);

var resourceClass = new CodeTypeDeclaration("Test") {Attributes = MemberAttributes.Public};
codeNamespace.Types.Add(resourceClass);

var rootField = new CodeMemberField(typeof (string), "root");
rootField.InitExpression = new CodePrimitiveExpression("Domain.TestResources.");
resourceClass.Members.Add(rootField);

var field = new CodeMemberField(typeof (string), "TITLE")
{
    Attributes = MemberAttributes.Public | MemberAttributes.Static,
    InitExpression = new CodePrimitiveExpression("Domain." + dictResource.Key + "Resources." + resource.Value)
};
resourceClass.Members.Add(field);



With this code I get this:

namespace Domain {
    using System;
    
    
    public class Title {
        
        private string root = "Domain.TitleResources.";
        
        public static string ITEM_0 = "Domain.TitleResources.SOMETITLE";
    }
}



and what I need is this:

namespace Domain {
    using System;


    public class Title {

        private string root = "Domain.TitleResources.";

        public static string ITEM_0 = root + "SOMETITLE";
    }
}



Is there a way to generate such field?
Thanks in advance, Mirza :)

解决方案

Hello,

I think you will have to modify the code which generates the the second assignment statement as shown below.

InitExpression = new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("root"), CodeBinaryOperatorType.Add, new CodePrimitiveExpression(resource.Value));


Regards,


这篇关于如何使用CodeDom生成器进行这种表达?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆