1

I have a CodeMethodInvokeExpression which i wish to use as ()=><[function call]>.

E.g.: Task.Run(()=><[My CodeMethodInvokeExpression]>).

And just to be clear, CodeMethodInvokeExpression can be very complex, such as a call to a generic method with out and ref pramaeters, so trying to write something which parses it, may be very complicated.

So the questions are:

  1. Might there be a CodeDom way of doing this?
  2. Might there be a CodeDom way of getting the string representation of CodeMethodInvokeExpression so I could use it as a CodeSnippetExpression.

1 Answer 1

1

CodeDom is kind of obsolete. As far as a I know, the best you can do is using CodeSnippetExpression, but by the time you have CodeMethodInvokeExpression, it's too late - you don't really have information about the method you're trying to call, just about it's name, object and arguments. Not to mention that it kind of defeats the purpose of using CodeDom in the first place.

Of course, you're generating code, so you could just create the anonymous method yourself, that's what the compiler does anyway. But again, you'll need more information than what you're getting in the CodeMethodInvokeExpression. In the end, you're just going in circles.

Also, note that out is C#'s speciality - it's not actually something common to CLR languages. In other languages, it might just be the same as ref.

Note that you can use CodeDom to generate code snippets:

var provider = new CSharpCodeProvider();

using (var writer = new StringWriter())
{
    provider.GenerateCodeFromExpression
        (
            new CodeMethodInvokeExpression
            (
                new CodeMethodReferenceExpression
                (
                    new CodeThisReferenceExpression(), 
                    "MyMethod", 
                    new CodeTypeReference(typeof(string))
                ),
                new CodeDirectionExpression(FieldDirection.Out, new CodeArgumentReferenceExpression("myArgument"))
            ),
            writer,
            null
        );

    writer.ToString().Dump();
}

This sample code will then generate this:

this.MyMethod(out myArgument)

You can create a CodeSnippetExpression out of that, prepending the () => manually, and use that snippet as an argument to your method taking a delegate. Again, this will only work for C# - you'll need to modify the code to make it work elsewhere.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I saw that method, but only after you wrote it, I decided to use it. But you said CodeDom is obsolete. What is the newer alternative?
@YechielB.D. I don't think there's any explicit replacement. However, there are other ways of accomplishing the same task; T4 templates are quite popular for generating code in compile-time, while expression trees are very useful while generating (and compiling) code in runtime. CodeDom doesn't really give you much except for multi-language support - if you're not using that part, T4s (or other templating generators) are more straightforward and easier to read.

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.