lundi 6 avril 2015

Create class from type and use it as a method's inputting generic class

I have a method like this:



public T GetDataObject<T>(int id) {
...
}


Now I want to invoke this method based on a type, like I have some code that does this:



var typeStr = String.Format("{0}.{1}, {0}", Properties.Settings.Default.ObjectNameSpace, Request.QueryString["Type"]);
var type = Type.GetType(typeStr);


the typestring can be anything, I want to keep it generic like this. So how can I invoke my method based on this type?


I know this doesn't work, which makes sense, but it gives u an idea of what I want to accomplish:



var obj = GetDataObject<type>(Convert.ToInt32(Request.QueryString["ID"]));


Any ideas?






Aucun commentaire:

Enregistrer un commentaire