为什么在具有接口类型约束的泛型方法中的显式C#接口调用总是调用基本实现?
例如,请考虑以下代码:
public interface IBase
{
    string Method();
}
public interface IDerived : IBase
{
    new string Method();
}
public class Foo : IDerived
{
    string IBase.Method()
    {
        return "IBase.Method";
    }
    string IDerived.Method()
    {
        return "IDerived.Method";
    }
}
static class Program
{
    static void Main()
    {
        IDerived foo = new Foo();
        Console.WriteLine(foo.Method());
        Console.WriteLine(GenericMethod<IDerived>(foo));
    }
    private static string GenericMethod<T>(object foo) where T : class, IBase
    {
        return (foo as T).Method();
    }
}
Run Code Online (Sandbox Code Playgroud)
此代码输出以下内容:
IDerived.Method
IBase.Method
而不是人们可能期望的:
IDerived.Method
IDerived.Method
似乎没有办法(缺少反射)来调用在运行时决定的类型的隐藏的,更加派生的显式接口实现.
编辑:要清楚,如果检查在上面的GenericMethod调用中计算结果为true: …
我有2个文件.两者的长度相同(以秒为单位):
如何在我的C#应用程序中使用NAudio .NET库用文件#2中的音频覆盖文件#1的音频?我想将最终结果写入磁盘作为新的视频文件.
我相信这可以使用NAudio 1.7的Media Foundation功能,但我无法弄清楚如何修改视频文件的音频流并重新保存视频.