Click here to Skip to main content
15,895,667 members
Articles / Programming Languages / C#

Dynamic Generation of Interfaces Using a Pool of Objects

Rate me:
Please Sign up or sign in to vote.
4.64/5 (9 votes)
3 Oct 20075 min read 32.6K   147   34  
An article introducing a mechanism for on-the-fly interface generation
using System;
using System.Collections.Generic;
using System.Text;

namespace RemotingClient
{
    class Program
    {
        static void Main(string[] args)
        {
            CommonInterfaces.IBridge bridge =
                System.Activator.GetObject(
                    typeof(CommonInterfaces.IBridge),
                    CommonInterfaces.Constants.BRIDGE_URL
                )
                    as CommonInterfaces.IBridge;

            /* first option: use a compile-time interface defined in 'CommonInterfaces' */
            CommonInterfaces.IDummyServer dummy = bridge.GetServiceProvider<CommonInterfaces.IDummyServer>();
            dummy.PrintIAmDummyToConsole();
            dummy.PrintNumber(2);
            dummy.PrintObjectParamArray(1, 2, 3);
            dummy.PrintNumber(3);

            /* second option: define a dynamic interface, and check whether it can be generated for you... */
            CommonInterfaces.DynamicInterface dynamicInterface =
                bridge.GetServiceProvider(
                    new CommonInterfaces.MethodDescriptor("PrintIAmDummyToConsole", System.Reflection.CallingConventions.Standard | System.Reflection.CallingConventions.HasThis, Type.GetType("System.Void"), Type.EmptyTypes),
                    new CommonInterfaces.MethodDescriptor("PrintIAmSmartToConsole", System.Reflection.CallingConventions.Standard | System.Reflection.CallingConventions.HasThis, Type.GetType("System.Void"), Type.EmptyTypes),
                    new CommonInterfaces.MethodDescriptor("PrintObjectParamArray", System.Reflection.CallingConventions.HasThis | System.Reflection.CallingConventions.Standard, true, Type.GetType("System.Void"), typeof(object[])),
                    new CommonInterfaces.MethodDescriptor("PrintNumberArray", Type.GetType("System.Void"), typeof(int[])),
                    new CommonInterfaces.MethodDescriptor("PrintNumber", Type.GetType("System.Void"), typeof(int)),
                    new CommonInterfaces.MethodDescriptor("PrintStringAndNumberParamArray", System.Reflection.CallingConventions.HasThis | System.Reflection.CallingConventions.Standard, true, typeof(object), typeof(string), typeof(int[])),
                    new CommonInterfaces.MethodDescriptor("GetName", typeof(string)),
                    new CommonInterfaces.MethodDescriptor("GetPluginNumber", typeof(int))
                );
            if (dynamicInterface != null)
            {
                dynamicInterface["PrintIAmDummyToConsole"].Invoke();
                dynamicInterface["PrintIAmSmartToConsole"].Invoke();
                dynamicInterface["PrintNumber"].Invoke(2);
                dynamicInterface["PrintNumberArray"].Invoke(new int[] { 1, 2, 3 });
                dynamicInterface["PrintObjectParamArray"].Invoke();
                dynamicInterface["PrintObjectParamArray"].Invoke(4, 5, 6);
                dynamicInterface["PrintStringAndNumberParamArray"].Invoke("Wow", 7, 8, 9, 10);
                dynamicInterface["GetName"].Invoke();
                dynamicInterface["GetPluginNumber"].Invoke();
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Israel Israel
I am a software engineer, as well as a research student at The Hebrew University. I love coding, math, computer theory, reading, tennis, guitar, swimming, animals, and - most importantly - my wife and family.

Comments and Discussions