CHIZOR Contact me at support [at] chizor [dot] com

Dynamic Generic Member Invoking (Example)



In order to do dynamic generic member invoking, you must do one of the 2 thing:
  1. Inherit from abstract class BaseDynamic, or
  2. Inherit from one, or more, of the interfaces listed below when inheriting from BaseDynamic is not an option, but you still want your object to participate in Dynamics infrastructure:
    • IBaseDynamicCommon - This interface represent Microsoft's existing dynamic infrastructure
    • IBaseDynamicGenericX - This interface implements the generic version of member invocation and object invocation.

      "X" - stands for a number between 1...16

      For example, IBaseDynamicGeneric4 is an interface that let's you handle 4 generic parameters in Dynamics.

    • IBaseDynamic - This interface inherits the IBaseDynamicCommon, and all of the IBaseDynamicGenericX interfaces.


Example of Dynamic Generic Member Invoking By Inheriting From BaseDynamic class
        //First let your object inherit from abstract base class BaseDynamic, then override one of the TryInvokeGenericMember functions

        public class Dog : BaseDynamic
        {
            public override bool TryInvokeGenericMember<Param1>(InvokeMemberBinder binder, object[] args, out object result)
            {
                //Do some code
            }
        }

        //Then in your code elsewhere....

        dynamic puppy = new Dog();

        puppy.Mate<Pitbull>();
    


Example of Dynamic Generic Member Invoking By Inheriting From One of the "base dynamic" interfaces, then "activating" dynamics on the object.
        //First let your object inherit from one of the "IBaseDynamic" interfaces, then implement the associated TryInvokeGenericMember function

        public class Dog : IBaseDynamicGeneric2
        {
            public bool TryInvokeGenericMember<Param1, Param2>(InvokeMemberBinder binder, object[] args, out object result)
            {
                //Do some code
            }

            bool TryInvokeGeneric<Param1, Param2>(InvokeBinder binder, object[] args, out object result)
            {
                //Do some code
            }

        }

        //Then in your code elsewhere use must "activate" the dynamic infrastructure for your object.

        Dog puppy = new Dog();
        IBaseDynamic ipuppy = BaseDynamic.ActivateDynamics(puppy);

        ((dynamic)ipuppy).Mate<Pitbull, GermanShepherd>();
    
Note that the BaseDynamic.ActivateDynamics() function returns an IBaseDynamic interface. This is because IBaseDynamic interface inherits from all the other "a la carte" dynamic generic interfaces.

◀◀ BACK