如何捕获变量 (C#)

发布于 2024-08-02 06:21:07 字数 1073 浏览 2 评论 0 原文

如何捕获变量?
或者,我可以存储对对象引用的引用吗?

通常,方法可以使用 ref 关键字更改其外部的变量。

void Foo(ref int x)
{
    x = 5;
}

void Bar()
{
    int m = 0;
    Foo(ref m);
}

这是明确且直接的。

现在让我们考虑一个类来实现同样的事情:

class Job
{
    // ref int _VarOutsideOfClass; // ?????

    public void Execute()
    {
        // _VarOutsideOfClass = 5; // ?????
    }
}

void Bar()
{
    int m = 0;
    var job = new Job()
    {
        _VarOutsideOfClass = ref m    // How ?
    };
    job.Execute();
}

我如何正确地编写它?


注释:我无法将其设置为带有 ref 参数的方法,因为通常 Execute() 稍后会在队列中出现时在不同的线程中调用。

目前,我制作了一个带有大量 lambda 的原型:

class Job
{
    public Func<int> InParameter;
    public Action<int> OnResult;

    public void Execute()
    {
        int x = InParameter();
        OnResult(5);
    }
}

void Bar()
{
    int m = 0;
    var job = new Job()
    {
        InParameter = () => m,
        OnResult = (res) => m = res
    };
    job.Execute();
}

...但也许有更好的主意。

How Do I capture a variable?
Alternatively, can I store a reference to an object reference?

Normally, a method can alter a variable outside of it using ref keyword.

void Foo(ref int x)
{
    x = 5;
}

void Bar()
{
    int m = 0;
    Foo(ref m);
}

This is clear and straight-forward.

Now let's consider a class to achieve the same thing:

class Job
{
    // ref int _VarOutsideOfClass; // ?????

    public void Execute()
    {
        // _VarOutsideOfClass = 5; // ?????
    }
}

void Bar()
{
    int m = 0;
    var job = new Job()
    {
        _VarOutsideOfClass = ref m    // How ?
    };
    job.Execute();
}

How do I write it correctly ?


Comments: I can't make it a method with an ref argument, because typically Execute() will called somewhat later in a different thread, when it comes up in the queue.

Currently, I made a prototype with plenty of lambdas:

class Job
{
    public Func<int> InParameter;
    public Action<int> OnResult;

    public void Execute()
    {
        int x = InParameter();
        OnResult(5);
    }
}

void Bar()
{
    int m = 0;
    var job = new Job()
    {
        InParameter = () => m,
        OnResult = (res) => m = res
    };
    job.Execute();
}

... but maybe there is a better idea.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

じее 2024-08-09 06:21:07

你不能有 ref 字段。例如,请参见 http://blogs.msdn.com/ericlippert/archive/2009/05/04/the-stack-is-an-implementation-detail-part-two.aspx(向下滚动到其中写着“这解释了为什么你不能创建“ref int”字段......”)。

lambda 或委托可能是您最好的选择。我想你可以使用事件,或者观察者接口,或者其他东西。

You can't have a ref field. See, for example, http://blogs.msdn.com/ericlippert/archive/2009/05/04/the-stack-is-an-implementation-detail-part-two.aspx (scroll down to where it says "This explains why you cannot make a “ref int” field....").

A lambda or a delegate is probably your best bet here. I suppose you could use an event, or an observer interface, or something.

执着的年纪 2024-08-09 06:21:07

使用具有 1 个元素的数组

class Job{
int[] _VarOutsideOfClass = new int[1];

您还可以使用包装器“int?” - 原谅它们可以为空,但请记住它总是传递引用。

Use Array with 1 element

class Job{
int[] _VarOutsideOfClass = new int[1];

Also You can use wrapper "int?" - forgive them nullable, but remember that it always passed over reference.

自此以后,行同陌路 2024-08-09 06:21:07

这是一个猜测(我还没有尝试/测试过):

class Job
{
  Action<int> m_delegate;

  public Job(ref int x)
  {
    m_delegate = delegate(int newValue)
    {
      x = newValue;
    };
  }

  public void Execute()
  {
    //set the passed-in varaible to 5, via the anonymous delegate
    m_delegate(5);
  }
}

如果上面的方法不起作用,那么就说 Job 构造函数将委托作为其参数,并在 Bar 类内部构造委托(并传递委托而不是传递委托)传递 ref 参数)。

Here's a guess (I haven't tried/tested it):

class Job
{
  Action<int> m_delegate;

  public Job(ref int x)
  {
    m_delegate = delegate(int newValue)
    {
      x = newValue;
    };
  }

  public void Execute()
  {
    //set the passed-in varaible to 5, via the anonymous delegate
    m_delegate(5);
  }
}

If the above doesn't work, then say that the Job constructor takes a delegate as its parameter, and construct the delegate inside the Bar class (and pass the delegate instead of passing the ref parameter).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文