vendredi 15 avril 2016

Pass a generic type 'class' when only having a string

In C# I have a method

public string GetString<T>() where T : class

In the calling function I have only the name of the class which I want to pass to GetString()<T>. E.g. "MyClass". How can I convert "MyClass" to class? I tried it using Types but I couldn't find a conversion.

EDIT: The real example is from a database access class. The client asks for information for a specific hardware component. This component is specified by a string. Based on this string I want to access a table with a known name pattern. Not hard coding table names allows us to add/remove tables from the database without the need to change the C# code.

The function which returns the needed information exists already and needs a class as generic type.

E.g.

string tblName = "HW_" + hwComponentFromClient;
string retValue = GetString<GetClassByString(tblName)>();

I need something like a method:

class GetClassByString(string);





Aucun commentaire:

Enregistrer un commentaire