为什么下面的代码需要指定泛型类型

发布于 2024-11-02 05:08:47 字数 683 浏览 0 评论 0原文

我有一个接口

public interface ITcpSerializable<T>
{
    Byte[] Serialize();
    T Deserialize(Byte[] data);
}

,在我的一个单独的类中,我希望公开以下属性。

public List<ITcpSerializable> RegisteredTypes { get; set; }

问题是我收到以下错误。

使用泛型类型 “ITcpSerialized”需要 1 种类型 论据

以及如何纠正它,但问题是我不希望将 RegisteredTypes 属性限制为 ITcpSerialized 接口的特定类型实现。

有办法解决这个问题吗?希望我想要实现的目标是明确的。

编辑:好的,我已经完全塞满了我想要解释的内容。刚点击了一下,我的想法完全扭曲了。请参阅此问题了解我实际要问的内容: 将公共财产限制为特定List中的类型

I have an interface

public interface ITcpSerializable<T>
{
    Byte[] Serialize();
    T Deserialize(Byte[] data);
}

In a seperate class of mine I wish to expose a following property.

public List<ITcpSerializable> RegisteredTypes { get; set; }

The problem is I am getting the following error.

Using the generic type
'ITcpSerializable' requires 1 type
arguments

Now I understand the error and how I could correct it but the problem is I do not wish to restrict my RegisteredTypes property to a specific typed implementation of my ITcpSerializable interface.

Is there a way around this problem? Hopefully what I am trying to accomplish is clear.

EDIT: OK i have completely stuffed up what I was trying to explain. Just clicked that my thinking was completely skewed. Please see this question for what I was actually asking: Constrain public property to specific types in List<Type>

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

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

发布评论

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

评论(4

迎风吟唱 2024-11-09 05:08:47

您必须创建 ITcpSerializedITcpSerialized 形式。然后用你的通用版本继承它。

interface ITcpSerializable { }
interface ITcpSerializable<T> : ITcpSerializable { }

You have to make a ITcpSerializable form of ITcpSerializable<T>. Then inherit from that with your generic version.

interface ITcpSerializable { }
interface ITcpSerializable<T> : ITcpSerializable { }
自我难过 2024-11-09 05:08:47

您可以创建非泛型 ITcpSerialized 并从新的非泛型接口继承 ITcpSerialized

You can create a non-generic ITcpSerializable and inherit ITcpSerializable<T> from the new non-generic interface.

静谧 2024-11-09 05:08:47

接口中的通用类型参数定义旨在在反序列化给定类型的对象时需要特定类型的类型安全结果。

特定的用例对我来说没有意义,因为您需要有一个对象的实例才能反序列化不同实例 - 这通常不是您想要的方式去。

我知道您想要要求有一个强类型反序列化器,但使用其他模式(如工厂)是有意义的

此外,您的输入数据是一个 byte[] - 除非有其他元数据,否则您没有办法知道需要调用哪个 ITCPSerialized 实现器才能获得正确的具体类型。

类型信息可以包含在数据中(即包含类型信息的某个标头)或通过某种其他契约(即始终相同的类型)。

序列化方法确实有意义 - 因为向对象询问其自身的序列化实例是合理的,但相反的方式通常是通过其他方式完成的。

因此,问题的解决方案是删除泛型类型定义(这对您没有帮助)和 Deserialize 方法(在这种情况下没有意义),而只保留 Serialize 方法。

如:

 public interface ITcpSerializable
{
    Byte[] Serialize();
}

并使用工厂实现反序列化。

The Generic type argument definition in the interface is designed to require type safe results of a specific type when deserializing an object of a given type.

The particular use-case doesn't make sense to me, because you'd need to have an instance of an object in order to deserialize a different instance - which is not usually the way you'd want to go.

I understand you want to require that there's a strongly typed deserializer, but it would make sense to use some other pattern (like a Factory)

Furthermore, your input data is a byte[] - unless there is some other metadata, you have no way of knowing which implementor of ITCPSerializable needs to be called in order to get the correct concrete type.

The type information may be contained in the data (i.e. some header that includes type information) or by some other contract (i.e. always the same type).

The serialize method DOES make sense - because it's reasonable to ask an object for a serialized instance of itself, but the other way around is usually done by some other means.

So the solution to your problem would be to remove the generic type definition (which doesn't help you) and the Deserialize method (which doesn't make sense in this context), and just keep the Serialize method.

as in:

 public interface ITcpSerializable
{
    Byte[] Serialize();
}

And implement the Deserialize by using a factory.

总以为 2024-11-09 05:08:47

您可能还想让您的单独的类变得通用,例如:

public class MyClass<T> {
    public List<ITcpSerializable<T>> RegisteredTypes { get; set; }
}

You probably want to make your separate class generic also then, like:

public class MyClass<T> {
    public List<ITcpSerializable<T>> RegisteredTypes { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文