LanguageDriverClass.cs :  » Testing » SystiN » Systin.Library » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Testing » SystiN 
SystiN » Systin.Library » LanguageDriverClass.cs
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
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.