dimanche 11 septembre 2016

GetRuntimeInterfaceMap Returns Unexpected Mapped Method

I am running into a problem with GetRuntimeInterfaceMap (GetInterfaceMap also exhibits the same behavior) and I am hoping to have someone shed some light on what I am doing wrong, and how I can possibly address it. Here is a simple xUnit test along with corresponding code that demonstrates the issue:

(I also have it hosted on GitHub if you would like to run it locally in VS2015+).

using System;
using System.Linq;
using System.Reflection;
using System.Windows.Input;
using Xunit;

namespace Reflection.InterfaceMaps
{
    public class BasicTests
    {
        [Fact]
        public void VerifyMappings()
        {
            var expected = typeof(Expected).GetTypeInfo().GetRuntimeInterfaceMap( typeof(ICommand) );
            Assert.Equal( "System.Windows.Input.ICommand.Execute", expected.TargetMethods.Last().Name );

            var unexpected = typeof(Unexpected).GetTypeInfo().GetRuntimeInterfaceMap( typeof(ICommand) );
            Assert.Equal( "System.Windows.Input.ICommand.Execute", unexpected.TargetMethods.Last().Name ); // Fails with "Execute"
        }
    }

    interface ICommand<in T> : ICommand
    {
        void Execute( T parameter );
    }

    abstract class CommandBase<T> : ICommand<T>
    {
        bool ICommand.CanExecute( object parameter ) => false;
        void ICommand.Execute( object parameter ) {}
        public event EventHandler CanExecuteChanged;

        public abstract void Execute( T parameter );
    }

    class Expected : CommandBase<object>
    {
        public override void Execute( object parameter ) {}
    }

    interface IUnexpected : ICommand<object> {}
    class Unexpected : CommandBase<object>, IUnexpected
    {
        public override void Execute( object parameter ) {}
    }

As you can see, the Expected type returns the expected mapping to the desired explicitly defined method of System.Windows.Input.ICommand.Execute. However, the Unexpected type does not. Unexpected also implements IUnexpected which implements ICommand<> which in turn implements ICommand, the ultimate interface I am interested in.

I would expect that the requested interface mapping for ICommand would return the explicit method mapped as found in the expected map, but alas it does not.

Is there another consideration here that I should explore? Or is this perhaps a bug? (Never know :) ).





Aucun commentaire:

Enregistrer un commentaire