小编beh*_*ani的帖子

对于以 long 列表作为其唯一参数的方法,在 C# 中陷入反射状态

我的课堂上有这个方法TeacherBusiness.cs

public List<Teacher> GetList(List<long> ids)
Run Code Online (Sandbox Code Playgroud)

我想通过反射来调用它。这就是我所做的:

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 });
Run Code Online (Sandbox Code Playgroud)

但是,当我调用它时,我收到此错误:

对象与目标类型不匹配。

我被困在这一点上。

如果我将调用代码更改为getListMethod.Invoke(business, ids)代码将无法编译,并且出现此错误:

错误CS1503:参数2:无法从“System.Collections.Generic.List”转换为“对象?[]?”

我应该怎么办?

c# reflection

0
推荐指数
1
解决办法
83
查看次数

标签 统计

c# ×1

reflection ×1