从泛型方法返回对象作为接口

发布于 2025-01-04 18:20:43 字数 1088 浏览 1 评论 0原文

我有一个接口 InterfaceBase 和一些从它派生的接口 Interface1、Interface2。接下来,我有一些实现 InterfaceX 接口的类,而不是基本接口。

现在,我是泛型的初学者,很多新方法在我的头脑中造成了很大的混乱:(。我想创建工厂(静态类),我称之为类似

Interface1 concrete1 = Factory.Get<Interface1>();

以下是我的工厂(示例)实现,这并不工作:

  public static class Factory {

    public static T Get<T>() where T: InterfaceBase{

      Type type = typeof(T);

      //return new Concrete1() as T; // type T cannot be used with the as
      //return new Concrete1() as type; //type not found
      //return new Concrete1(); // cannot implicitly convert
      //return new Concrete1() as InterfaceBase; //cannot convert IBase to T
      //return new Concrete1() as Interface1; //cannot convert Interface1 to T
    }
  }

我想要实现的是从应用程序的其余部分隐藏类(它们是Web服务处理程序),以便轻松地交换它们,因为这些类将是单例,并且它们将存储在工厂内的字典中,这样工厂就可以将它们分散到各地通过此方法应用程序,但作为接口.. 也许我没有正确使用约束 我做错了什么吗?我的方法不好吗?有没有更好的东西,也许整个架构应该重新设计?图表以更好地显示架构。工厂不在里面

I have one interface InterfaceBase and some interfaces derived from it Interface1, Interface2. Next I have classes that are implementing the InterfaceX interfaces, not the base one.

Now, i am beginner in generics and so many new approaches in this made great mess in my head :( . I want to create factory (static class) where I call something like

Interface1 concrete1 = Factory.Get<Interface1>();

Here is my (sample) implementation of factory, that does not work:

  public static class Factory {

    public static T Get<T>() where T: InterfaceBase{

      Type type = typeof(T);

      //return new Concrete1() as T; // type T cannot be used with the as
      //return new Concrete1() as type; //type not found
      //return new Concrete1(); // cannot implicitly convert
      //return new Concrete1() as InterfaceBase; //cannot convert IBase to T
      //return new Concrete1() as Interface1; //cannot convert Interface1 to T
    }
  }

What I want to achieve is hide the classes (they are webservice handlers) from the rest of the application to exchange them lightly. I wanted use the factory as the classes will be singletons and they will be stored in Dictionary inside the factory, so the factory can spread them across the application through this method, but as interfaces..
Maybe i am not using the constraints correctly
am I doing smthing wrong? is my approach bad? can be there something better, maybe the whole architecture shoul be reworked? diagram to show better the architecture. The factory is not in it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

紧拥背影 2025-01-11 18:20:43

我认为您正在寻找的是“穷人依赖注入”。我想你应该使用一个真正的 IoC 容器,有很多选择(Unity、Castle Windsor、Ninject...)。

但无论如何,如果您坚持自己做,请遵循@Sergey Kudriavtsev 的推荐。只需确保对于每个接口,您返回正确的具体类。像这样:

public interface InterfaceBase { }
public interface Interface1 : InterfaceBase { }
public interface InterfaceX : InterfaceBase { }

public class Concrete1 : Interface1 { }
public class ConcreteX : InterfaceX { }

public static class Factory
{
    public static T Get<T>()
        where T : InterfaceBase
    {
        if (typeof(Interface1).IsAssignableFrom(typeof(T)))
        {
            return (T)(InterfaceBase)new Concrete1();
        }
        // ...
        else if (typeof(InterfaceX).IsAssignableFrom(typeof(T)))
        {
            return (T)(InterfaceBase)new ConcreteX();
        }

        throw new ArgumentException("Invalid type " + typeof(T).Name, "T"); // Avoids "not all code paths return a value".
    }
}

你通过将接口引用传递给工厂来调用它:

var instance = factory.Get<Interface1>();

Methinks what you are looking for is a "Poor-Man Dependency Injection". I guess you should use a real IoC container for that, there are a lot of options (Unity, Castle Windsor, Ninject...).

But anyway, If you insist in doing it by yourself, go with what @Sergey Kudriavtsev is recomending. Just make sure that, for each interface, you return the proper concrete class. Something like this:

public interface InterfaceBase { }
public interface Interface1 : InterfaceBase { }
public interface InterfaceX : InterfaceBase { }

public class Concrete1 : Interface1 { }
public class ConcreteX : InterfaceX { }

public static class Factory
{
    public static T Get<T>()
        where T : InterfaceBase
    {
        if (typeof(Interface1).IsAssignableFrom(typeof(T)))
        {
            return (T)(InterfaceBase)new Concrete1();
        }
        // ...
        else if (typeof(InterfaceX).IsAssignableFrom(typeof(T)))
        {
            return (T)(InterfaceBase)new ConcreteX();
        }

        throw new ArgumentException("Invalid type " + typeof(T).Name, "T"); // Avoids "not all code paths return a value".
    }
}

And you call it by passing the interface reference to the factory:

var instance = factory.Get<Interface1>();
沙沙粒小 2025-01-11 18:20:43

这应该可行:

return (T)(new Concrete1());

此外,调用工厂方法的代码应该如下所示:

Interface1 concrete1 = Factory.Get<Interface1>();

This should work:

return (T)(new Concrete1());

Also the code for calling factory method should be like this:

Interface1 concrete1 = Factory.Get<Interface1>();
很快妥协 2025-01-11 18:20:43

回答您问题的一部分:

return new Concrete1() as T; // type T cannot be used with the as 

类型 T 不能与 as 一起使用,因为 T 未知为引用类型。您可以使用 where T : class [, ...] 约束将类约束为引用类型。这将允许您使用 as 运算符,当然,假设您不需要能够将此方法与值类型一起使用。

编辑

话虽如此,我更喜欢rsenna的答案。由于您在编译时知道直接转换会起作用,因此使用直接转换比 as 更有意义。 我也同意他的建议,即您研究“真正的”IoC 容器,但我想补充一点,当您了解泛型时,对泛型的深入理解将对您非常有用。既然您说您是泛型的初学者,那么像这样的练习可能是一个好主意,因为它将帮助您了解泛型,并让您更好地理解 IoC 容器的价值可以添加。

编辑2

我看到另一个问题:您将 T 约束为 InterfaceBase,然后将 Concrete1 转换为 T。不知道 Concrete1 是从 T 派生的!这当然就是您使用 as 转换的原因。所以答案是,添加 class 约束就可以了。

编辑3

正如rsenna指出的,您还可以通过向上和向下进行类型检查:

return (T)(object)new Concrete1();

或者

return (T)(InterfaceBase)new Concrete1();

我想知道这在效率方面与

return new Concrete1() as T;

我今天晚些时候检查(如果我有时间的话) 相比如何这样做。

To answer one part of your question:

return new Concrete1() as T; // type T cannot be used with the as 

Type T cannot be used with as because T is not known to be a reference type. You can constrain the class to be a reference type with the where T : class [, ...] constraint. This will allow you to use the as operator, assuming, of course, that you don't need to be able to use this method with value types.

EDIT

Having said that, I prefer rsenna's answer. Since you know at compile time that the direct cast will work, it makes more sense to use a direct cast than as. I also agree with his recommendation that you investigate "real" IoC containers, but I would add that a strong understanding of generics will be very useful to you as you learn about them. Since you say that you are a beginner in generics, an exercise like this one is probably a good idea, because it will help you learn about generics, and give you a better appreciation for the value that IoC containers can add.

EDIT 2

I see another problem: You constrain T to be InterfaceBase, and then you cast Concrete1 to T. Concrete1 is not known to derive from T! Which is of course why you use the as cast. So the answer is, add the class constraint and you should be fine.

EDIT 3

As rsenna points out, you can also get a runtime type check with an upcast and downcast:

return (T)(object)new Concrete1();

or

return (T)(InterfaceBase)new Concrete1();

I wonder how this compares in terms of efficiency with

return new Concrete1() as T;

I'll check later today if I find some time to do so.

花心好男孩 2025-01-11 18:20:43

做这样的事情

public static class Factory {

public static T Get<T>() where T: InterfaceBase{

  return (T) new Concrete1();
}

不能安全地输入。您不能保证将在 T == Concrete1 的情况下调用该方法。 T 可以是 InterfaceBase 的任何子类型,Concrete1 只是这些子类型之一,不一定是相同的 T,因此编译器不会允许您转换为 T,就像它不允许您转换为字符串或任何其他类型一样其他不相关的类型。

Activator.CreateInstance() 是处理该问题的一种方法:CreateInstance 确实保证构建的实例是 T 类型,这是该方法的预期输出值。

Doing something like this

public static class Factory {

public static T Get<T>() where T: InterfaceBase{

  return (T) new Concrete1();
}

Cannot be typed safely. You cannot guarantee that the method will be invoked with T == Concrete1. T can be ANY subtype of InterfaceBase, Concrete1 is just one of those subtypes and not necessarily the same T, therefore the compiler won't allow you to cast to T, the same way it won't allow you to cast to string or any other unrelated type.

Activator.CreateInstance() is one way of handling that: CreateInstance does guarantee that the built instance is of type T, which is the expected output value of the method.

酒废 2025-01-11 18:20:43

如果您知道 T 是 InterfaceBase 的子类型(其中 T : InterfaceBase),那么您可以将 Get 方法的返回类型设置为 InterfaceBase:

public static InterfaceBase Get<T>() where T : InterfaceBase
{        
    return new Concrete1();
}

可以使用 InterfaceBase 的子接口作为 T 来调用此方法:

InterfaceBase ib = Factory.Get<Interface1>();

如果您传递以下任何内容 : InterfaceBase 的子接口作为 T 编译器会抱怨。

我认为这是一个更好的方法,但正如 @phhoog 指出的那样,它仅适用于 T 类型参数的具体类:

public static T Get<T>() where T : InterfaceBase
{
    Type type = typeof(T);
    if (t.IsAbstract || t.IsInterface)
{
        throw new ArgumentException(@"Only non-abstract classes supported as T type parameter.");
}
    return Activator.CreateInstance<T>();
}

If you know that T is a subtype of InterfaceBase (where T : InterfaceBase) then you can make the return type of the Get method be InterfaceBase:

public static InterfaceBase Get<T>() where T : InterfaceBase
{        
    return new Concrete1();
}

This method can be invoked using a sub-interface of InterfaceBase as T:

InterfaceBase ib = Factory.Get<Interface1>();

If you pass anything but a sub-interface of InterfaceBase as T the compiler will complain.

I think this is a better approach but as @phoog points it only works with concrete classes for the T type parameter:

public static T Get<T>() where T : InterfaceBase
{
    Type type = typeof(T);
    if (t.IsAbstract || t.IsInterface)
{
        throw new ArgumentException(@"Only non-abstract classes supported as T type parameter.");
}
    return Activator.CreateInstance<T>();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文