using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace Systin.Library{
public sealed class LanguageDriverClass
{
#region Properties and Fields
private Type mLanguageDriverObject;
private Object myActivatedInstance;
/// <summary>
/// Gets the activated instance.
/// </summary>
/// <value>The activated instance.</value>
public Object ActivatedInstance
{
get { return myActivatedInstance; }
}
#endregion
#region Constructors
private LanguageDriverClass() { }
/// <summary>
/// This constructor takes a given class reference and creates an instance of it for later method invocation.
/// </summary>
/// <param name="classType">Type language Driver class</param>
private LanguageDriverClass(Type classType)
{
//set the class type variable
this.mLanguageDriverObject = classType;
//Create an instance of this class type
Object instance = Activator.CreateInstance(classType);
//Save this instance for later invocation uses
this.myActivatedInstance = instance;
}
#endregion
#region Public Methods
/// <summary>
/// Creates a LanguageDriverClass Object
/// </summary>
/// <param name="classType">Type of the class.</param>
/// <returns>A LanguageDriverClass</returns>
/// <exception cref="ArgumentNullException">Thrown if classType is null</exception>
/// <exception cref="ArgumentException">Thrown if classType does not have LanguageDriverAttribute defined</exception>
public static LanguageDriverClass Factory(Type classType)
{
//Make sure the type isn't null
if (classType == null)
{
throw new ArgumentNullException("classType");
}
//Make sure that the language class has
if (!Attribute.IsDefined(classType, typeof(LanguageDriverAttribute)))
{
throw new ArgumentException("The Language Attribute is undefined for this type");
}
//Create a new language driver class instance with the provided class type
LanguageDriverClass ldc = new LanguageDriverClass(classType);
return ldc;
}
/// <summary>
/// Create an empty instance of Language Driver Class
/// </summary>
/// <returns>LanguageDriverClass</returns>
public static LanguageDriverClass FactoryEmpty()
{
LanguageDriverClass ldc = new LanguageDriverClass();
ldc.mLanguageDriverObject = null;
ldc.myActivatedInstance = null;
return ldc;
}
/// <summary>
/// This class will call a method on a language driver.
/// </summary>
/// <param name="methodToInvoke">Name of method to call</param>
public MethodInfo CallMethod(string methodToInvoke, ref MethodInfo method)
{
//Make sure method is not null
if (methodToInvoke == null)
{
throw new ArgumentNullException("methodToInvoke");
}
//retrieve the method from the instanced class
method = this.myActivatedInstance.GetType().GetMethod(methodToInvoke);
//Make sure that method was not null
if (method == null)
{
//We throw a targetInvocationException here because we want it to appear that the
//user has attempted to invoke a method that is not in our library
throw new System.Reflection.TargetInvocationException(null);
}
try
{
//Invoke the method
method.Invoke(this.ActivatedInstance, null);
}
catch (Exception ex)
{
//Unable to invoke method on target
throw new System.Reflection.TargetInvocationException("The Language Driver library threw an exception during Invocation", ex.InnerException);
}
return method;
}
/// <summary>
/// Methods the exists.
/// </summary>
/// <param name="methodToFind">The method to find.</param>
/// <param name="ldc">The place to search</param>
/// <returns></returns>
public static bool MethodExists(string methodToFind, LanguageDriverClass ldc)
{
//If either the method to find or the place to look for it in are null throw an Arugment Null Exception
if (methodToFind == null || ldc == null)
{
throw new ArgumentNullException("Parm is null");
}
MethodInfo method =ldc.myActivatedInstance.GetType().GetMethod(methodToFind);
//Look for the method in the class
if(method != null){
//If we found the method make sure it is a systin method
if (Attribute.GetCustomAttribute(method, typeof(SystinAttribute)) != null)
{
return true;
}
else
{
throw new InvalidOperationException("A method call was attempted on a non Systin method");
}
}
return false;
}
#endregion
}
}
|