Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

C#
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:

C#
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:
C#
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 :)
Posted
Updated 12-Jun-13 20:46pm
v2
Comments
Sergey Alexandrovich Kryukov 13-Jun-13 2:47am    
Absolutely everything you can write in code, you can generate with CodeDOM (otherwise why CodeDOM). The question is not clear though. Where is that expression? What's the problem, exactly?

Just a note: did you try such a powerful thing as System.Reflection.Emit?
—SA
gwyder 13-Jun-13 3:05am    
Sorry if I wasn't clear?
I want to get this in this form:
<pre lang="c#">public static string ITEM_0 = root + "SOMETITLE" </pre>
No, I haven't tried it, did heard of it. Gonna check it right now. Thank you.

P.S. I got a solution from Prasad.
Sergey Alexandrovich Kryukov 13-Jun-13 3:08am    
Great. I up-voted it.
—SA

1 solution

Hello,

I think you will have to modify the code which generates the the second assignment statement as shown below.
C#
InitExpression = new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("root"), CodeBinaryOperatorType.Add, new CodePrimitiveExpression(resource.Value));

Regards,
 
Share this answer
 
Comments
gwyder 13-Jun-13 3:02am    
That is! Thank you very much :)
Sergey Alexandrovich Kryukov 13-Jun-13 3:08am    
5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900