It is strange but the source code
public class Processor<T> where T: class
{
...
private object WorkWithSubtype(IRequester nextRequester, Type type)
{
if (type.GetInterface("IList") != null)
{
var originalType = type.GetGenericArguments()[0];
var list = Activator.CreateInstance(type);
var method = typeof(Processor<>).GetMethod("GetObjects", BindingFlags.NonPublic | BindingFlags.Instance).MakeGenericMethod(originalType);
var resList = method.Invoke(this, new object[] { nextRequester });
typeof(List<>).GetMethod("AddRange").MakeGenericMethod(originalType).Invoke(list, new object[] { resList });
return list;
}
}
private IEnumerable<K> GetObjects<K>(IRequester requester) where K: class
{
...
//We can call method WorkWithSubtype in this method sometimes
}
}
And i get “Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true.”. The exception is throwing at line ‘var resList = method.Invoke(this, new object[] { nextRequester });’. Can you help me? Thanks in advance!
You have two generic parameters: one is for the Processor class; the other is for the GetObjects method. In making the generic method, you’ve supplied a type argument for the method’s type parameter, but you haven’t supplied a type argument for the class’s generic parameter.
Depending on the purpose of the processor class, you could try one of the following solutions:
typeof(Processor<T>)rather than using reflection to build the closed generic typeI think the second is most likely to be what you’re looking for:
Additionally,
AddRangeis not a generic method; rather,List<>is a generic type, so: