GetRuntimeInterfaceMap返回意外的映射方法

时间:2016-09-11 19:28:38

标签: c# .net reflection system.reflection

我遇到问题GetRuntimeInterfaceMapGetInterfaceMap也表现出同样的行为)我希望有人能说明我做错了什么,以及我如何解决它。这是一个简单的xUnit测试以及演示该问题的相应代码:

(如果您想在VS2015 +中本地运行它,我也have it hosted on GitHub

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 ) {}
    }

如您所见,Expected类型将预期的映射返回到System.Windows.Input.ICommand.Execute所需的显式定义方法。但是,Unexpected类型没有。 Unexpected还实现了IUnexpected,它实现了ICommand<>,后者又实现了ICommand,这是我感兴趣的终极界面。

我希望ICommand所请求的接口映射会返回映射为在预期映射中找到的显式方法,但它不会。

这里有另一个考虑我应该探索吗?或者这可能是一个错误? (永远不知道:))。

0 个答案:

没有答案