locked
How to write a Event-Delegate using Reflection and Compact Framework 2.0 RRS feed

  • Question

  • Hello,

    i'm looking for a way to implement (rewrite) the following lines by using Compact Framework 2.0 !!!, reflection and loading the Assembly Reference Symbol.ResourceCoordination.dll during Runtime. 

    Instance + Event-Deligate

              Symbol.ResourceCoordination.Trigger trigger = new Trigger();
              trigger.Stage2Notify += new Symbol.ResourceCoordination.Trigger.TriggerEventHandler(Trigger_Stage2Notify);

    Handler Target

    void Trigger_Stage2Notify(object sender, Symbol.ResourceCoordination.TriggerEventArgs e)
        {
    //do something
    }

    I have tried to do this:

    Type triggerType = asm.GetType("Symbol.ResourceCoordination.Trigger, Symbol.ResourceCoordination, 2.5.0.3, Culture=neutral, PublicKeyToken=68EC8DB391F150CA");
    object trigger = Activator.CreateInstance(triggerType );
    triggerType.GetEvent("Stage2Notify").AddEventHandler(???)
    
    

    So i'm not sure how to implement the delegate for the AddEventHandler especially the Type of the Handler itselfs and there EventArgs (that i need to get the Current-Trigger Value) 

    Symbol.ResourceCoordination.Trigger.TriggerEventHandler
    Symbol.ResourceCoordination.TriggerEventArgs

    Thanks for any advice

    Thursday, September 5, 2013 10:11 AM

Answers

  • No, there's none. This functionality is supported in NETCF 3.5 though.

    Personally I would not bother with that even if everything would be supported. Instead I would just package everything together and be done with it.

    The benefits (less code, more reliable, better performance, lower support cost, etc.) far outweigh some size reduction (which I believe you're after?) IMO.

    I had some experience with setup and deployment and trust me - you generally don't want to choose any solution which require multiple packages vs. a solution with a single package.

    Another things you may try:

    1. Wrap all the device specific functionality into set of your own DLLs using same public interfaces. These you can load at run time.

    2. Try excluding unneeded DLLs from deployment and see if they won't be loaded unless used (even if referenced). For that you probably would need the wrapper DLL as well.


    This posting is provided "AS IS" with no warranties, and confers no rights.

    • Marked as answer by tronix01 Tuesday, September 10, 2013 6:41 AM
    Friday, September 6, 2013 3:59 PM
  • You would not as you know the type at compile time.

    That is, instead of "object" you would have some interface, e.g. IHardwareWrapper which has a known event, e.g, if type WhateverEventHander.

    So it would look like this:

    IHardwareWrapper  hardwareWrapper = LoadDllForHardwareAndCreateInstance();

    hardwareWrapper.WhateverEvent += new WhateverEventHander(hander);


    To accomplish this you would need to create another DLL which defines all these universal types so it can be referenced in your main project and all the hardware specific DLLs.


    This posting is provided "AS IS" with no warranties, and confers no rights.


    Monday, September 9, 2013 4:56 PM

All replies

  • Why do you need to do that? Are these not public?

    In any case I don't think there's a way for you to create an event handler method via reflections since NETCF 2.0 does not have support for either Delegate.CreateDelegate method or Reflection.Emit. Both would be needed to create a handler of a type unknown at compile time. 

    Perhaps you may be able to bypass Reflection.Emit by declaring a generic class which has a handler method with needed signature and then using Type.MakeGenericType() to bind it to parameters needed... However since you cannot create a delegate it's no use.

    If you wondering what is the process for that you can check it here:

    http://msdn.microsoft.com/en-us/library/ms228976.aspx


    This posting is provided "AS IS" with no warranties, and confers no rights.

    Thursday, September 5, 2013 4:49 PM
  • I need to do that, because there is one Codebasis for different mobile Computers (Motorola, Intermec, tec) and i don't wan't to Setup all Device-Specific-Dll's on each Device. My idea is to use only these Device-Specific-Dll's they are related to a specific Device. (e.g. Motorola Dlls at Motorola Devices, Intermec Dll's at Intermec Devices, etc)

    So i start to remove all my static Assembly references and try to load / use them during the Runtime by using Reflection. 

    I'm right with, there is no way to get a similar functionallity like Delegate.CreateDelegate in NETCF 2.0 ?

    Here is a Overview about the Event:

    Overview Trigger Class:

    Thx for any advice

    Friday, September 6, 2013 7:36 AM
  • No, there's none. This functionality is supported in NETCF 3.5 though.

    Personally I would not bother with that even if everything would be supported. Instead I would just package everything together and be done with it.

    The benefits (less code, more reliable, better performance, lower support cost, etc.) far outweigh some size reduction (which I believe you're after?) IMO.

    I had some experience with setup and deployment and trust me - you generally don't want to choose any solution which require multiple packages vs. a solution with a single package.

    Another things you may try:

    1. Wrap all the device specific functionality into set of your own DLLs using same public interfaces. These you can load at run time.

    2. Try excluding unneeded DLLs from deployment and see if they won't be loaded unless used (even if referenced). For that you probably would need the wrapper DLL as well.


    This posting is provided "AS IS" with no warranties, and confers no rights.

    • Marked as answer by tronix01 Tuesday, September 10, 2013 6:41 AM
    Friday, September 6, 2013 3:59 PM
  • Ilya you are right but this decision is not depending on my opinion (i would prever the easiest way as well), so i need try to follow your Point 1. and wrap all the device specific functionality into set of my own DLLs using same public interfaces and load the dll at runtime using reflection, How could i solve the Delegate.CreateDelegate issue ? Do i not run into the same Problems ?

    Thx again

    Monday, September 9, 2013 7:36 AM
  • You would not as you know the type at compile time.

    That is, instead of "object" you would have some interface, e.g. IHardwareWrapper which has a known event, e.g, if type WhateverEventHander.

    So it would look like this:

    IHardwareWrapper  hardwareWrapper = LoadDllForHardwareAndCreateInstance();

    hardwareWrapper.WhateverEvent += new WhateverEventHander(hander);


    To accomplish this you would need to create another DLL which defines all these universal types so it can be referenced in your main project and all the hardware specific DLLs.


    This posting is provided "AS IS" with no warranties, and confers no rights.


    Monday, September 9, 2013 4:56 PM