是否可以使对象公开类型参数的接口? [英] Is it possible to make an object expose the interface of an type parameter?

查看:94
本文介绍了是否可以使对象公开类型参数的接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,是否可以这样写:

In C#, is it possible to write something like this:

public class MyClass<T> : T 
    where T : class, new() 
{
}

我知道上面的实现无法编译,但是我实际上试图实现的是将某种通用包装器实现为未知类型,以便客户端可以像调用该类型一样调用包装器,只要通过参数 T 来代替,而不是使用 wrapper.Instance.SomeMember()之类的东西来调用它。

I know that the above implementation does not compile, but what I am actually trying to achive is implementing some kind of generic wrapper to an unknown type, so that an client can call the wrapper just as he would call the type, provided by the parameter T, instead of calling it using something like wrapper.Instance.SomeMember().

谢谢!

推荐答案

这是不可能的。

在我看来,我不认为应该使用继承来实现包装器。

In my opinion, I don't think that a wrapper should be implemented using inheritance.

例如,假设我们有一个 Engine 类,您需要实现一个 FerrariEngine 。并且您有一个 Car 类。

For example, let's say we've an Engine class and you need to implement a FerrariEngine. And you have a Car class.

您是说 Car 应该继承 FerrariEngine

在一天结束时,您正在寻找使用继承进行依赖注入的操作,然后再次,这不是正确的道路。

At the end of the day, you're looking to do something like dependency injection using inheritance and, again, this isn't the right path.

我的建议是不要试图让您的生活更轻松:基于理性的观点来决定一种体系结构。

My suggestion is don't try to make your life easier: decide an architecture based on rational points.

OP在一些评论中表示:

The OP said in some comment:


我想让此类管理T类型的对象的实例,因此当实例需要创建
时,客户端不需要照顾的

I want to make this class to manage instances of objects of type T, so that the client does not need to take care of when the instances need to be created.

您不需要做任何奇怪的事情就能得到想要的东西:

You don't need to make strange things to get what you want:

public interface IEngine 
{
     void Start();
}

public sealed class FerrariEngine : IEngine
{
     public FerrariEngine()
     {
          Start();
     }

     public void Start()
     {
     }
}

public abstract class Car<TEngine> where TEngine: IEngine, new()
{
    public Car()
    {
        _engine = new Lazy<TEngine>(() => new TEngine());
    }

    private readonly Lazy<TEngine> _engine;

    public TEngine Engine
    {
        get { return _engine.Value; }
    }
}

public class FerrariCar : Car<FerrariEngine>
{
}

最后,如果我们创建<$ c的实例$ c> FerrariCar :

Car<FerrariEngine> myFerrari = new FerrariCar();

引擎将被实例化并启动,而无需开发人员干预!

The engine will be instantiated and started, without developer intervention!

检查 Lazy< T> 和基本通用约束是如何完成工作的;)

Check how Lazy<T> and basic generic constraints make the job ;)

在摘要:


  • 使用 Lazy< T> 引擎只会在某些情况下实例化访问 Engine 属性。

  • 实例化惰性加载的引擎,因为 FerrariEngine 实现一个无参数构造函数,该函数本身会调用 Start(),它将启动引擎。

  • Using Lazy<T> the engine will be instantiated only when some access the Engine property.
  • Once the lazy-loaded engine is instantiated, since FerrariEngine implements a parameterless constructor calling Start() itself, it will start the engine.

我相信此示例向您说明了如何获取所需的内容并按原样使用C#!

I believe that this sample illustrates you how you can get what you're looking for and using C# "as is"!

这篇关于是否可以使对象公开类型参数的接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆