jeudi 11 juillet 2019

Cast Object with string name of class in c# [duplicate]

This question already has an answer here:

I'm trying to cast object with string name of class, it is possible?

Example:

string[] reportsName = { "MB51", "FX55", "LK08" };
object[] parameters = new object[] { ... }; 

foreach(string reportName in reportsName)
{
    MethodInfo query = typeof(Queries).GetMethod("GetRows" + reportName);
    var rows = query.Invoke(typeof(Queries), parameters);
    //How can i cast rows variable, like this way:
    //var rows = (IEnumerable<$"Class{reportName}">) query.Invoke(typeof(Queries), parameters);

    AddNewSheet(rows, reportName);
}

My problem is that rows is object and my AddNewSheet receives the parameters:

public void AddNewSheet<T>(IEnumerable<T> rows, string sheetName)

How can i do that:

(IEnumerable< PASS NAME OF CLASS AS STRING >)

or there is another way to convert rows object to IEnumerable <"stringParameter">





Aucun commentaire:

Enregistrer un commentaire