更新时间:2023-02-09 14:18:51
泛型版本继承的接口的泛型版本,这会像往常一样强制实现类。
interface ITypeSerializer {}
$ p $ >
接口ITypeSerializer< T> :ITypeSerializer {} $ b $ class StringSerializer:ITypeSerializer< string> {} //实现ITypeSerializer,也是
//非泛型数组
ITypeSerializer [] serializers = ...在
ITypeSerializer
上需要的任何方法应接受或返回object
,实现通常是通用版本的传递方法。一个实现示例:
public interface ITypeSerializer
{
void Write(BinaryWriter writer,object obj);
对象读取(BinaryReader reader);
}
public interface ITypeSerializer< T> :ITypeSerializer
{
void Write(BinaryWriter writer,T obj);
T读取(BinaryReader reader);
}当您执行
ITypeSerializer< T> $ c>,您还需要为
ITypeSerializer
方法提供实现。一个常见的模式是隐式地实现泛型和非泛型泛型,这样非泛型方法仅在通过接口引用类时才可见。class StringSerializer:ITypeSerializer< string>
{
// ITypeSerializer的隐式实现< string>
public void Write(BinaryWriter writer,string obj)
{
//在这里提供核心实现
}
public string Read(BinaryReader reader)
{
//在这里提供核心实现
}
// ITypeSerializer的显式实现
void ITypeSerializer.Write(BinaryWriter writer,object obj)
{
if(!(obj is string))throw new ArgumentException(obj);
this.Write(writer,(string)obj);
object ITypeSerializer.Read(BinaryReader reader)
{
return this.Read(reader);请注意非泛型方法是如何传递给/来自通用同行。
I am a Java guy, and trying to implement some code in C#. I did a research for an hour but couldn't find any. I have an interface that I declared as Generic called
interface TypeSerializer<T>
Then I have two implementation of this.
class StringSerializer: TypeSerializer<String>
and
class ByteArraySerializer: TypeSerializer<byte[]>
Now I want to create an array with these instances and refer to array as TypeSerializer. Sth like this:
TypeSerializer[] serializers = new TypeSerializer[2] {new ByteArraySerializer(), new StringSerializer()};
This code doesn't compile, however in Java a similar code does compile.
I also tried sth
TypeSerializer<Object>[] serializers = new TypeSerializer<Object>[2] {new ByteArraySerializer(), new StringSerializer()};
still no way.
How should I declare this array and reach to the instances without knowing the Generic Type.
EDIT
Based on the answers below it becomes clear that I should have two interfaces; with and without generic. Here is the Interface methods. Could you please show also how to implement them.
public interface ITypeSerializer<T>:TypeSerializer { void write(BinaryWriter writer, T obj); T read(BinaryReader reader); }
You can create a non-generic version of the interface that the generic version inherits, which forces the implementations to the classes as usual.
interface ITypeSerializer { } interface ITypeSerializer<T> : ITypeSerializer { } class StringSerializer : ITypeSerializer<string> { } // implements ITypeSerializer, too // non-generic array ITypeSerializer[] serializers = ...
Any method that you need on
ITypeSerializer
should either accept or returnobject
, with the implementations typically being pass-through methods for the generic versions.An implementation example:
public interface ITypeSerializer { void Write(BinaryWriter writer, object obj); object Read(BinaryReader reader); } public interface ITypeSerializer<T> : ITypeSerializer { void Write(BinaryWriter writer, T obj); T Read(BinaryReader reader); }
When you implement
ITypeSerializer<T>
, you also need to provide implementations for theITypeSerializer
methods. A common pattern is to implement the generic implicitly and the non-generic explicitly, so that the non-generic methods are only visible when referring to the class via the interface.class StringSerializer : ITypeSerializer<string> { // implicit implementation of ITypeSerializer<string> public void Write(BinaryWriter writer, string obj) { // provide core implementation here } public string Read(BinaryReader reader) { // provide core implementation here } // explicit implementation of ITypeSerializer void ITypeSerializer.Write(BinaryWriter writer, object obj) { if (!(obj is string)) throw new ArgumentException("obj"); this.Write(writer, (string)obj); } object ITypeSerializer.Read(BinaryReader reader) { return this.Read(reader); } }
Notice how the non-generic methods are simply passing to/from the generic counterparts.