Using c# Generics for a Method

Best practices, code snippets for common functionality, examples, and guidelines.
bberrabah
Posts: 24
Joined: Wed Dec 20, 2017 6:18 pm

Using c# Generics for a Method

Post by bberrabah » Thu Jun 07, 2018 10:51 am

Hello;
I want to sk you how is it possible to use Generics for a method, so i can take two types of Arguments: Adapter and RepoItemInfo.

The idea is to have

Code: Select all

[UserCodeMethod]
public static void CheckExistsAndSetValueG<T> (T input, String value)
{
	if (input is Adapter){
		Adapter adapter = (Adapter) input;
		CheckExistsAndSetValue(adapter, value);
	}
	if (input is RepoItemInfo){
		RepoItemInfo itemInfo = (RepoItemInfo) input;
		CheckExistsAndSetValue(itemInfo, value);
	}
}
How do you do to make this possible?

Thanks for assistance

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3930
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Using c# Generics for a Method

Post by odklizec » Thu Jun 07, 2018 11:34 am

Hi,

I believe something like this is not currently possible. Ranorex methods support only these arguments:
MehtodArguments.png
MehtodArguments.png (4.85 KiB) Viewed 176 times
Anyway, in my opinion, the best thing you can do is to always pass elements as RepoItemInfo. This would avoid some unexpected exceptions, caused by not yet available element (at a time of calling the method). And you can always create Adapter (if needed) from RepoItemInfo object. So there is really no need to pass the element as Adapter. Yes, it may be a bit quicker to create such method, but using RepoItemInfo is much safer option. At least I believe so ;)
Last edited by odklizec on Thu Jun 07, 2018 2:14 pm, edited 1 time in total.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

bberrabah
Posts: 24
Joined: Wed Dec 20, 2017 6:18 pm

Re: Using c# Generics for a Method

Post by bberrabah » Thu Jun 07, 2018 1:40 pm

Thanks for the answer.

I finally used to create this Method,

Code: Select all

public static void CheckExistsAndSetValueG<T> (T input, String value) where T:RepoItemInfo, IAdapter
{
	if (input is IAdapter){
		IAdapter adapter = (IAdapter) input;
		CheckExistsAndSetValue(adapter, value);
	}
	if (input is RepoItemInfo){
		RepoItemInfo itemInfo = (RepoItemInfo) input;
		CheckExistsAndSetValue(itemInfo, value);
	}
}
But i think that ranorex do not allow to call such "Generic" methods form its Modules top level classes. So i'll follow your advice, thanks a lot.

Regards