简体   繁体   English

不能从匿名方法引用非静态方法

[英]can't reference non-static method from anonymous method

i need to call a non-static method from an async operation , for ease i'm using apm design , by defining a delegate assign it an anonymous method and calling beginInvoke on it . 我需要从异步操作中调用非静态方法,以便于使用apm设计,通过定义委托为其分配匿名方法并在其上调用beginInvoke。

to my surprise i could not reference a non-static method from my implementation 令我惊讶的是,我无法从我的实现中引用非静态方法

any idea why that is ? 知道为什么会这样吗?

public delegate void UpdatePlayersLogin(IServerCallback callback, Guid callback_playerId, Player player, List<IServerCallback> toRemove, ManualResetEvent handel);

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant , InstanceContextMode = InstanceContextMode.PerSession)]
public class ServerService : IServer
{

    UpdatePlayersLogin updateLogin = (callback, callback_playerId, player, toRemove, handle) =>
    {
        try
        {
            callback.PlayerChangedStatus(player);
        }
        catch (Exception)
        {
            RemovePlayer(callback, callback_playerId, toRemove);
        }
        finally
        {
            handle.Set();
        }
    };

    .
    .
    private void RemovePlayer(IServerCallback callback, Guid playerId, List<IServerCallback> toRemove)
    { . . . . . . }

    private void NotifyPeersOfClientLogin(Player player)
    {
        . . . . .   
         foreach (var key_CallBackPair in players)
         {
                handels[i] = new ManualResetEvent(false);
                updateLogin.BeginInvoke(key_CallBackPair.Value, key_CallBackPair.Key, player, toRemove, handels[i], null, null);                     
                . . . . .
         }
        ..... 
    }

is there a way i could reference the non-static method ? 有没有办法可以参考非静态方法?

That should be fine if the lambda expression itself is within an instance method... but if it's in a static method, then what instance do you expect RemovePlayer to be called on? 如果lambda表达式本身在一个实例方法中,那应该没问题......但如果它在一个静态方法中,那么你期望RemovePlayer哪个实例的RemovePlayer

(As an aside, update_players_login is a highly unconventional type name. UpdatePlayersLogin would be better.) UpdatePlayersLoginupdate_players_login是一个非常规的类型名称UpdatePlayersLogin会更好。)

EDIT: Okay, my guess is that you're declaring an instance variable, like this: 编辑:好的,我的猜测是你要声明一个实例变量,如下所示:

class SomeClass
{
    Action action = () => Foo();

    void Foo()
    {
    }
}

If that's not the case, please clarify your post, because it's missing important information at the moment. 如果情况并非如此,请澄清您的帖子,因为它目前缺少重要信息。

If that is the case, the problem is just that an instance variable initializer can't refer to this ... but you can initialize it in the constructor instead: 如果这种情况,问题只是实例变量初始值设定项不能引用this ...但您可以在构造函数中初始化它:

class SomeClass
{
    Action action;

    public SomeClass()
    {
        action = () => Foo();
    }

    void Foo()
    {
    }
}

I would also make the field readonly unless you intend to reassign it somewhere else. 除非你打算将它重新分配到其他地方,否则我也会将该字段设为readonly

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM