Lets assume the following interface
interface Foo {
void Bar();
void Bar(string str);
int Bar(int i);
IEnumerable<string> Bar1();
void Bar1(string str);
void Bar1(int i);
//...
}
lets assume a method that receives a method info. How can I check if this method info is about a specific method on the interface?
void DoStuff(MethodInfo method){
//basically in pseudo code
if(method is Foo.Bar(string)){
//dostuff
}
}
The closest I got is something like
if(method.ReflectedType.IsAssignableFrom(typeof(Foo))
&& method.Name == nameof(Foo.Bar)
&& method.GetParameters().Length == 1
&& method.GetParameters()[0].ParameterType == typeof(string))
However, this is quite verbose, and not refactor proof.
In a next step I would like to have a method that can check against a list of MethodInfo. Something like
bool IsIn(this MethodInfo self, IEnumerable<??> methods){
//again, pseudo code
return methods.Any(m => m is self);
}
That can easly be used like
bool b = methodInfo.IsIn(new [] {
Foo.Bar(str),
Foo.Bar1(int)
});
Aucun commentaire:
Enregistrer un commentaire