简体   繁体   English

C#:我可以扩展外部类,使其实现本地接口

[英]C#: Can I extend a foreign class such that it implements a local interface

I have 2 assemblies( foreign and local ). 我有2个程序集(国外和本地)。 The foreign assembly cannot be modified. 无法修改外部部件。 I would like to extend a foreign class such that it will now implement a local interface. 我想扩展一个外部类,以便它现在将实现一个本地接口。 Is this possible in C#? 这在C#中可能吗?

EDIT: I know that I can do the following: 编辑:我知道我可以执行以下操作:

// foreign.dll
namespace DX {
    public struct Vector3 {
        ...
    }
}

// local.dll
namespace MyDX {
    public static class Extensions {
        public static void MyExtensionMethod( this Vector3 vec, int index ){
            ...
        }
    }
}

What I'm interested in doing is forcing the foreign class/struct to implement a particular interface (without using inheritance ). 我感兴趣的是强制外部类/结构实现特定接口(不使用继承)。 I would like to use Vector3 as a type in a generic class: 我想将Vector3用作通用类中的类型:

public class MyGeneric<T> where T: IComparable<T> {
    T _value;
}

The above struct: Vector3 does not implement IComparable but I would like to use reflection to implement the interface so that I can use the foreign class/struct in its existing form (ie. w/o using inheritance). 上面的结构:Vector3没有实现IComparable,但是我想使用反射来实现接口,以便我可以以其现有形式使用外部类/结构(即不使用继承)。 Inheritance may be necessary... 继承可能是必要的...

Can I extend a foreign class such that it implements a local interface 我可以扩展外部类以使其实现本地接口吗

Sure, provided that the foreign class is not sealed. 当然可以,但前提是该外国类别未密封。 (And, of course, accessible to the local project, and so on.) (当然,本地项目也可以访问,依此类推。)

// Foreign.DLL
namespace F 
{ 
  public class B 
  {
    public void Foo() { }
  }
}

// Local.DLL
interface IFooBar 
{ 
  void Foo();
  void Bar();
}
class D : F.B, IFooBar
{
  public void Bar() { }
}

There, D has extended FB , as required, and D implements IFooBar , as required. 在那里, D根据需要扩展了FB ,并且D根据需要实现了IFooBar Notice how FBFoo meets the contractual obligation of IFooBar even though it is on the base class. 请注意,即使IFooBar位于基类上,它也如何FBFoo的合同义务。

If the foreign class is sealed then you'll have to use composition rather than inheritance. 如果外部类是密封的,那么您将不得不使用组合而不是继承。

However, I agree with the commenter that says that this smells like an XY problem. 但是,我同意评论员的说法,即闻起来像XY问题。 Can you say what problem you are really trying to solve, in more detail? 您能否更详细地说出您真正要解决的问题? Because there might be a better way to accomplish what you want. 因为可能会有更好的方法来完成您想要的。

In the narrow sense (like extension methods ), extension interfaces are currently not supported by the C# language. 从狭义上讲(类似于扩展方法 ),C#语言当前不支持扩展接口 But in a wider sense, you can use inheritance or composition as explained in @Eric's answer. 但是从更广泛的意义上讲,您可以按照@Eric的答案中所述使用继承或组合。

Supporting extension interfaces in C# has been briefly discussed in 2015 in Roslyn issue 3357 . 2015年, 罗斯林(Roslyn)第3357版简要讨论了在C#中支持扩展接口。 However, this can't be fully implemented by the C# compiler alone, as it requires CLR support to ensure reference equality between (ITheAddedInterface)x and x . 但是,这不能仅由C#编译器完全实现,因为它需要CLR支持以确保(ITheAddedInterface)xx之间的引用相等。 The topic seems to be abandoned for now. 该主题目前似乎已被放弃。

Other CIL languages such as F# have a much more flexible extension mechanism than C#. 其他CIL语言(例如F#)具有比C#更灵活的扩展机制。 But C# is catching up, as seen eg in C# language issue no. 但是C#正在迎头赶上,例如在C#语言第11期中看到的 164 , and the video A Preview of C# 8 . 164 ,以及视频C#8的预览

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎样才能拥有一个实现接口并继承自抽象 class 的 class? (C# .NET 3.5) - How can I have a class that implements an interface and inherits from an abstract class? (C# .NET 3.5) 如何编写在C#中实现给定接口的通用容器类? - How can I write a generic container class that implements a given interface in C#? 我可以定义一个 IDictionary 在 C# 中实现的接口吗? - Can I define an Interface that IDictionary implements in C#? 声明实现通用接口的 C# Class - Declare C# Class That Implements a Generic Interface C# 实现接口并扩展通用 class - C# implements interface and extends generic class c#实现接口的类中的虚方法 - c# virtual methods in class that implements interface 无法在C#中将实现接口的类的实例添加到所述接口的List中 - Can't add instance of class that implements an interface to a List of said interface in C# 如果我扩展ac#接口,我可以接听电话吗? - If I extend a c# interface, can I eat a call? C#:有没有一种方法可以将类强制转换为在运行时实现的接口,而不是在编译时实现的接口? - C#: Is there a way to cast a class to an interface it implements at runtime, but not compile time? C#相当于创建实现接口的匿名类 - C# equivalent of creating anonymous class that implements an interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM