Example #1
0
        public object InvokeMethod(ISynchronizeInvoke obj, string objId, string methodName, params object[] paramValues)
        {
            lock (_lockObj)
            {
                try
                {
                    Delegate del           = null;
                    Type     invokeeType   = obj.GetType();
                    string   typeMethodKey = string.Format(DelegateMethodFmt, invokeeType.FullName, methodName);

                    // #1. Check and prepare the Delegate Type map
                    Type delType = null;
                    if (_delegateTypeMap.ContainsKey(typeMethodKey))
                    {
                        delType = _delegateTypeMap[typeMethodKey];
                    }
                    else
                    {
                        delType = _delegatesBuilder.BuildDelegateType(invokeeType, methodName);
                        if (delType == null)
                        {
                            throw new Exception(string.Format(BindMethodFailureMsgFmt, methodName));
                        }

                        _delegateTypeMap.Add(typeMethodKey, delType);
                    }

                    // #2. Check and prepare the Delegate instance map
                    if (!_delegateInstanceMap.ContainsKey(typeMethodKey))
                    {
                        _delegateInstanceMap.Add(typeMethodKey, new Dictionary <string, Delegate>());
                    }

                    // #3. Check if instance needs added.
                    string objKey = objId;
                    Dictionary <string, Delegate> objMap = _delegateInstanceMap[typeMethodKey];
                    if (objMap.ContainsKey(objKey))
                    {
                        del = objMap[objKey];
                    }
                    else
                    {
                        del = MulticastDelegate.CreateDelegate(delType, obj, methodName);
                        objMap.Add(objKey, del);
                    }

                    return(obj.Invoke(del, paramValues));
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }