Esempio n. 1
0
        /// <summary>
        ///  creates a new COMObject array
        /// </summary>
        /// <param name="caller">parent there have created comProxy</param>
        /// <param name="comProxyArray">new created proxy array</param>
        /// <returns>corresponding Wrapper class Instance array or plain COMObject array</returns>
        public static COMObject[] CreateObjectArrayFromComProxy(COMObject caller, object[] comProxyArray)
        {
            CheckInitialize();
            bool isLocked = false;

            try
            {
                if (null == comProxyArray)
                {
                    return(null);
                }

                if (Settings.EnableThreadSafe)
                {
                    Monitor.Enter(_comObjectLock);
                    isLocked = true;
                }

                Type        comVariantType  = null;
                COMObject[] newVariantArray = new COMObject[comProxyArray.Length];
                for (int i = 0; i < comProxyArray.Length; i++)
                {
                    comVariantType = comProxyArray[i].GetType();
                    IFactoryInfo factoryInfo   = GetFactoryInfo(comProxyArray[i]);
                    string       className     = TypeDescriptor.GetClassName(comProxyArray[i]);
                    string       fullClassName = factoryInfo.AssemblyNamespace + "." + className;
                    newVariantArray[i] = CreateObjectFromComProxy(factoryInfo, caller, comProxyArray[i], comVariantType, className, fullClassName);
                }
                return(newVariantArray);
            }
            catch (Exception throwedException)
            {
                DebugConsole.WriteException(throwedException);
                throw throwedException;
            }
            finally
            {
                if (isLocked)
                {
                    Monitor.Exit(_comObjectLock);
                    isLocked = false;
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// creates a new COMObject based on classType of comProxy
        /// </summary>
        /// <param name="caller">parent there have created comProxy</param>
        /// <param name="comProxy">new created proxy</param>
        /// <returns>corresponding Wrapper class Instance or plain COMObject</returns>
        public COMObject CreateObjectFromComProxy(COMObject caller, object comProxy)
        {
            CheckInitialize();
            bool isLocked = false;

            try
            {
                if (null == comProxy)
                {
                    return(null);
                }

                Monitor.Enter(_comObjectLock);
                isLocked = true;

                IFactoryInfo factoryInfo   = GetFactoryInfo(comProxy);
                string       className     = TypeDescriptor.GetClassName(comProxy);
                string       fullClassName = factoryInfo.AssemblyNamespace + "." + className;

                // create new proxyType
                Type comProxyType = null;
                if (false == _proxyTypeCache.TryGetValue(fullClassName, out comProxyType))
                {
                    comProxyType = comProxy.GetType();
                    _proxyTypeCache.Add(fullClassName, comProxyType);
                }

                COMObject newObject = CreateObjectFromComProxy(factoryInfo, caller, comProxy, comProxyType, className, fullClassName);
                return(newObject);
            }
            catch (Exception throwedException)
            {
                Console.WriteException(throwedException);
                throw throwedException;
            }
            finally
            {
                if (isLocked)
                {
                    Monitor.Exit(_comObjectLock);
                    isLocked = false;
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// creates a new COMObject based on classType of comProxy
        /// </summary>
        /// <param name="caller">parent there have created comProxy</param>
        /// <param name="comProxy">new created proxy</param>
        /// <param name="comProxyType">Type of comProxy</param>
        /// <returns>corresponding Wrapper class Instance or plain COMObject</returns>
        public static COMObject CreateObjectFromComProxy(COMObject caller, object comProxy, Type comProxyType)
        {
            CheckInitialize();
            bool isLocked = false;

            try
            {
                if (null == comProxy)
                {
                    return(null);
                }

                if (Settings.EnableThreadSafe)
                {
                    Monitor.Enter(_comObjectLock);
                    isLocked = true;
                }

                IFactoryInfo factoryInfo = GetFactoryInfo(comProxy);

                string className     = TypeDescriptor.GetClassName(comProxy);
                string fullClassName = factoryInfo.AssemblyNamespace + "." + className;

                // create new classType
                COMObject newObject = CreateObjectFromComProxy(factoryInfo, caller, comProxy, comProxyType, className, fullClassName);
                return(newObject);
            }
            catch (Exception throwedException)
            {
                DebugConsole.WriteException(throwedException);
                throw throwedException;
            }
            finally
            {
                if (isLocked)
                {
                    Monitor.Exit(_comObjectLock);
                    isLocked = false;
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Must be called from client assembly for COMVariant and COMObject Support
        /// Recieve FactoryInfos from all loaded LateBindingApi based Assemblies
        /// </summary>
        public static void Initialize()
        {
            _factoryList.Clear();
            Assembly callingAssembly = System.Reflection.Assembly.GetCallingAssembly();

            foreach (AssemblyName item in callingAssembly.GetReferencedAssemblies())
            {
                Assembly itemAssembly = Assembly.Load(item);
                object[] attributes   = itemAssembly.GetCustomAttributes(true);
                foreach (object itemAttribute in attributes)
                {
                    string fullnameAttribute = itemAttribute.GetType().FullName;
                    if (fullnameAttribute == "LateBindingApi.Core.LateBindingAttribute")
                    {
                        Type         factoryInfoType = itemAssembly.GetType(item.Name + ".Utils.FactoryInfo");
                        IFactoryInfo factoryInfo     = Activator.CreateInstance(factoryInfoType) as IFactoryInfo;
                        _factoryList.Add(factoryInfo);
                    }
                }
            }
        }
Esempio n. 5
0
        public static Type GetObjectType(object comProxy)
        {
            CheckInitialize();

            if (null == comProxy)
            {
                return(null);
            }
            else
            {
                IFactoryInfo factoryInfo   = GetFactoryInfo(comProxy);
                string       className     = TypeDescriptor.GetClassName(comProxy);
                string       fullClassName = factoryInfo.AssemblyNamespace + "." + className;
                Type         proxyType     = null;
                if (!_proxyTypeCache.TryGetValue(fullClassName, out proxyType))
                {
                    proxyType = comProxy.GetType();
                    _proxyTypeCache.Add(fullClassName, proxyType);
                }
                return(proxyType);
            }
        }
Esempio n. 6
0
        /// <summary>
        ///  creates a new COMVariant based on type of comVariant
        /// </summary>
        /// <param name="caller"></param>
        /// <param name="comVariant"></param>
        /// <returns></returns>
        public static COMVariant CreateVariantFromComProxy(COMObject caller, object comVariant)
        {
            if (null == comVariant)
            {
                return(null);
            }

            Type comVariantType = comVariant.GetType();

            if (false == comVariantType.IsCOMObject)
            {
                COMVariant newVariant = new COMVariant(caller, comVariant, comVariantType);
                return(newVariant);
            }
            else
            {
                IFactoryInfo factoryInfo   = GetFactoryInfo(comVariant);
                string       className     = TypeDescriptor.GetClassName(comVariant);
                string       fullClassName = factoryInfo.Namespace + "." + factoryInfo.Prefix + className;
                COMObject    newObject     = CreateObjectFromComProxy(factoryInfo, caller, comVariant, comVariantType, className, fullClassName);
                return(newObject);
            }
        }
Esempio n. 7
0
        private static COMObject CreateObjectFromComProxy(IFactoryInfo factoryInfo, COMObject caller, object comProxy, Type comProxyType, string className, string fullClassName)
        {
            Type classType = null;

            if (true == _typeCache.TryGetValue(fullClassName, out classType))
            {
                // cached classType
                object newClass = Activator.CreateInstance(classType, new object[] { caller, comProxy });
                return((COMObject)newClass);
            }
            else
            {
                // create new classType
                classType = factoryInfo.Assembly.GetType(fullClassName);
                if (null == classType)
                {
                    throw new ArgumentException("Class not exists: " + fullClassName);
                }

                _typeCache.Add(fullClassName, classType);
                object newClass = Activator.CreateInstance(classType, new object[] { caller, comProxy, comProxyType });
                return((COMObject)newClass);
            }
        }
Esempio n. 8
0
        /// <summary>
        /// creates a new COMObject from factoryInfo
        /// </summary>
        /// <param name="factoryInfo">Factory Info from Wrapper Assemblies</param>
        /// <param name="caller">parent there have created comProxy</param>
        /// <param name="comProxy">new created proxy</param>
        /// <param name="comProxyType">Type of comProxy</param>
        /// <param name="className">name of COMServer proxy class</param>
        /// <param name="fullClassName">full namespace and name of COMServer proxy class</param>
        /// <returns>corresponding Wrapper class Instance or plain COMObject</returns>
        public COMObject CreateObjectFromComProxy(IFactoryInfo factoryInfo, COMObject caller, object comProxy, Type comProxyType, string className, string fullClassName)
        {
            CheckInitialize();
            bool isLocked = false;
            try
            {
                Monitor.Enter(_comObjectLock);
                isLocked = true;

                Type classType = null;
                if (true == _wrapperTypeCache.TryGetValue(fullClassName, out classType))
                {
                    // cached classType
                    object newClass = Activator.CreateInstance(classType, new object[] { caller, comProxy });
                    return newClass as COMObject;
                }
                else
                {
                    // create new classType
                    classType = factoryInfo.Assembly.GetType(fullClassName, false, true);
                    if (null == classType)
                        throw new ArgumentException("Class not exists: " + fullClassName);

                    _wrapperTypeCache.Add(fullClassName, classType);
                    COMObject newClass = Activator.CreateInstance(classType, new object[] { caller, comProxy, comProxyType }) as COMObject;
                    return newClass;
                }
            }
            catch (Exception throwedException)
            {
                Console.WriteException(throwedException);
                throw throwedException;
            }
            finally
            {
                if (isLocked)
                {
                    Monitor.Exit(_comObjectLock);
                    isLocked = false;
                }
            }
        }