mercredi 15 novembre 2023

Stuck at reflection in C# for a method with a list of long as its only parameter

I have this method in my TeacherBusiness.cs class:

public List<Teacher> GetList(List<long> ids)

I want to call it via reflection. This is what I do:

var ids = new List<long> { 1, 2, 3 }
var business = typeof(TeacherBusiness);
var getListMethod = business.GetMethod("GetList", new System.Type[] { typeof(List<long>) });
var entities = getListMethod.Invoke(business, new object[] { ids });

However, when I call it I get this error:

Object does not match target type.

I'm stuck at this point.

If I change the invoking code to getListMethod.Invoke(business, ids) the code won't compile and I get this error:

error CS1503: Argument 2: cannot convert from 'System.Collections.Generic.List' to 'object?[]?'

What should I do?





Aucun commentaire:

Enregistrer un commentaire