I've already checked a few other posts regarding reflection and overloaded methods but could find any help. One post I found was this one, but that didn't help a lot.
I have the following to methods:
1 | public void Delete<T>(T obj) where T : class { ... }
2 | public void Delete<T>(ICollection<T> obj) where T : class { ... }
I'm trying to get method N°1.
I tried the classic GetMethod("Delete")
approach, but since there are two methods with this name a Ambiguous
-Exception was thrown. I tried specifying the method schema with an additional parameter like GetMethod("Delete", new [] { typeof(Object) })
which didn't find anything (null returned).
I figgured I might as well just loop through all methods and check for the parameters. I have the following method...
public static IEnumerable<MethodInfo> GetMethods(this Type t, String name, Type schemaExclude)
{
IEnumerable<MethodInfo> rm = t.GetRuntimeMethods().Where(x => x.Name.Equals(name));
return (from runtimeMethod in rm let pis = runtimeMethod.GetParameters() where !pis.Any(x => schemaExclude.IsAssignableFrom(x.ParameterType)) select runtimeMethod).ToList();
}
... which returns the methods which do not contain a parameter with type schemaExclude
.
I called it like this GetMethods("Delete", typeof(ICollection))
which didn't work as expected.
Apparently ..ICollection'1[T]
is not assignable to ICollection
. Neither is it to IEnumerable
, IEnumerable<>
and ICollection<>
. I, again, tried it with typeof(Object)
which did work but did return both methods (like its supposed to).
What exactly am I missing?
Aucun commentaire:
Enregistrer un commentaire