没有参数的方法如何分配给 ExpandoObject?

发布于 2024-10-26 12:46:03 字数 776 浏览 1 评论 0原文

我正在尝试使用以下签名将方法(函数)分配给 ExpandoObject:

public List<string> CreateList(string input1, out bool processingStatus)
{
  //method code...
}

我尝试执行类似以下代码的操作,但该代码无法编译:

dynamic runtimeListMaker = new ExpandoObject();
runtimeListMaker.CreateList =
     new Func<string, bool, List<string>>(
           (input1, out processingStatus) =>
                {
                     var newList = new List<string>();

                     //processing code...

                     processingStatus = true;
                     return newList;
                });

不幸的是,我无法更改 CreateList 签名,因为它会破坏向后兼容性,因此需要重写这不是一个选择。我尝试通过使用委托来解决此问题,但在运行时,我收到“无法调用非委托类型”异常。我想这意味着我没有正确分配委托。我需要帮助使语法正确(委托示例也可以)。谢谢!!

I'm trying to assign a method (function) to an ExpandoObject with this signature:

public List<string> CreateList(string input1, out bool processingStatus)
{
  //method code...
}

I've tried to do something like this code below which doesn't compile:

dynamic runtimeListMaker = new ExpandoObject();
runtimeListMaker.CreateList =
     new Func<string, bool, List<string>>(
           (input1, out processingStatus) =>
                {
                     var newList = new List<string>();

                     //processing code...

                     processingStatus = true;
                     return newList;
                });

Unfortunately I cannot change the CreateList signature because it will break backward compatibility so rewriting it is not an option. I've tried to get around this by using delegates but at runtime, I got an "Cannot invoke a non-delegate type" exception. I guess this means I'm not assigning the delegate correctly. I need help getting the syntax correct (delegate examples are OK too). Thanks!!

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

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

发布评论

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

评论(2

乜一 2024-11-02 12:46:03

该示例按预期编译并运行:

dynamic obj = new ExpandoObject();
obj.Method = new Func<int, string>((i) =>
    {
        Console.WriteLine(i);
        return "Hello World";
    });

obj.Method(10);
Console.ReadKey();

您的语句的问题是您的 Func 没有像您的签名那样使用输出参数。

(input1, out processingStatus)

如果你想分配当前的方法,你将无法使用 Func,但你可以创建你自己的委托:

    public delegate List<int> MyFunc(int input1, out bool processing);

    protected static void Main(string[] args)
    {
        dynamic obj = new ExpandoObject();
        obj.Method = new MyFunc(Sample);

        bool val = true;
        obj.Method(10, out val);
        Console.WriteLine(val);
        Console.ReadKey();
    }

    protected static List<int> Sample(int sample, out bool b)
    {
        b = false;
        return new List<int> { 1, 2 };
    }

This sample compiles and runs as expected:

dynamic obj = new ExpandoObject();
obj.Method = new Func<int, string>((i) =>
    {
        Console.WriteLine(i);
        return "Hello World";
    });

obj.Method(10);
Console.ReadKey();

The problem with your statement is that your Func does not use an output parameter like your signature does.

(input1, out processingStatus)

If you want to assign your current method, you won't be able to use Func, but you CAN create your own delegate:

    public delegate List<int> MyFunc(int input1, out bool processing);

    protected static void Main(string[] args)
    {
        dynamic obj = new ExpandoObject();
        obj.Method = new MyFunc(Sample);

        bool val = true;
        obj.Method(10, out val);
        Console.WriteLine(val);
        Console.ReadKey();
    }

    protected static List<int> Sample(int sample, out bool b)
    {
        b = false;
        return new List<int> { 1, 2 };
    }
长梦不多时 2024-11-02 12:46:03

代码中使用 out 参数类型作为 processingStatus 参数的问题 在 C# 中不允许

The problem in your code that using out parameter type for processingStatus parameter which is not allowed in C#.

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