Generate Extension Methods using System.CodeDom

回眸只為那壹抹淺笑 提交于 2019-11-30 20:45:20

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...

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!