使用lambda在C#中使用不兼容的匿名函数签名

时间:2013-11-05 05:45:16

标签: c# lambda

// method 1
public void RegisterPlayer(Player player)
{
    var mysealedClass = new GreatSubscription
    {
        player_0 = player,
        Subscription = this
    };

    Subscribe(_registeredPlayers, player, PlayerTypes.Player, mysealedClass.Register);
}

// Method 2
private void Subscribe<T>(CachedSynchronizedDictionary<T, int> subscribers, 
    T subscriber, PlayerTypes type, Func<bool> isRegistered) where T: Player
{
// There is some code here -- intentionally omitted, since it is irrelevant
}

// Method 3
protected virtual bool OnRegisterPlayer(Player myplayer)
{
    return false;
}

// Sealed Class
private sealed class GreatSubscription
{
    public Subscription NewSubscription;
    public Player NewPlayer;

    public bool Register()
    {
        return NewSubscription.OnRegisterPlayer(NewPlayer);
    }
}

我想在方法1中使用匿名方法并删除mysealedClass&amp;它的引用,但我在lambda之前的rs上收到错误“不兼容的匿名函数签名”。我做错了什么?

任何帮助都将不胜感激。

当我按如下方式编写方法1时:

// method 1
public void RegisterPlayer(Player player)
{                  
   Subscribe(_registeredPlayers, player, PlayerTypes.Player, rs => this.OnRegisterPlayer(player));
}

1 个答案:

答案 0 :(得分:1)

您的lambda接受一个参数,但isRegisteredFunc<bool>,不接受任何参数。删除rs参数:

Subscribe(_registeredPlayers, player, PlayerTypes.Player, () => this.OnRegisterPlayer(player));