简体   繁体   English

通用 class 中未使用的类型参数

[英]unused type parameter in generic class

Lets say I have classes like the following:可以说我有如下课程:

public class Input<T>
{
    // some data here but type T is never directly used

    internal void Foo()
    {
        object output = someOther.CreateOutput(this);
        .....
    }
}
public class Output<T>
{
    public T Value { get; }
    //.....
}
class SomeOtherClass
{
    public Output<T> CreateOutput<T>(Input<T> input)
    {
       // type T is used here
    }
}

Resharper gives a warning here that the generic type parameter T in class Input is not used. Resharper 在这里给出警告,class 输入中的泛型类型参数 T 未使用。 However it is used indirectly in Foo().但是它在 Foo() 中间接使用。

My question is, can i safely ignore this Resharper warning, or is there some issue with this design?我的问题是,我可以安全地忽略这个 Resharper 警告,还是这个设计有问题? Basically I want an instance of class Input to contain information about type T so that a particular instance of Input is used to create only a particular type of Output.基本上我想要一个 class Input 的实例来包含有关类型 T 的信息,以便使用 Input 的特定实例仅创建特定类型的 Output。 This might also be used to diagnose an error when output creation fails and i have access to an instance of Input (eg inside an exception) and would like to know what kind of output was getting created.当 output 创建失败并且我可以访问 Input 实例(例如在异常中)并且想知道正在创建哪种 output 时,这也可能用于诊断错误。

I guess to rephrase the question, is it an acceptable design to have a generic class whose type parameter defines its behavior when passed to other classes, but the type parameter is not used to define any data within this class?我想换个说法,拥有一个通用的 class 其类型参数定义其在传递给其他类时的行为,但类型参数不用于定义此 class 中的任何数据是否是可接受的设计?

What you wrote is a complicated version of:你写的是一个复杂的版本:

public class Input
{
    internal void Foo<T>()
    {
        object output = someOther.CreateOutput<T>(this);
        .....
    }
}
public class Output<T>
{
    public T Value { get; }
    //.....
}
class SomeOtherClass
{
    public Output<T> CreateOutput<T>(Input input)
    {
       // type T is used here
    }
}

Since your Input class does not carry any information related to T there is no reason for it to be generic.由于您的Input class 不携带任何与T相关的信息,因此没有理由将其通用。 What you wrote is not technically incorrect, but you're introducing an artificial restriction that a given input can only be Foo d for the concrete T it was constructed with.您写的内容在技术上并非不正确,但是您引入了一个人为限制,即给定输入只能是Foo d 用于构造它的具体T Artificial, since nothing in the data that Input carries depends on T in any way.人为的,因为Input携带的数据中没有任何内容以任何方式依赖于T

Is this bad?这很糟糕吗? Dunno, did you intend it to be that way?不知道,你打算这样吗? If the constraint created this way is what you want, then suppress the warning.如果以这种方式创建的约束您想要的,则禁止显示警告。 If it is not then change the design.如果不是,则更改设计。 That's why it's a warning, there's a good chance someone would write this unknowingly.这就是为什么它是一个警告,很有可能有人会在不知不觉中写这个。

I guess to rephrase the question, is it an acceptable design to have a generic class whose type parameter defines its behavior when passed to other classes, but the type parameter is not used to define any data within this class?我想换个说法,拥有一个通用的 class 其类型参数定义其在传递给其他类时的行为,但类型参数不用于定义此 class 中的任何数据是否是可接受的设计?

I would say this should probably be fine.我会说这应该没问题。 It is difficult to tell without more details about your particular use case since the CreateOutput method is not shown.由于未显示 CreateOutput 方法,因此如果没有有关您的特定用例的更多详细信息,就很难判断。

I have done similar things to ensure types are explicit.我做了类似的事情来确保类型是明确的。 For example Id<T> wrapper around a Guid to help ensure Id<Orange> is not passed into a method expecting an Id<Apple> .例如,围绕 Guid 的Id<T>包装器有助于确保Id<Orange>不会传递到需要Id<Apple>的方法中。

you might also consider storing the type as a property rather than a generic type parameter.您也可以考虑将类型存储为属性而不是泛型类型参数。 Ie IE

public class Input
{ 
     private Type myType;

This might make it easier to forward the type to CreateOutput without having to make all as many methods generic, and this can have benefits and downsides depending on how you use it.这可能会使将类型转发到 CreateOutput 变得更容易,而不必使所有方法都通用,这可能有好处和坏处,具体取决于您如何使用它。

Resharper warnings are nice to have, but I would worry to much about them. Resharper 警告很好,但我会非常担心它们。 I would be more concerned about actual compiler warnings.我会更关心实际的编译器警告。

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

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