【发布时间】:2010-09-20 18:18:45
【问题描述】:
我收到 InvalidOperationException 的消息:
“不能对 ContainsGenericParameters 为真的类型或方法执行后期绑定操作。”
以下是相关部分代码:
// Gets the entity type of the table to update.
Type entityType = Jobs.GetType(syncSettings.TableToUpdate);
// Creates a generic list with the same type to hold the records to update.
Type listType = typeof(List<>).MakeGenericType(entityType);
object recordsToUpdate = Activator.CreateInstance(listType);
// Fills the list recordsToUpdate...
// A few lines below, I try to call the extension method ElementAt:
MethodInfo elementAtMethod = typeof(Enumerable).GetMethod("ElementAt", BindingFlags.Static | BindingFlags.Public);
elementAtMethod.MakeGenericMethod(entityType);
object record = elementAtMethod.Invoke(
recordsToUpdate,
new object[] { recordsToUpdate, recordIndex });
在我的最后一个动作中,抛出了上面提到的异常。我究竟做错了什么?这个错误是什么意思?
我一直在调查,似乎方法参数类型 T 仍然是通用的。这就是 ContainsGenericParameters 为真的原因。如何将参数设置为 entityType?
【问题讨论】:
标签: c# reflection