Partager via


CodeDom now supports a way to generate code for CodeTypeMember [Vinaya Bhushana Gattam Reddy]

CodeDom now supports a way to generate code for CodeTypeMember.

In V1.1 there is no way to generate a method without a type information using CodeDom. There are several tools and applications including ASP.Net had requirements to create a method without the type or namespace information. Currently one has to parse the generated code or file to get the method code but that work around is horrible because it’s hard to implement and one has to come up with a solution that works for all the languages.

In Whidbey, we added an API to the CodeDomProvider class to generate a method or in general any CodeTypeMember type code directly without the type information. We have re-arranged the existing code generation API’s little bit to support this new feature and to provide consistency across all code generation API’s. We did the following changes in Whidbey to support this functionality…

  1. Obsoleted ICodeGenerator and ICodeCompiler interfaces.
  2. Moved all the methods in those interfaces to the abstract class (CodeDomProvider)
  3. Added GenerateCodeFromMember( CodeTypeMemeber, …)

Providers support:

Not all Microsoft Code Providers supports this new API in Whidbey time frame. The workaround is that one would be able try the call and catch exceptions to determine if it's supported or not. It’s kind of ugly but we have plans to provide a better mechanism in the next release to query the Code Provider to see whether it supports code generation from CodeTypeMember or not,

Code Snippet: The following code snippet demonstrates how to generate code for method without type information.

            CodeMemberMethod method = new CodeMemberMethod();

method.Name = "TestMethod";

CodeDomProvider provider = new CSharpCodeProvider();

StringWriter writer = new StringWriter();

provider.GenerateCodeFromMember(method, writer, null);

Console.WriteLine(writer.ToString());

writer.Close();

Sample output:

private void TestMethod() {}

Comments

  • Anonymous
    March 15, 2005
    CodeDom now supports a way to generate code for CodeTypeMember
  • Anonymous
    March 16, 2005
    Yay! Finally :)
  • Anonymous
    March 17, 2005
    Tips
  • Anonymous
    March 21, 2005
    Tips