https://stackoverflow.com/questions/7036367/static-dictionary-containing-delegates-to-instance-methods

 

@this 는 this 가 C# 예약어이기 때문에 변수명으로 사용하기 위해 @를 붙인다.

class MyClass
{
    static Dictionary<string, Func<MyClass, string, bool>> commands
        = new Dictionary<string, Func<MyClass, string, bool>>
    {
        { "Foo", (@this, x) => @this.Foo(x) },
        { "Bar", (@this, y) => @this.Bar(y) }
    };

    public bool Execute(string command, string value)
    {
        return commands[command](this, value);
    }

    public bool Foo(string x)
    {
        return x.Length > 3;
    }

    public bool Bar(string x)
    {
        return x == "";
    }
}

'C#' 카테고리의 다른 글

C# .net core 빌드 및 powershell 전송  (0) 2022.03.01
c# stack size 확인  (0) 2022.02.24
숫자 범위 추출 및 확장  (0) 2021.11.03
C# LZ4  (0) 2021.10.20
[ASP.NET] .net core 2.2 singleton controller  (0) 2019.08.29
[AppMetrics 3.1.0] ASP.NET Core 2.2 모니터링 with InfluxDB, Grafana  (0) 2019.08.06
[asp.net] asp.net core 2.2 iis 게시  (0) 2019.08.03
C#  (0) 2011.05.15