Beispiel #1
0
        /*****************  These are called from Mathematica  ***************/


        /// <summary>
        /// "Casts" the __ComObject to the given type.
        /// </summary>
        /// <remarks>
        /// The type can be a class or an interface. If it is a class, you get back an object typed as that class.
        /// If it is an interface you get back a COMObjectWrapper that associates the object with that interface.
        /// Allows exceptions to be thrown.
        /// </remarks>
        /// <param name="obj">The COM object to cast.</param>
        /// <param name="t">The type to cast to.</param>
        /// <returns></returns>
        ///
        internal static object Cast(object obj, Type t)
        {
            if (t.IsInterface)
            {
                IntPtr pUnk   = Marshal.GetIUnknownForObject(obj);
                object result = Marshal.GetTypedObjectForIUnknown(pUnk, t);
                Marshal.Release(pUnk);
                return(new COMObjectWrapper(result, t));
            }
            else
            {
                return(Marshal.CreateWrapperOfType(obj, t));
            }
        }