不能引用来自匿名方法非静态方法 [英] can't reference non-static method from anonymous method

查看:135
本文介绍了不能引用来自匿名方法非静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从一个异步操作调用非静态方法, 为便于我使用APM设计,通​​过定义一个委托给它分配一个匿名方法 并呼吁BeginInvoke的就可以了。

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 .

让我吃惊我无法从我的执行引用非静态方法

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 ?

推荐答案

这应该是罚款,如果在lambda EX pression本身是一个实例方法内......但如果​​它在一个静态方法,那么什么情况下做你期望 RemovePlayer 被称为呢?

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?

(顺便说一句, update_players_login 是一个非常非常规的类型名称。 UpdatePlayersLogin 会更好。)

(As an aside, update_players_login is a highly unconventional type name. UpdatePlayersLogin would be better.)

编辑:好的,我的猜测是,你声明的实例变量,像这样的:

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:

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.

这篇关于不能引用来自匿名方法非静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆