How to define an extension method in a scriptcs csx script

前端 未结 3 652
逝去的感伤
逝去的感伤 2021-01-01 16:38

I\'m playing with ScriptCS (which is awesome!) but I couldn\'t figure out how to define an extension method within a .csx script file.

Take this e

3条回答
  •  说谎
    说谎 (楼主)
    2021-01-01 17:28

    Good news! It is now supported in C# script files (.csx)

    But you have to declare an extension method on top level:

    static string MyToLowerExtension(this string str)
    {
        return str.ToLower();
    }
    

    Do not declare it in a static class:

    // this will not work!
    public static class MyExtensionsClass
    {
        static string MyToLowerExtension(this string str)
        {
            return str.ToLower();
        }
    }
    

提交回复
热议问题