which I know can have references of VideoControl,HtmlContr">

Dynamic Objects Call relative functionality everywhere to extend a liabrary C#

by Technacron   Last Updated April 27, 2018 07:05 AM

I have different types of Objects under the same Parent class "Control". I have a List< Control > which I know can have references of VideoControl,HtmlControl etc. Which all are 'Control'. Debugging also shows that runtime cast is properly parsing them into relative Control and all other specific properties are present.

Now these Control objects are included in two projects, and I know using signalR I will receive a serialized list of Control objects of multiple types.

I want to call a Draw function on every control but in a way that my Xamarin.Android project knows how to Draw wach control in it's own way.

          foreach (Control control in listControls)
            {
                try
                {

                   control.Draw(this, myLayout);
                    // this will call relative method for videoPlayer
                   //((VideoPlayer)control).Render(this, myLayout);
                }
                catch (Exception ex)
                {
                }
            }

P.S. Draw is defined in an Interface so it is imposed to every control. My problem is I want to enable all projects to extend or provide functionality on their own and Draw each control with my encapsulated data.

I have searched alot and found one possible way i.e Extension Methods but again the problem is I have to do explicit type cast which will again make my code lengthy and difficult to handle.

For further assistance , I am adding my Extension methods if they can be handled without explicit type casting.

    public static void Render(this Control Player, object context, object container)
    {
    }

    public static void Render(this VideoPlayer Player, object context, object container)
    {

    }
    public static void Render(this HTMLControl Player, object context, object container)
    {
    }
    public static void Render(this Button Player, object context, object container)
    {
    }

If I do not type cast then every time my extension method for Type Control will run but I want to run relative method for each object.

What will be the possible way to handle this either some partial implementation or some extension methods or some other way.



Related Questions




Is static typing worth the trade-offs?

Updated April 14, 2018 18:05 PM