如何在 C# 中从字符串调用委托?
是否可以通过变量名称(作为字符串)调用存储在变量中的委托?我想我必须使用反射机制,但我没有得到任何结果
示例代码:
class Demo {
public delegate int DemoDelegate();
private static int One() {
return 1;
}
private static void CallDelegate(string name) {
// somehow get the value of the variable with the name
// stored in "name" and call the delegate using reflection
}
private static void CallDelegate(string name, DemoDelegate d) {
d();
}
static void main(string[] args) {
DemoDelegate one = Demo.One;
CallDelegate(one);
// this works, but I want to avoid writing the name of the variable/delegate twice:
CallDelegate("one", one);
}
}
这可能吗?如果是这样怎么办?
Is it possible to call a delegate stored in a variable by its variable name (as a string)? I guess I'd have to use reflection mechanism, but I'm not getting anywhere
Example code:
class Demo {
public delegate int DemoDelegate();
private static int One() {
return 1;
}
private static void CallDelegate(string name) {
// somehow get the value of the variable with the name
// stored in "name" and call the delegate using reflection
}
private static void CallDelegate(string name, DemoDelegate d) {
d();
}
static void main(string[] args) {
DemoDelegate one = Demo.One;
CallDelegate(one);
// this works, but I want to avoid writing the name of the variable/delegate twice:
CallDelegate("one", one);
}
}
Is this even possible? If so how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
变量几乎不存在。可靠地按字符串调用(在这种情况下)的唯一方法是将委托存储在字典中:
现在将该字典存储在某处(在字段中,通常),并执行以下操作:
Variables barely exist. The only way to reliably call-by-string (in this scenario) would be to store the delegates in a dictionary:
Now store that dictionary somewhere (in a field, typically), and do something like:
是的,这是可能的,只要你使用 Linq 表达式,并且很少进行反射。
看看这段代码,它的功能与我认为您想要的类似:
Yes, it is possible, as long as you use Linq Expressions, and little reflection.
Take a look at this code, it does something simillar to what I think you want:
您无法真正访问另一个堆栈框架中的变量(尽管我认为可以在 StackFrame 类周围使用黑客技术)。相反,如果您想以类似反射的方式调用通用委托,则需要传递
Delegate
对象并使用DynamicInvoke
等方法。You can't really access variables in another stack frame (although I think it is possible using hackery around the
StackFrame
class). Instead, you'll be wanting to pass anDelegate
object around and use methods likeDynamicInvoke
, if you want to invoke a generalised delegate in a reflection-like manner.显然限制您拥有参数化委托。
Obviously restricts you from having parameterized delegates.
我不记得十多年前我想实现什么目标,但现在读到这个问题,我想提出一个潜在的解决方法。该解决方法并不能完全避免重复,但它摆脱了手动字符串化变量名称的情况。
nameof
表达式 允许生成变量、类型或成员的名称。标识符仍然重复,但至少现在使用重构工具更改变量的名称也会更改用于日志记录的名称。现在不可能忘记更新日志记录中使用的名称,因为这会导致编译错误(“未知标识符”)。
I cannot remember what I wanted to achieve more than 10 years ago, but reading the question now, I want to present a potential workaround. The workaround doesn't completely avoid the duplication, but it gets rid of manually stringifying the name of the variable.
The
nameof
expression allows to produce the name of a variable, type, or member.The identifier is still repeated, but at least now changing the name of the variable with a refactoring tool will change the name used for logging too. It is now impossible to forget to update the name used in logging, since that would result in a compilation error ("unknown identifier").