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);



Sample output:

private void TestMethod() {}

Skip to main content