I want to get a method from specific interface, but it can be in multiple interfaces. I write this code:
private static Expression<Func<T, T, int>> CreateCompareTo<TProperty>(MemberExpression expression, Expression<Func<T, T, int>> result) where TProperty : IComparable<TProperty>, IComparable
{
var methodInfo = typeof(TProperty).GetMethod("CompareTo", new[] { typeof(IComparable<TProperty>), typeof(IComparable) });
...
MSDN
An array of Type objects representing the number, order, and type of the parameters for the method to get.
So I expect that it will search method through IComparable<T>
, and, if didn't found, will search it in non-generic IComparable
. But it doesn't. Well, now I rewrite it:
private static Expression<Func<T, T, int>> CreateCompareTo<TProperty>(MemberExpression expression, Expression<Func<T, T, int>> result) where TProperty : IComparable<TProperty>, IComparable
{
Type t = typeof(TProperty);
var methodInfo = t.GetMethod("CompareTo", new[] { typeof(IComparable<TProperty>) }) ?? t.GetMethod("CompareTo", new[] { typeof(IComparable) });
...
And now it works.
Why first option is not working?
Aucun commentaire:
Enregistrer un commentaire