Forum Discussion

socc3rpr0's avatar
socc3rpr0
Occasional Contributor
8 years ago

Creating C# List Using dotNET

Hello, 

 

I want to create a script using TestComplete to test a C# API one of my colleagues wrote. How would I go about creating a C# list with TestComplete since it requires all arguments to be passed to a C# function, even though all arguments have a default value. I am using the CLR Bridge to load the DLL for the API. I  want to use dotNET in order to call these functions. 

 

// Here are two C# functions I want to call:

 

1) public short SendRequest(List<IP> ipList, double timeout = 2.0)

 

2) public void RefreshWindow(List<Devices> deviceList = null, int maxAttempts = 3, double timeout=2.0) 

 

 

// So Far all I got to work is this but that just creates a list of 'Sytem.Object' not a list of the class I want. 

// I found this example here

 

var typeListOf = dotNET.System.Type.GetType("System.Collections.Generic.List`1");
var paramTypes = dotNET.System.Array.CreateInstance(dotNET.System.Type.GetType("System.Type"), 1);
var objType = dotNET.System.Type.GetType("System.Object");
paramTypes.SetValue(objType, 0);
var typeListOfString = typeListOf.MakeGenericType(paramTypes);
var list = dotNET.System.Activator.CreateInstance_3(typeListOfString);

Any help would be greatly appreciated. 

 

 

 

 

 

4 Replies

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    Hi socc3rpr0,

     

    Please keep in mind that TestComplete is a GUI testing tool, it's not really suitable for .NET API unit testing. Consider using a unit testing tool like NUnit, MSTest, xUnit.net, etc.

     

    That said, your example is almost correct. You only need to specify the appropriate type - IP or Devices - for objType:

    var objType = dotNET.System.Type.GetType("Namespace.Whatever.IP");
    
    var objType = dotNET.System.Type.GetType("Namespace.Whatever.Devices");

    The assembly containing the IP and Devices classes needs to be in the CLR Bridge list.

     

    Once you have created the list you can add list items as follows:

    var ip1 = dotNET.name_space.IP.zctor(parameters);
    var ip2 = dotNET.name_space.IP.zctor(parameters);
    
    list.Add(ip1);
    list.Add(ip2);
  • Lagencie's avatar
    Lagencie
    Frequent Contributor

    I know its very old, but I had the same Problem and with the suggestion it didnt work, because it just wouldnt know the class i type into the 

     

    GetType("System.Object")

    I managed it with 

     

    objType = dotNET.namespace_further_further_whatever.ClassIwant.zctor().GetType()

    So i just created a new instance of the class i needed and used the GetType onto it, this way the type set was exactly what I needed

     

    As for the whole construct:

     

    typeListOf = dotNET.System.Type.GetType("System.Collections.Generic.List`1")
    objType = dotNET.namespace_further.SpecificValueClass.zctor().GetType()
    paramTypes = dotNET.System.Array.CreateInstance(dotNET.System.Type.GetType("System.Type"), 1)
    paramTypes.SetValue(objType, 0)
    typeListOfString = typeListOf.MakeGenericType(paramTypes)
    
    newValue = dotNET.namespace.WattPeakValues.zctor()
    
    newValue.ID = 1
    settings.SpecificValue.Add(newValue)