无法分配 Action 属性

问题描述 投票:0回答:1

这里一定要疯了,但我看不出我在下面的代码中做错了什么导致语法错误

CS0828:无法将“方法组”分配给匿名类型属性

我在代码中看不到任何匿名信息,该属性有一个名称。我分配给它的方法被命名为等。

    // A basic function that takes an int and has void return
    private static void DoSomething(int d) 
    {
        // .... do stuff
    };

    // A class is defined with an Action<int> delegate
    private class XX
    {
        public Action<int> YY { get; set; }
    }

    // An instance of the class which clearly assigns a known method to the property
    private static readonly XX xx = new
    {
        // error "CS0828: Cannot assign 'method group' to anonymous type property"
        YY = DoSomething
    };
c# func
1个回答
0
投票

您应该

create an instance of the XX
类,然后将方法分配给 YY 属性:

    private static readonly XX xx = new XX
    {
        // error "CS0828: Cannot assign 'method group' to anonymous type property"
        YY = DoSomething
    };
© www.soinside.com 2019 - 2024. All rights reserved.