简体   繁体   English

通过通用接口包装C#类以与多态性一起使用

[英]Wrapping C# classes to use with polymorphism through a common interface

I've got several C# classes each with similar properties. 我有几个C#类,每个类都有类似的属性。 (They're part of an SDK and their code can't be changed.) (它们是SDK的一部分,它们的代码无法更改。)

  • Person.Name Person.Name
  • Product.Name 产品名称
  • Order.Name Order.Name

I want to use these classes polymorphically, but they don't implement a common interface or derive from a common base class, so that's not possible. 我希望以多态方式使用这些类,但它们不实现公共接口或派生自公共基类,因此这是不可能的。 To get around this, I'd like to wrap each one in another class that does implement a common interface, and wire-up each class property to its corresponding interface property. 为了解决这个问题,我想将每一个包装在另一个实现公共接口的类中,并将每个类属性连接到相应的接口属性。

What would be a suitable name for the wrapper classes? 什么是包装类的合适名称? Wrapper, Decorator, Adaptor, Proxy? 包装,装饰,适配器,代理? Does this pattern have a name? 这个模式有名字吗? Is there a better approach? 有更好的方法吗?

(I don't want to use dynamic duck-typing or an impromptu interface .) (我不想使用动态鸭子打字或即兴界面 。)

它看起来像Adapter,因为您正在调整现有接口以满足特定要求。

(I don't want to use dynamic duck-typing or an impromptu interface.) (我不想使用动态鸭子打字或即兴界面。)

So what is wrong with a NamedObject? 那么NamedObject有什么问题?

public class NamedObject
{
    public string Name { get; set; }
}

It literally says what it is, nothing less, nothing more. 从字面上说它是什么,没有更少,仅此而已。

I'd stick with CodeCaster's idea, and perhaps with a dash of Func<T> for no other reason than I get withdrawal symptoms when I don't use angle brackets ... 我坚持使用CodeCaster的想法,也许还有一些Func<T> ,除了我没有使用尖括号时出现戒断症状,​​没有其他原因 ......

public class NamedEntity
{
    public string Name { get { return _getName(); } }

    private Func<string> _getName;

    public NamedObject(Func<string> getName)
    {
        _getName = getName;
    }
}

And then call thus: 然后打电话:

var named = new[] 
    { 
        new NamedEntity(() => person.Name),
        new NamedEntity(() => product.Name),
        new NamedEntity(() => order.Name)
    };

The added benefit with this is when the value of the property changes on the target object, it changes within the NamedEntity reference too via the Func , this means within the life span of the objects you can get away with wrapping them once. 这样做的NamedEntity好处是,当属性的值在目标对象上发生变化时,它也会在NamedEntity引用中通过Func更改,这意味着在对象的生命周期内,您可以将它们包装一次。 You can also do the inverse with Func s that set values as well as get, and can adapt more properties. 您也可以使用Func来设置值和get,并且可以调整更多属性。

I am not immediately sure what pattern this represents (if any), though I would guess Adapter pattern (which is a type of wrapper pattern). 我不会立即确定这代表什么样的模式(如果有的话),虽然我会猜测适配器模式(这是一种包装模式)。 However, it could also be argued to be a Proxy pattern . 但是,它也可以被认为是代理模式 Not sure really. 真的不确定。

也许你可以只更改名称空间并保留原始类的名称。

Technically, I think the most correct name would be Adapter, see this question. 从技术上讲,我认为最正确的名称是Adapter,请看这个问题。

Adapter is used when you have an abstract interface, and you want to map that interface to another object which has similar functional role, but a different interface. 当您具有抽象接口时,将使用适配器,并且您希望将该接口映射到具有类似功能角色但具有不同接口的另一个对象。

You don't have abstract interface, but "similar functional role, but a different interface". 你没有抽象的界面,但是“类似的功能角色,但是不同的界面”。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM