C# 等价接口间的隐式转换

C# 等价接口间的隐式转换,c#,generics,interface,casting,C#,Generics,Interface,Casting,假设我有以下接口: public interface IMap<S, T>() { T Map(S source); } 显然,这种请求在很大程度上取决于IMyMap没有比它实现的接口更多的定义 tehDorf提供的解决方案是我想要的;但是,我也在寻找其他可能的解决方案,例如使用包装器: public class MyMapWrapper : IMyMap { private IMap<int, IEnumerable<SomeOtherType>>

假设我有以下接口:

public interface IMap<S, T>()
{
  T Map(S source);
}
显然,这种请求在很大程度上取决于
IMyMap
没有比它实现的接口更多的定义

tehDorf提供的解决方案是我想要的;但是,我也在寻找其他可能的解决方案,例如使用包装器:

public class MyMapWrapper : IMyMap
{
  private IMap<int, IEnumerable<SomeOtherType>> wrapped;
  public MyMapWrapper(IMap<int, IEnumerable<SomeOtherType>> wrapped)
  { this.wrapped = wrapped; }

  public IEnumerable<SomeOtherType> Map(int source) { return wrapped.Map(source); }
}
公共类MyMapWrapper:IMyMap
{
私有IMap封装;
公共MyMapWrapper(IMap包装)
{this.wrapped=wrapped;}
公共IEnumerable映射(int源){return wrapped.Map(源);}
}
有没有其他不同的方法来做我想做的事情?尤其是任何未降级为单个文件的文件。

您可以添加(谢谢,):

但是,您必须将using alias指令添加到任何要在其中使用它的代码文件中。

您可以执行以下操作:

public interface IMap<S, T>
{
    T Map(S source);
}

public class SomeClass { }
public class ThatClass { }

public interface IMapOfSomething : IMap<SomeClass, ThatClass> { }

public class Map : IMapOfSomething
{
    ThatClass IMap<SomeClass, ThatClass>.Map(SomeClass source)
    {
        throw new NotImplementedException();
    }
}
公共接口IMap
{
T图(S源);
}
公共类SomeClass{}
公共类ThatClass{}
公共接口IMapOfSomething:IMap{}
公共类映射:IMapOfSomething
{
ThatClass IMap.Map(SomeClass源代码)
{
抛出新的NotImplementedException();
}
}
您可以将
Map
转换为
IMap
IMap

您可以将
IMap
转换为
IMap

这一部分不清楚:

我还希望能够将任何具有完全相同类型参数的IMap实现强制转换为特定命名的类型

如果类没有实现
IMap
,它恰好具有相同的属性和方法,那么就不能将其强制转换为
IMap
。这将对语言产生不利影响。其目的是有意实现一个接口。如果是故意的,那么您可以通过声明类实现了接口来表明这一点


想象一下,如果您可以将某个对象转换为
IMap
,而不实际实现该接口,仅仅因为它具有相同的属性和方法,那么您可能会遇到什么麻烦。您可以更改该类,使其不再“看起来像”
IMap
,但不会出现任何编译器错误。代码在运行时会失败。

它被称为a。这不符合OP的要求。它不会将
IMap
投射到
IMyMap
@Rob。OP希望使用
IMap
减少麻烦。他们具体声明“这不是C++ TyPufF,这对于我正在寻找的东西来说是完美的,这是C.*”——我解释为“OP不知道C现在有一个类似的特性”。想要制作另一个界面的例子是OP对C#中可能出现的情况的最佳猜测。Rob我为我的问题不够清晰而道歉——这是我正在寻找的解决方案领域。我会修改我的部分问题。@tehDorf说得很对。如果你想稍微修改一下你的答案(也许删除划掉的文本),我可以删除我的反对票;因为它现在被锁定了。我将修改我的问题,希望能减轻困惑。我想我已经有了我的头脑。我认为你发布的代码正是你想要做的。您正在实现适配器模式。您有一个实现一个接口的类,您需要它来实现不同的接口。你所说的“包装器”是一个适配器。我之所以感到困惑,是因为tehDorf的建议和使用包装器是完全不相关的解决方案,可以解决完全不同的问题。“我不认为他们两个都有可能解决同一个问题。”ScottHannen抱歉,我不太熟悉所有不同的模式名称。在我看来,在这个特定的实例中,我可以使用using alias将IMyMap定义为使用IMyMap实例的类的同一个文件中的IMap,但是,别名只存在于该文件中。或者,我可以将IMyMap定义为一个接口(项目范围的定义,而不是本地文件),在前面提到的类中使用该接口,并使用适配器模式包装碰巧使用完全相同类型参数的IMap的任何非IMyMap实现。
public class MyMapWrapper : IMyMap
{
  private IMap<int, IEnumerable<SomeOtherType>> wrapped;
  public MyMapWrapper(IMap<int, IEnumerable<SomeOtherType>> wrapped)
  { this.wrapped = wrapped; }

  public IEnumerable<SomeOtherType> Map(int source) { return wrapped.Map(source); }
}
using IMyMap = IMap<int, IEnumerable<SomeOtherType>>;
using System.Collections.Generic;
using IMyMap = SomeExample.IMap<int, System.Collections.Generic.IEnumerable<bool>>;

namespace SomeExample
{
    public interface IMap<T, S>
    {
        T Map(S source);
    }

    public class ExampleUsage
    {
        public IMyMap Foo { get; set; }

        public void SetFoo()
        {
            Foo = (IMyMap) new object();
            Foo = (IMap<int, IEnumerable<bool>>) new object(); // Same thing
        }
    }
}
public interface IMap<S, T>
{
    T Map(S source);
}

public class SomeClass { }
public class ThatClass { }

public interface IMapOfSomething : IMap<SomeClass, ThatClass> { }

public class Map : IMapOfSomething
{
    ThatClass IMap<SomeClass, ThatClass>.Map(SomeClass source)
    {
        throw new NotImplementedException();
    }
}