Click here to Skip to main content
15,895,084 members
Articles / Web Development / ASP.NET

A threading framework to optimize interoperation between .NET and apartment threaded COM components

Rate me:
Please Sign up or sign in to vote.
4.73/5 (6 votes)
7 May 200710 min read 50K   743   42  
A generic solution and an accompanying threading framework to optimize calls between .NET and apartment threaded COM components
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using System.Diagnostics;
using System.Reflection;

namespace NUnitTests
{
    public abstract class BaseTest
    {
        protected void SameTest(object expected, object actual)
        {
            if (expected == null && actual == null)
            {
                Console.WriteLine("expected & actuall are null");
                Assert.AreEqual(null, null);
                return;
            }
            Console.WriteLine("Expected hashcode = {0}\nActual hashcode = {1}",
                expected.GetHashCode(), actual.GetHashCode());
            Assert.AreEqual(expected.GetHashCode(), actual.GetHashCode());
        }
        protected void SameTest(object[] expected, object[] actual)
        {
            Assert.AreEqual(expected.Length, actual.Length);
            for (int i = 0; i < expected.Length; i++)
                SameTest(expected[i], actual[i]);
        }

        protected void Check(object expected, object actual)
        {
            string mesg = String.Format("\nResult of test [{0}] -",
                GetLastMethodCalled());            
            Console.WriteLine(mesg);
            for (int i = 0; i < mesg.Length-2; i++)
                Console.Write("-");
            Console.WriteLine("\nExpected = {0}\nActual = {1}\n",
                expected, actual);
            Assert.AreEqual(expected, actual);
        }

        protected void Check(object[] expected, object[] actual)
        {
            Assert.AreEqual(expected.Length, actual.Length);
            for (int i = 0; i < expected.Length; i++) Check(expected[i], actual[i]);
        }

        private string GetLastMethodCalled()
        {
            StackTrace s = new StackTrace(false);
            foreach (StackFrame f in s.GetFrames()) {
                MethodBase method = f.GetMethod();
                Type dt = method.DeclaringType;
                /*
                 * GetType is a polymorphic call that resolves to derived test type
                 * Base type is always BaseTest
                 */
                Type testType = GetType();
                Type penultimateType = GetPenultimateBaseType(testType);
                if (dt.Name != penultimateType.Name) 
                    return method.Name;                
            }
            return null;
        }

        private Type GetPenultimateBaseType(Type t)
        {
            Type baseType = t.BaseType;
            while ((baseType = t.BaseType) != typeof(Object))
                t = t.BaseType;
            return t;
        }
    }
}

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
United States United States
I am a tech lead working for Cap Gemini. Although I primarily work with the Microsoft technology stack (including .NET and legacy technologies) I like to keep myself informed about developments in the Java world.

Comments and Discussions