简体   繁体   English

具有强类型对象的事件回调

[英]Event Callback with Strongly Typed Object

I'm trying to get a proof of concept down for a feature I need in my code. 我正在尝试为我的代码中需要的功能提供概念证明。 In the real application, I have a Dependency Injection container, and children sometimes need to 'new up' an object, and I need the DI container to return that object. 在实际的应用程序中,我有一个依赖注入容器,而子代有时需要“新建”一个对象,并且我需要DI容器来返回该对象。 So I'm using an event with a callback that lets the child get that instatiated object. 所以我正在使用一个带有回调的事件,该回调使孩子能够获得该实例化的对象。

The problem is, I'd like one event, but allow it to return whichever object the caller specifies. 问题是,我想要一个事件,但是允许它返回调用者指定的任何对象。 I'm not sure how to get that strongly typed object back (without casting). 我不确定如何获取强类型对象(不强制转换)。 I feel like it should be possible. 我觉得应该有可能。 If that's confusing enough, here's my example: 如果这足够令人困惑,这是我的示例:

class Program
{
    public static event EventHandler<MyEventArgs> MyEvent;
    public static void Main()
    {
        MyEvent += (sender, args) =>
        {
            Console.WriteLine("Event Fired");
            args.Callback("hello");
        };


        MyEvent(null, new MyEventArgs(obj => Console.WriteLine($"How do I get {obj} strongly typed")));

        Console.Read();
    }
}

and the event args: 和事件参数:

public class MyEventArgs
{
    public Action<object> Callback { get; set; }

    public MyEventArgs(Action<object> callback)
    {
        Callback = callback;
    }
}

I'm stuck, because MyEventArgs can't take a type parameter, as I would really like just one event to handle any return object type. 我被困住了,因为MyEventArgs不能接受类型参数,因为我真的只希望一个事件来处理任何返回对象类型。 Is it possible to get a strongly typed object through the callback? 是否可以通过回调获取强类型对象?

Again, I could send the Type through as a parameter, and cast it, but I feel like this should be do-able. 同样,我可以将Type作为参数发送并进行转换,但是我觉得这应该可行。 It's entirely possible that this is impossible, too. 这完全有可能也是不可能的。

Thanks for any guidance. 感谢您的指导。

You probably would like to take a look at dynamic types . 您可能想看一下动态类型

public class MyEventArgs 
{
    public Action<dynamic> Callback { get; set; }

    public MyEventArgs(Action<dynamic> callback)
    {
        Callback = callback;
    }
}

It will allow you to pass any type to callback. 它将允许您将任何类型传递给回调。

I worked out a solution that is almost exactly what I was going for: 我制定了几乎与我想要的解决方案相同的解决方案:

public class MyEventArgs<T> : MyEventArgs where T : class
{
    private Action<T> _callback;
    public MyEventArgs(Action<T> callback)
    {
        _callback = callback;
    }

    public override void DoCallback(object item)
    {
        _callback(item as T);
    }
}

public abstract class MyEventArgs
{
    public abstract void DoCallback(object item);
}

and in my Main: 在我的主目录中:

public static event EventHandler<MyEventArgs> MyEvent;
public static void Main()
{
    MyEvent += (sender, args) =>
    {
        Console.WriteLine("Event Fired");
        args.DoCallback("hello");
    };

    MyEvent(null, new MyEventArgs<string>(s => Console.WriteLine($"{s} is a string")));

 Console.Read();
}

All it's missing is DoCallback()'s parameter is not strongly typed. 缺少的只是DoCallback()'s参数不是强类型的。 But it's exremely close to what I wanted. 但这与我想要的非常接近。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM