1

I have used CodeDOM to generate C# code but I'm not able to generate code for following line of statement.

string FileName=String.Empty;
FileName=Path.GetFileName(file1);

From above code I have written the code for first line using following snippet

using(CodeVariableDeclarationStatement strFileName = 
    new CodeVariableDeclarationStatement(typeof(string), "strFileName",
    new CodeTypeReferenceExpression("String.Empty"));) 

but not able to add code for second line using codeDOM. So please let me know how can I achive it.

4
  • You have to use CodeAssignStatement Commented Feb 19, 2016 at 10:06
  • I have used CodeAssignStatement to generate following line of code. CodeAssignStatement stat = new CodeAssignStatement(new CodeVariableReferenceExpression("VPath"), new CodeTypeReferenceExpression(new CodeTypeReference("VPath + \"\\\\\" + arr[arr.Length - 1]"))); But it replaced '+' with '.' in the generated code.So how can I resolve it. Commented Feb 19, 2016 at 11:32
  • @Pankaj Don't use CodeTypeReference if you're not going to reference a type. Also, that seems like a new question to me, so you should ask about that in a new question. Commented Feb 19, 2016 at 16:09
  • @xanatos It looks like your comment answered the question. Do you want to write it as an answer? Commented Feb 19, 2016 at 16:09

1 Answer 1

3
// For the first line: string FileName = string.Empty;
var declareVariableFileName = new CodeVariableDeclarationStatement(                 // Declaring a variable.
    typeof(string),                                                                 // Type of variable to declare.
    "FileName",                                                                     // Name for the new variable.
    new CodePropertyReferenceExpression(                                            // Initialising with a property.
        new CodeTypeReferenceExpression(typeof(string)), "Empty"));                 // Identifying the property to invoke.

// For the second line: FileName = System.IO.Path.GetFileName(file1);
var assignGetFileName = new CodeAssignStatement(                                    // Assigning a value to a variable.
    new CodeVariableReferenceExpression("FileName"),                                // Identifying the variable to assign to.
    new CodeMethodInvokeExpression(                                                 // Assigning from a method return.
        new CodeMethodReferenceExpression(                                          // Identifying the class.
            new CodeTypeReferenceExpression(typeof(Path)),                          // Class to invoke method on.
            "GetFileName"),                                                         // Name of the method to invoke.
        new CodeExpression[] { new CodeVariableReferenceExpression("file1") }));    // Single parameter identifying a variable.

string sourceCode;

using (StringWriter writer = new StringWriter())
{
    var csProvider = CodeDomProvider.CreateProvider("CSharp");
    csProvider.GenerateCodeFromStatement(declareVariableFileName, writer, null);
    csProvider.GenerateCodeFromStatement(assignGetFileName, writer, null);

    sourceCode = writer.ToString();

    // sourceCode will now be...
    // string FileName = string.Empty;
    // FileName = System.IO.Path.GetFileName(file1);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.