相关疑难解决方法(0)

为什么有些C#lambda表达式编译为静态方法?

正如您在下面的代码中看到的,我已将Action<>对象声明为变量.

有人请让我知道为什么这个动作方法委托表现得像一个静态方法?

为什么它会true在以下代码中返回?

码:

public static void Main(string[] args)
{
    Action<string> actionMethod = s => { Console.WriteLine("My Name is " + s); };

    Console.WriteLine(actionMethod.Method.IsStatic);

    Console.Read();
}
Run Code Online (Sandbox Code Playgroud)

输出:

示例输出示例

.net c# reflection lambda

121
推荐指数
3
解决办法
8143
查看次数

为什么局部函数有“internal”访问修饰符?

为什么 Roslyn 编译器生成带有internal访问修饰符(在 IL 中,assembly)而不是 的本地函数private

    private void M()
    {
        bool f = true;
        bool x1() => f;
        static bool x2() => true;
    }
Run Code Online (Sandbox Code Playgroud)
    .method assembly hidebysig static
        bool '<M>g__x1|0_0' (
            valuetype C/'<>c__DisplayClass0_0'& ''
        ) cil managed { ... }

    .method assembly hidebysig static
        bool '<M>g__x2|0_1' () cil managed { ... }
Run Code Online (Sandbox Code Playgroud)

锐实验室

文档说它应该是私有的: https: //learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/local-functions

本地函数是嵌套在另一个成员中的类型的私有方法。

由于所有本地函数都是私有的,包括访问修饰符(例如 private 关键字),会生成编译器错误 CS0106

c#

8
推荐指数
1
解决办法
332
查看次数

标签 统计

c# ×2

.net ×1

lambda ×1

reflection ×1