using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Collections;
using System.Threading;
namespace Systin.Library{
/// <summary>
/// This class manages all of the different Language Driver Classes for language assemblies.
/// </summary>
public sealed class LanguageDriverClassManager
{
#region Properties and Fields
private Dictionary<string, LanguageDriverClass> mLanguageDriverClassHash;
private Stack<MethodInfo> mInstructionStack;
/// <summary>
/// Gets the language driver class hash.
/// </summary>
/// <value>The language driver class hash.</value>
public Dictionary<string, LanguageDriverClass> LanguageDriverClassHash
{
get { return mLanguageDriverClassHash; }
}
/// <summary>
/// Gets the instruction stack.
/// </summary>
/// <value>The instruction stack.</value>
public Stack<MethodInfo> InstructionStack
{
get { return mInstructionStack; }
}
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="LanguageDriverClassManager"/> class.
/// </summary>
private LanguageDriverClassManager() {
this.mLanguageDriverClassHash = new Dictionary<string, LanguageDriverClass>();
this.mInstructionStack = new Stack<MethodInfo>();
}
#endregion
#region Public Methods
/// <summary>
/// Factories the specified language driver assembly.
/// </summary>
/// <param name="languageDriverAssembly">The language driver assembly.</param>
/// <returns> <see cref="LanguageDriverClassManager"/> </returns>
public static LanguageDriverClassManager Factory(Assembly languageDriverAssembly)
{
if (languageDriverAssembly == null)
{
throw new ArgumentNullException("languageDriverAssembly 1");
}
//Create a new LanguageDriverClass Manager
LanguageDriverClassManager ldcm = new LanguageDriverClassManager();
//Look through the list of classes in the assembly and find the Systin ones
foreach (Type classType in languageDriverAssembly.GetTypes())
{
if (Attribute.GetCustomAttribute(classType, typeof(LanguageDriverAttribute)) != null)
{
LanguageDriverClass ldc = LanguageDriverClass.Factory(classType);
ldcm.mLanguageDriverClassHash.Add(classType.Name, ldc);
}
}
return ldcm;
}
/// <summary>
/// Calls the method.
/// </summary>
/// <param name="methodToInvoke">The method to invoke.</param>
/// <param name="langDriveMan">The language driver manager.</param>
/// <returns>LanguageDriverClassManager </returns>
public static LanguageDriverClassManager CallMethod(String methodToInvoke, LanguageDriverClassManager langDriveMan)
{
#region Validate Parms
if (methodToInvoke == null)
{
throw new ArgumentNullException("methodToInvoke");
}
if (langDriveMan == null)
{
throw new ArgumentNullException("langDriveMan");
}
#endregion
foreach (LanguageDriverClass ldc in langDriveMan.mLanguageDriverClassHash.Values)
{
if (LanguageDriverClass.MethodExists(methodToInvoke, ldc))
{
//Thread newThread = new Thread(new ParameterizedThreadStart(ldc.CallMethod));
MethodInfo method = null;
ldc.CallMethod(methodToInvoke, ref method);
langDriveMan.mInstructionStack.Push(method);
}
else
{
throw new TargetException("Method not found in Language Driver");
}
}
LanguageDriverClassManager ldcm = null;
ldcm = ((LanguageDriverClassManager)langDriveMan.MemberwiseClone());
return ldcm;
}
/// <summary>
/// Executes all instructions against the language driver.
/// </summary>
/// <param name="data">The data.</param>
public static void CallMethodThreadedWrapper(object data)
{
//If the data input parm is null throw an ArgumentNullException
if (data == null)
{
throw new ArgumentNullException("data");
}
//If the type of data object is not a ThreadWorkermethodCall object throw an ArgumentException
if (!data.GetType().Equals(typeof(ThreadWorkerMethodCall)))
{
throw new ArgumentException("data");
}
//Cast the data input to a string type
ThreadWorkerMethodCall tworker = ((ThreadWorkerMethodCall)data);
//Invoke all the methods in the queue
foreach(Instruction instruction in tworker.Instructions){
LanguageDriverClassManager.CallMethod(instruction.InstructionContent, tworker.LanguageDriverClassManager);
}
}
#endregion
}
}
|