QTP DotNetFactory使用解析

上一篇 / 下一篇  2009-12-01 14:57:41 / 个人分类:QTP

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
 
 
例子:
  1. Set objBtn1 = DotNetFactory.CreateInstance(”System.Windows.Forms.Button”, “System.Windows.Forms”)
  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
{// FieldsprivateArrayList_ArgList;// MethodsstaticDotNetFactory()
    {
     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);
// PropertiesprotectedvirtualTypeMyType{get; }
 Type IReflect.UnderlyingSystemType{get; }
}
  
 
[ComVisible(true), ClassInterface(ClassInterfaceType.AutoDispatch)]internalclassDispWrapper: IReflect
{// FieldsinternalException_exception;internalobject_object;privateType_ObjectType;// MethodsinternalDispWrapper(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);
 
// PropertiespublicTypeUnderlyingSystemType{get; }
}

TAG:

 

评分:0

我来说两句

Open Toolbar