如何在 C# 中从字符串调用委托?

发布于 2024-11-07 12:56:18 字数 692 浏览 0 评论 0原文

是否可以通过变量名称(作为字符串)调用存储在变量中的委托?我想我必须使用反射机制,但我没有得到任何结果

示例代码:

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 技术交流群。

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

发布评论

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

评论(5

面犯桃花 2024-11-14 12:56:18

变量几乎不存在。可靠地按字符串调用(在这种情况下)的唯一方法是将委托存储在字典中:

Dictionary<string, DemoDelegate> calls = new Dictionary<string, DemoDelegate>
{
    {"one",one}, {"two",two}
}

现在将该字典存储在某处(在字段中,通常),并执行以下操作:

private int CallDelegate(string name) {
    return calls[name].Invoke(); // <==== args there if needed
}

Variables barely exist. The only way to reliably call-by-string (in this scenario) would be to store the delegates in a dictionary:

Dictionary<string, DemoDelegate> calls = new Dictionary<string, DemoDelegate>
{
    {"one",one}, {"two",two}
}

Now store that dictionary somewhere (in a field, typically), and do something like:

private int CallDelegate(string name) {
    return calls[name].Invoke(); // <==== args there if needed
}
策马西风 2024-11-14 12:56:18

是的,这是可能的,只要你使用 Linq 表达式,并且很少进行反射。

看看这段代码,它的功能与我认为您想要的类似:

using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Collections.Generic;

namespace q6010555
{
    class Demo
    {
        static List<string> varNamesUsed = new List<string>();

        public delegate int DemoDelegate();

        private static int One()
        {
            return 1;
        }
        private static void CallDelegate(Expression<Func<DemoDelegate>> expr)
        {
            var lambda = expr as LambdaExpression;
            var body = lambda.Body;
            var field = body as MemberExpression;
            var name = field.Member.Name;
            var constant = field.Expression as ConstantExpression;
            var value = (DemoDelegate)((field.Member as FieldInfo).GetValue(constant.Value));

            // now you have the variable name... you may use it somehow!
            // You could log the variable name.
            varNamesUsed.Add(name);

            value();
        }
        static void Main(string[] args)
        {
            DemoDelegate one = Demo.One;
            CallDelegate(() => one);

            // show used variable names
            foreach (var item in varNamesUsed)
                Console.WriteLine(item);
            Console.ReadKey();
        }
    }
}

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:

using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Collections.Generic;

namespace q6010555
{
    class Demo
    {
        static List<string> varNamesUsed = new List<string>();

        public delegate int DemoDelegate();

        private static int One()
        {
            return 1;
        }
        private static void CallDelegate(Expression<Func<DemoDelegate>> expr)
        {
            var lambda = expr as LambdaExpression;
            var body = lambda.Body;
            var field = body as MemberExpression;
            var name = field.Member.Name;
            var constant = field.Expression as ConstantExpression;
            var value = (DemoDelegate)((field.Member as FieldInfo).GetValue(constant.Value));

            // now you have the variable name... you may use it somehow!
            // You could log the variable name.
            varNamesUsed.Add(name);

            value();
        }
        static void Main(string[] args)
        {
            DemoDelegate one = Demo.One;
            CallDelegate(() => one);

            // show used variable names
            foreach (var item in varNamesUsed)
                Console.WriteLine(item);
            Console.ReadKey();
        }
    }
}
女中豪杰 2024-11-14 12:56:18

您无法真正访问另一个堆栈框架中的变量(尽管我认为可以在 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 an Delegate object around and use methods like DynamicInvoke, if you want to invoke a generalised delegate in a reflection-like manner.

旧街凉风 2024-11-14 12:56:18
public void Fire(string name)
        {
            FieldInfo field = this.GetType().GetField(name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

            if (field != null)
            {
                Delegate method = field.GetValue(this) as Delegate;

                if (method != null)
                {
                    method.Method.Invoke(method.Target, new object[0]);
                }
            }
        }

显然限制您拥有参数化委托。

public void Fire(string name)
        {
            FieldInfo field = this.GetType().GetField(name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

            if (field != null)
            {
                Delegate method = field.GetValue(this) as Delegate;

                if (method != null)
                {
                    method.Method.Invoke(method.Target, new object[0]);
                }
            }
        }

Obviously restricts you from having parameterized delegates.

爱人如己 2024-11-14 12:56:18

我不记得十多年前我想实现什么目标,但现在读到这个问题,我想提出一个潜在的解决方法。该解决方法并不能完全避免重复,但它摆脱了手动字符串化变量名称的情况。

nameof 表达式 允许生成变量、类型或成员的名称。

static void main(string[] args) {
  DemoDelegate one = Demo.One;
  CallDelegate(one);
  CallDelegate(nameof(one), one);
}

标识符仍然重复,但至少现在使用重构工具更改变量的名称也会更改用于日志记录的名称。现在不可能忘记更新日志记录中使用的名称,因为这会导致编译错误(“未知标识符”)。

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.

static void main(string[] args) {
  DemoDelegate one = Demo.One;
  CallDelegate(one);
  CallDelegate(nameof(one), one);
}

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").

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