0

I have the following interface in an assembly Base.dll

    public interface IMessageHander<in T>{
        void Handle(T msg);
    }

    public class MyBase<TChild> where TChild: MyBase<TChild>{
        private TChild Self => this as TChild;

        void CallMethods(){
            // how to call implemented interfaces handlers??
            // Self.Handle(msg1); 
            // Self.Handle(msg2)
        }
    }

Someone references Base.dll and writes the following code

    public class Msg1{}
    public class Msg2{}
    public class Msg3{}

    public class MyHandler: IMessageHander<Msg1>, 
       IMessageHander<Msg2>, 
       IMessageHander<Msg3>
    {
        void Handle(Msg1 msg){}
        void Handle(Msg2 msg){}
        void Handle(Msg3 msg){}
    }

In Base.Dll through reflection I'm able to find out what interfaces are implemented and what are their generic types being passed i.e. all the message types and their corresponding handlers. I want to be able to call them using the Expression.Lambda. How would I do that?

I don't want to use reflection which I already know. Please see this question for the background How to create object dynamically

I receive the object to be passed as message in the form of base object only.

Simple Fellow
  • 4,315
  • 2
  • 31
  • 44
  • 1
    I have absolutely no idea what you want to achieve and why you want to do that using `Expression.Lambda`. Can you describe the problem in more detail? – Krzysztof Nov 14 '19 at 11:36
  • It's unclear how `MyBase` is used. Did you mean to have `MyHandler` inherit from `MyBase`? – canton7 Nov 16 '19 at 12:19
  • It's also not clear where `msg1` and `msg2` come from in `CallMethods` – canton7 Nov 16 '19 at 12:22
  • msg1 & msg2 are any immutable classes that are used for passing data. anyone using the library would have their own immutable classes as messages with a particular attribute, Message(Type=Command) ]. I can pick the messages at runtime through reflection and create a map. the value of the message keys would be the handlers themselves. so to implement a handle they'd implement an interface IMessageHandler which would require them to have Handle(Msg1 msg); – Simple Fellow Nov 17 '19 at 13:39
  • I still don't see any use for `Expression.Lambda` here. Let's assume you have `Msg1` and want to execute all handlers, then you can just check handler using `is` operator: `handler is IMessageHandler`. – Krzysztof Nov 19 '19 at 07:39

0 Answers0