Creating C# List Using dotNET
Creating C# List Using dotNET
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");
var typeListOfString = typeListOf.MakeGenericType(paramTypes);
var list = dotNET.System.Activator.CreateInstance_3(typeListOfString);
Any help would be greatly appreciated.
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);
SmartBear Documentation Team Lead
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
> Consider using a unit testing tool like NUnit, MSTest, xUnit.net, etc.
Or TestLeft (https://smartbear.com/product/testleft/overview/) which will provide you all power of .Net programming while keeping you within TestComplete's eco-system (Object Browser, Log, etc.).
/Alex [Community Hero]
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
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
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)
Thanks for sharing your solution with us, @Lagencie!
SmartBear Community and Education Manager