简体   繁体   English

使用双泛型的C#类:当两个泛型相同时,仅给出一个?

[英]C# Class using double Generics: give only one when both of them should be the same?

So I'm struggling a bit with a certain piece of code, and I'm not sure if there's a better way to handle it - but it feels like it's possible. 所以我在某些代码上有些挣扎,我不确定是否有更好的方法来处理它-但这似乎是可能的。

Let's say I have a class: 假设我有一堂课:

public class DeviceProcessingResult<TSucceeded, TFailed>
    {
        public TSucceeded[] Succeeded { get; set; }

        public TFailed[] Failed { get; set; }

        public DeviceProcessingResult(IEnumerable<TSucceeded> succeeded, IEnumerable<TFailed> failed)
        {
            Succeeded = succeeded.Cast<TSucceeded>().ToArray();
            Failed = failed.Cast<TFailed>().ToArray();
        }
    }

I use this class to create objects that contain an Array of object that have succeeded in their processing, and an Array of objects that have failed. 我使用此类创建包含处理成功的对象数组和失败的对象数组的对象。 This type of object gets used on various parts of the code we currently have. 这种类型的对象可用于我们当前拥有的代码的各个部分。 In one of those places, we would use it like this: 在这些地方之一中,我们将这样使用它:

return new DeviceProcessingResult<DeviceResponse, DeviceModel>(succeeded, failed);

Since we need to return the results, and the type of object that gets returned when processing has succeeded contains other/more info than the model that gets returned when processing has failed. 由于我们需要返回结果,因此处理成功时返回的对象类型包含的信息比处理失败时返回的模型的信息更多/更多。

Now this is currently the only piece of code that has 2 different types of objects, as all other code looks like this: 现在,这是唯一具有两种不同类型对象的代码,因为所有其他代码如下所示:

return new DeviceProcessingResult<DeviceModel, DeviceModel>(succeeded, failed);

So as you see, we use 2 times the same object. 如您所见,我们使用相同对象的2倍。

The point of this question now is: Is it possible to do something like 现在,这个问题的重点是:是否可以做类似的事情

return new DeviceProcessingResult<DeviceModel>(succeeded, failed);

Since it's gonna be the same things there anyways? 既然那里总会有一样的东西? Or is there no (good) way around this and should we always specify both types? 还是没有(好的)解决方法,我们是否应该始终指定两种类型?

The easy solution would be to simply inherit this class like this: 一个简单的解决方案是像这样简单地继承此类:

public class DeviceProcessingResult<T> : DeviceProcessingResult<T, T>
{
    public DeviceProcessingResult(IEnumerable<T> succeeded, IEnumerable<T> failed)
        : base(succeeded, failed)
    { /*Nothing more to do here...*/ }
}

public class DeviceProcessingResult<T, T1>
{
    public T[] Succeeded { get; set; }
    public T1[] Failed { get; set; }

    public DeviceProcessingResult(IEnumerable<T> succeeded, IEnumerable<T1> failed)
    {
        Succeeded = succeeded.ToArray();
        Failed = failed.ToArray();
    }
}

To do this you would need to have another intermediate class, for example: 为此,您将需要另一个中间类,例如:

public class public class DeviceProcessingResult<TDeviceModel> 
    : DeviceProcessingResult<TDeviceModel, TDeviceModel>
{
}

Alternatively, you can rely on the compiler inferring the types based on the parameters: 或者,您可以依靠编译器根据参数推断类型:

return new DeviceProcessingResult(succeeded, failed)

What about: 关于什么:

    public class DeviceProcessingResult<TFailed> : DeviceProcessingResult< DeviceResponse, TFailed>
    {
        public DeviceProcessingResult(IEnumerable<DeviceResponse> succeeded, IEnumerable<TFailed> failed) : base(succeeded, failed)
        {
        }
    }

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

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