LorenVS
LorenVS

Reputation: 12867

Generate Extension Methods using System.CodeDom

Has anyone ever tried to generate extension methods using System.CodeDom under .NET 4.0? There doesn't seem to be any way to specify a CodeMemberMethod or CodeParameterDeclarationExpression as being an extension method/parameter.

If this isn't possible, are there any good workarounds?

Thanks

Upvotes: 7

Views: 1169

Answers (2)

Thomas Levesque
Thomas Levesque

Reputation: 292465

Apparently CodeDom isn't able to generate the correct code for the first parameter of an extension method, but you can cheat it like this:

var param = new CodeParameterDeclarationExpression("this string", "s");

It will blissfully ignore the fact that "this string" is not a valid type...

Upvotes: 7

sehe
sehe

Reputation: 393134

See Extension Attribute

Quote:

In Visual Basic, you should use this attribute to create an extension method. For more information, see Extension Methods (Visual Basic).

In C#, you do not need to use this attribute; you should use the this (C# Reference) modifier for the first parameter to create an extension method. The compiler automatically emits ExtensionAttribute for extension methods. For more information, see Extension Methods (C# Programming Guide).

If you are writing a compiler that supports extension methods, your compiler should emit this attribute on each extension method and on each class and assembly that contains one or more extension methods.

Upvotes: 0

Related Questions