(转载)QTP DotNetFactory使用解析

上一篇 / 下一篇  2010-03-24 14:32:02 / 个人分类:我的自动化学习

http://docs.google.com/View?id=ddnrfkv6_1917fjkgm9hk
QuickTest Professional 9.x 增加了一项新功能DotNetFactory,它能让你创建.NET对象实例,它封装了CreateInstance方法。如果你想使用一个.NET对象 作为QuickTest中一个方法的一个参数,你必须使用CreateInstance方法首先为该.NET对象创建一个COM接口:

Set var_CreateInstance= DotNetFactory.CreateInstance (TypeName[,Assembly][,args])

Argument

Type

Description

TypeName
String
The full name of the object type, for example,System.Windows.Forms.Form.
Assembly
Any
Optional. If the assembly is preloaded in the registry, you do not need to enter it. Otherwise, you should enter the full path name, for example,System.Windows.Forms. If you want to create an instance of a type in the GAC (Global Assembly Cache), you can enter the short name, for example,Forms.
Note:If you do not pass this argument, QuickTest assumes that the assembly is resident in memory. If QuickTest does not find the assembly, an error message is displayed during the run session.
args
Any
Optional. The required arguments, if any, for the specifiedTypeNameand/orAssembly.
支持DotNetFactory这项功能的DLL位于%programfiles%\HP\QuickTest Professional\bin\DotNetFactory.dll
 

Mscorlib.dll代表微软Common Language Runtime Library,是.NET框架 通用库的主要程序集。它定义了如下命名空间(v2.0.50727):

Microsoft.Win32

Microsoft.Win32.SafeHandles

System

System.Collections

System.Collections.Generic

System.Collections.ObjectModel

System.Configuration.Assemblies

System.Deployment.Internal

System.Deployment.Internal.Isolation

System.Deployment.Internal.Isolation.Manifest

System.Diagnostics

System.Diagnostics.CodeAnalysis

System.Diagnostics.SymbolStore

System.Globalization

System.IO

System.IO.IsolatedStorage

System.Reflection

System.Reflection.Cache

System.Reflection.Emit

System.Resources

System.Runtime

System.Runtime.CompilerServices

System.Runtime.ConstrainedExecution

System.Runtime.Hosting

System.Runtime.InteropServices

System.Runtime.InteropServices.ComTypes

System.Runtime.InteropServices.Expando

System.Runtime.InteropServices.TCEAdapterGen

System.Runtime.Remoting

System.Runtime.Remoting.Activation

System.Runtime.Remoting.Channels

System.Runtime.Remoting.Contexts

System.Runtime.Remoting.Lifetime

System.Runtime.Remoting.Messaging

System.Runtime.Remoting.Metadata

System.Runtime.Remoting.Metadata.W3cXsd2001

System.Runtime.Remoting.Proxies

System.Runtime.Remoting.Services

System.Runtime.Serialization

System.Runtime.Serialization.Formatters

System.Runtime.Serialization.Formatters.Binary

System.Runtime.Versioning

System.Security

System.Security.AccessControl

System.Security.Cryptography

System.Security.Cryptography.X509Certificates

System.Security.Permissions

System.Security.Policy

System.Security.Principal

System.Security.Util

System.Text

System.Threading

.NET类继承体系的根类Sytem.Object也定义在这个类集当中,几乎所有的托管应用程序都必须引用这个类集,所以C#编译器在编译过程中自动加上对mscorlib类集的引用,所有程序集也都自动引用了它。延伸阅读mscorlib.dll & System.dll

 
 
例子:
  1. Set objBtn1 = DotNetFactory.CreateInstance(”System.Windows.Forms.Button”)
  2. Set myList = DotnetFactory.CreateInstance("System.Collections.ArrayList")
  3. Set File = DotNetFactory.CreateInstance("System.IO.File")
  4. Set p1 = DotNetFactory.CreateInstance(”System.Drawing.Point”,”System.Drawing”,x,y)
  5. Set objReader1 = DotNetFactory.CreateInstance(”System.Xml.XmlTextReader”, “System.Xml”,”C:\Documents and Settings\kmohankumar\Desktop\11.xml”)
 
附录:
[Description("Enables you to create an instance of a .NET object, and access its methods and properties."), Guid("15494157-E302-4a17-95C7-24C545CA8F53"), ClassInterface(ClassInterfaceType.None), ProgId("Mercury.DotNetFactory.1"), ComVisible(true)]
publicclassDotNetFactory: IObjFactory, IReflect, IObjFactoryServices
{
// Fields
privateArrayList_ArgList;

// Methods
staticDotNetFactory()
    {
    Trace.WriteLine("ObjectFactory");
    RegistryKeycurrentUser= Registry.CurrentUser;
    RegistryKeykey2= currentUser.OpenSubKey(@"Software\Mercury Interactive\QuickTest Professional\MicTest\ReservedObjects\DotNetFactory");//见下图

    stringstr= (string) key2.GetValue("PreloadAssemblies",null);
    if(str !=null)
    {
        string[]strArray= str.Split(newchar[] {';'});
        DotNetFactoryfactory=newDotNetFactory();
        currentUser.Close();
        key2.Close();
        foreach(stringstr2instrArray)
        {
            try
            {
                Trace.WriteLine(str2);
                factory.LoadAssembly(str2);
                Trace.WriteLine("Loaded");
            }
            catch
            {
                Trace.WriteLine("Failed");
            }
        }
    }
    }

publicDotNetFactory();
    {
       this._ArgList =newArrayList();
    }

privatevoidAddArg(object arg)
    {
        if(argisDispWrapper)
        {
            arg = ((DispWrapper) arg)._object;
        }
        this._ArgList.Add(arg);
    }
privatevoidClearArgs();
 
publicobjectCreateInstance(string TypeName, object assembly,paramsobject[] CtorArgs)
    {
        objectobj3;
        ConstructorInfo[]constructors;
        this._ArgList.Clear();
        if(CtorArgs !=null)
        {
            foreach(objectobj2inCtorArgs)
            {
                this.AddArg(obj2);
            }
        }
        if((assemblyisstring) && (((string) assembly).Length >0))
        {
            this.LoadAssembly(assemblyasstring);
        }
        Assembly[]assemblies= AppDomain.CurrentDomain.GetAssemblies();//获取当前应用程序域加载的所有程序集
        TypeconversionType=null;
        foreach(Assemblyassembly2inassemblies)
        {
            conversionType = assembly2.GetType(TypeName);//如果CreateInstance仅有一个参数无须带任何可选参数,帮助里说程序集已在内存中,如例2和例3             
             if(conversionType !=null)
            {
                break;
            }
        }
        if(conversionType ==null)
        {
            thrownewException("Type '"+ TypeName +"' not found in any loaded assembly");
        }
        if(this._ArgList.Count ==0)
        {
            constructors = conversionType.GetConstructors();//值来自于如果CreateInstance仅有一个参数
            if((constructors ==null) || (constructors.Length ==0))
            {
                if(conversionType.IsValueType)
                {
                    stringstr="0";
                    try
                    {
                        obj3 = Convert.ChangeType(str, conversionType);
                    }
                    catch(InvalidCastException)
                    {
                        obj3 =newDispWrapper(conversionType);
                    }
                }
                else
                {
                    obj3 =newDispWrapper(conversionType);
                }
            }
            else
            {
                obj3 = conversionType.Assembly.CreateInstance(TypeName);
            }
        }
        else
        {
            object[]args=newobject[this._ArgList.Count];
            for(inti=0; i <this._ArgList.Count; i++)
            {
                args[i] =this._ArgList[i];
            }
            this.ClearArgs();
            constructors = conversionType.GetConstructors();//值来自于如果CreateInstance仅有一个参数
            if((constructors ==null) || (constructors.Length ==0))
            {
                obj3 =newDispWrapper(conversionType);
            }
            else
            {
                obj3 = conversionType.Assembly.CreateInstance(TypeName,false, BindingFlags.CreateInstance,null, args,null,null);//此步能接受例4和例5的输入
            }
            if(obj3 ==null)
            {
                thrownewException("Cannot create instance of "+ TypeName);
            }
        }
        if(!(obj3isstring) && !(obj3isDispWrapper))
        {
            obj3 =newDispWrapper(obj3);
        }
        returnobj3;
    }
 
publicvoidLoadAssembly(string partialName)
    {
    stringstr= partialName.ToLower();
    if(str.IndexOf(".dll") >0)
    {
        this.LoadAssemblyByPath(partialName);
    }
    elseif(str.IndexOf(".exe") >0)
    {
        this.LoadAssemblyByPath(partialName);
    }
    elseif(Assembly.LoadWithPartialName(partialName) ==null)
    {
        thrownewFileNotFoundException("Cannot load Assembly "+ partialName);
    }
    }

privatevoidLoadAssemblyByPath(string path)
    {
    Assembly.LoadFrom(path);
    }

    FieldInfo IReflect.GetField(string name, BindingFlags bindingAttr);
    FieldInfo[] IReflect.GetFields(BindingFlags bindingAttr);
    MemberInfo[] IReflect.GetMember(string name, BindingFlags bindingAttr);
    MemberInfo[] IReflect.GetMembers(BindingFlags bindingAttr);
    MethodInfo IReflect.GetMethod(string name, BindingFlags bindingAttr);
    MethodInfo IReflect.GetMethod(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers);
    MethodInfo[] IReflect.GetMethods(BindingFlags bindingAttr);
    PropertyInfo[] IReflect.GetProperties(BindingFlags bindingAttr);
    PropertyInfo IReflect.GetProperty(string name, BindingFlags bindingAttr);
    PropertyInfo IReflect.GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers);
    object IReflect.InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters);publicobjectUnwrap(object wrapper);
// Properties
protectedvirtualTypeMyType{get; }
Type IReflect.UnderlyingSystemType{get; }
}
  
 
[ComVisible(true), ClassInterface(ClassInterfaceType.AutoDispatch)]
internalclassDispWrapper: IReflect
{
// Fields
internalException_exception;
internalobject_object;
privateType_ObjectType;

// Methods
internalDispWrapper(object obj)
{
    this._object = obj;
    this._ObjectType = obj.GetType();
}

internalDispWrapper(Type t)
{
    this._object =null;
    this._ObjectType = t;
}
publicFieldInfoGetField(string name, BindingFlags bindingAttr);
publicFieldInfo[]GetFields(BindingFlags bindingAttr);
publicMemberInfo[]GetMember(string name, BindingFlags bindingAttr);
 public MemberInfo[] GetMembers(BindingFlags bindingAttr);
 public MethodInfo GetMethod(string name, BindingFlags bindingAttr);
 public MethodInfo[] GetMethods(BindingFlags bindingAttr);
 public PropertyInfo[] GetProperties(BindingFlags bindingAttr);  public PropertyInfo GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers);
 public object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters); MethodInfo IReflect.GetMethod(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers); PropertyInfo IReflect.GetProperty(string name, BindingFlags bindingAttr);
 
// Properties
publicTypeUnderlyingSystemType{get; }
}
 

TAG:

 

评分:0

我来说两句

Open Toolbar