[英]Can I use a action to pass a value to a method parameter?
I will try my best to explain what I am asking.我会尽力解释我在问什么。
Imagine I have a Action in one class:想象一下我在一个 class 中有一个动作:
public Action Something;
And in another class I subscribe a method ot it with a parameter在另一个 class 中,我使用参数订阅了一个方法
private void Foo(int num)
{
Debug.Log(num);
}
OneClass.Something += Foo;
So basically I want the parameter of Foo to be something that the OneClass Passes.所以基本上我希望 Foo 的参数是 OneClass 传递的东西。 Is that something that exists in C#?
这是 C# 中存在的东西吗?
Is this what you are trying to do:这是你想要做的:
public class Bar
{
public event Action<int> Something;
}
class Program
{
static void Main(string[] args)
{
var bar = new Bar();
bar.Something += Foo;
}
static void Foo(int x)
{
Debug.WriteLine(x);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.