一个列表中的多个结构类型
Multiple Struct Types in One List
有没有办法使用 classes here 中描述的方法在列表中包含多个结构类型?
参考一个告诉我从抽象class(对于结构不可能)或接口继承的答案,我想到了这个:
public interface IMemorable {}
public struct Memorable<type> : IMemorable where type : struct
{
private type Data;
public Memorable<type> (type data)
{
Data = data;
}
}
并以这种方式实现列表:
List<Memorable> memorables;
memorables.Add (new Memorable<someStruct> ());
memorables.Add (new Memorable<otherStruct> (new otherStruct (6, "Six")));
哪个应该工作,因为它与 classes 一起工作,我认为结构确实支持接口的继承?
不过我收到了这个错误:
Assets/Test.cs(51,22): error CS0305: Using the generic type `Memorable<T>' requires `1' type argument(s)
在List声明行
或者你能给我一些更好的解决方案吗...关于性能,我可能需要存储数千个这样的结构。我不确定如果这种方法会以某种方式起作用,classes 从抽象 class 继承是否更合适...
你能给我建议吗?
是的(有一些小的更正),但是没有用:
public interface IMemorable {}
public struct Memorable<T> : IMemorable where T : struct
{
private T Data;
public Memorable(T data)
{
Data = data;
}
}
List<IMemorable> memorables = new List<IMemorable>();
memorables.Add(new Memorable<someStruct>(new someStruct()));
memorables.Add(new Memorable<otherStruct>(new otherStruct(6, "Six")));
(请注意,通常泛型类型称为 TSomething
(T
和 S
大写))
因为你只有一个 IMemorable
,你不能用它来访问 T Data
。
我要补充一点,一般来说,struct
(以及所有值类型)和接口不能很好地混合,因为将 struct
强制转换为接口会导致其装箱(因此创建结构的副本),只有一个例外(此处不相关)
错误消息告诉您在将 class Memorable<T>
用作 List<>
声明的类型参数时定义类型参数 T
。
通常不可能声明包含多个不同结构类型的列表,除非您声明该列表包含这些结构的任何公共父类型。那将是 object
、System.ValueType
(所有结构的直接父级 class)或任何公共声明的接口。
仅类型参数不同的两个通用类型不会自动具有比具有相同父级的任何其他两个类型更具体的通用类型 class。换句话说,具有不同 T
的各种 Memorable<T>
结构类型没有比任何其他两种结构类型更多的派生公共父级。任何可以包含它们两者的列表也将能够包含至少任何其他结构类型,并且仅以盒装形式,即作为引用,而不是值。 (这与将它们声明为结构体的想法相悖,除非您还有其他原因。)
有没有办法使用 classes here 中描述的方法在列表中包含多个结构类型?
参考一个告诉我从抽象class(对于结构不可能)或接口继承的答案,我想到了这个:
public interface IMemorable {}
public struct Memorable<type> : IMemorable where type : struct
{
private type Data;
public Memorable<type> (type data)
{
Data = data;
}
}
并以这种方式实现列表:
List<Memorable> memorables;
memorables.Add (new Memorable<someStruct> ());
memorables.Add (new Memorable<otherStruct> (new otherStruct (6, "Six")));
哪个应该工作,因为它与 classes 一起工作,我认为结构确实支持接口的继承?
不过我收到了这个错误:
Assets/Test.cs(51,22): error CS0305: Using the generic type `Memorable<T>' requires `1' type argument(s)
在List声明行
或者你能给我一些更好的解决方案吗...关于性能,我可能需要存储数千个这样的结构。我不确定如果这种方法会以某种方式起作用,classes 从抽象 class 继承是否更合适...
你能给我建议吗?
是的(有一些小的更正),但是没有用:
public interface IMemorable {}
public struct Memorable<T> : IMemorable where T : struct
{
private T Data;
public Memorable(T data)
{
Data = data;
}
}
List<IMemorable> memorables = new List<IMemorable>();
memorables.Add(new Memorable<someStruct>(new someStruct()));
memorables.Add(new Memorable<otherStruct>(new otherStruct(6, "Six")));
(请注意,通常泛型类型称为 TSomething
(T
和 S
大写))
因为你只有一个 IMemorable
,你不能用它来访问 T Data
。
我要补充一点,一般来说,struct
(以及所有值类型)和接口不能很好地混合,因为将 struct
强制转换为接口会导致其装箱(因此创建结构的副本),只有一个例外(此处不相关)
错误消息告诉您在将 class Memorable<T>
用作 List<>
声明的类型参数时定义类型参数 T
。
通常不可能声明包含多个不同结构类型的列表,除非您声明该列表包含这些结构的任何公共父类型。那将是 object
、System.ValueType
(所有结构的直接父级 class)或任何公共声明的接口。
仅类型参数不同的两个通用类型不会自动具有比具有相同父级的任何其他两个类型更具体的通用类型 class。换句话说,具有不同 T
的各种 Memorable<T>
结构类型没有比任何其他两种结构类型更多的派生公共父级。任何可以包含它们两者的列表也将能够包含至少任何其他结构类型,并且仅以盒装形式,即作为引用,而不是值。 (这与将它们声明为结构体的想法相悖,除非您还有其他原因。)