Click here to Skip to main content
15,885,366 members
Articles / Desktop Programming / Windows Forms

Integrating WCF Services

Rate me:
Please Sign up or sign in to vote.
4.84/5 (12 votes)
24 Apr 2008CPOL3 min read 44.9K   483   46  
This article describes how WCF services can be loaded on-the-fly (without prior knowledge of the services’ contracts), setting its parameters and sequencing their invocation.
#region Copyright Notice
// ----------------------------------------------------------------------------
// Copyright (C) 2006 Microsoft Corporation, All rights reserved.
// ----------------------------------------------------------------------------

// Author: Vipul Modi (vipul.modi@microsoft.com)
#endregion

namespace Integrator.Framework.DynamicProxyModel
{
    using System;
    using System.Reflection;

    public class DynamicObject
    {
        private Type objType;
        private object obj;

        private BindingFlags CommonBindingFlags = BindingFlags.Instance | BindingFlags.Public;
         
        public DynamicObject(Object obj)
        {
            if (obj == null) 
                throw new ArgumentNullException("obj");

            this.obj = obj;
            this.objType = obj.GetType();
        }

        public DynamicObject(Type objType)
        {
            if (objType == null)
                throw new ArgumentNullException("objType");

            this.objType = objType;
        }

        public void CallConstructor()
        {
            CallConstructor(new Type[0], new object[0]);
        }

        public void CallConstructor(Type[] paramTypes, object[] paramValues)
        {
            ConstructorInfo ctor = this.objType.GetConstructor(paramTypes);
            if (ctor == null)
            {
                throw new DynamicProxyException(Constants.ErrorMessages.ProxyCtorNotFound);            
            }

            this.obj = ctor.Invoke(paramValues);
        }

        public object GetProperty(string property)
        {
            object retval = this.objType.InvokeMember(
                property,
                BindingFlags.GetProperty | CommonBindingFlags,
                null /* Binder */,
                this.obj,
                null /* args */);

            return retval;
        }

        public object SetProperty(string property, object value)
        {
            object retval = this.objType.InvokeMember(
                property,
                BindingFlags.SetProperty | CommonBindingFlags,
                null /* Binder */,
                this.obj,
                new object[] { value });

            return retval;
        }

        public object GetField(string field)
        {
            object retval = this.objType.InvokeMember(
                field,
                BindingFlags.GetField | CommonBindingFlags,
                null /* Binder */,
                this.obj,
                null /* args */);

            return retval;
        }

        public object SetField(string field, object value)
        {
            object retval = this.objType.InvokeMember(
                field,
                BindingFlags.SetField | CommonBindingFlags,
                null /* Binder */,
                this.obj,
                new object[] { value });

            return retval;
        }

        public object CallMethod(string method, params object[] parameters)
        {
            object retval = this.objType.InvokeMember(
                method,
                BindingFlags.InvokeMethod | CommonBindingFlags,
                null /* Binder */,
                this.obj,
                parameters /* args */);

            return retval;
        }

        public object CallMethod(string method, Type[] types, object[] parameters)
        {
            if (types.Length != parameters.Length)
                throw new ArgumentException(Constants.ErrorMessages.ParameterValueMistmatch);

            MethodInfo mi = this.objType.GetMethod(method, types);
            if (mi == null) 
                throw new ApplicationException(string.Format( Constants.ErrorMessages.MethodNotFound, method));

            object retval = mi.Invoke(this.obj, CommonBindingFlags, null, parameters, null);

            return retval;
        }

        public Type ObjectType
        {
            get
            {
                return this.objType;
            }
        }

        public object ObjectInstance
        {
            get
            {
                return this.obj;
            }
        }

        public BindingFlags BindingFlags
        {
            get
            {
                return this.CommonBindingFlags;
            }

            set
            {
                this.CommonBindingFlags = value;
            }
        }
    }
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Singapore Singapore
Yes, I design. Then, I code. Next, I refactor.

Comments and Discussions