类实例无法分配给继承的接口

时间:2017-08-14 07:10:21

标签: c# interface casting

我有两个接口声明和三个类声明,它们之间的关系是这样的:

interface IProtocolPackage
{
 ///some declaration
}
public class ProtocolPackage : IProtocolPackage
{
 ///implementation code for IProtocolPackage
}
interface IProtocolPackage<T> : IProtocolPackage
{
 ///some declaration
}
public class BytesProtocolPackage : ProtocolPackage, IProtocolPackage<byte[]>
{
///implementation code for IProtocolPackage<byte[]>
}
public class ChargingPileProtocolPackage : BytesProtocolPackage
{
///implementation code for IProtocolPackage<byte[]>
}

然后有一个方法将IProtocolPackage类型作为参数,如下所示:

public void DecodeCommand(IProtocolPackage<byte> package)
    {
        ///implementation code forDecodeCommand
    }

但编译器显示错误说: 无法从'ChargingPileCommandCoder.ChargingPileProtocolPackage'转换为'SHWDTech.Platform.ProtocolCoding.Coding.IProtocolPackage'ChargingPileCommandCoder

我以为我已经使用类ChargingPileProtocolPackage继承了接口IProtocolPackage。 这是怎么发生的?

发生错误的代码:

public IProtocolPackage DecodeProtocol(byte[] bufferBytes, Protocol matchedProtocol)
    {
        var package = new ChargingPileProtocolPackage { Protocol = matchedProtocol, ReceiveDateTime = DateTime.Now };

        var structures = matchedProtocol.ProtocolStructures.ToList();

        var currentIndex = 0;

        for (var i = 0; i < structures.Count; i++)
        {
            var structure = structures.First(obj => obj.StructureIndex == i);

            var componentDataLength = structure.StructureName == StructureNames.Data && structure.StructureDataLength == 0
                ? Globals.BytesToInt16(package["DataLength"].ComponentContent, 0, true)
                : structure.StructureDataLength;

            if (currentIndex + componentDataLength > bufferBytes.Length)
            {
                package.Status = PackageStatus.NoEnoughBuffer;
                return package;
            }

            if (structure.StructureName == StructureNames.Data)
            {
                DetectCommand(package, matchedProtocol);
                componentDataLength = package.Command.ReceiveBytesLength == 0 ? componentDataLength : package.Command.ReceiveBytesLength;
            }

            var component = new PackageComponent<byte[]>
            {
                ComponentName = structure.StructureName,
                DataType = structure.DataType,
                ComponentIndex = structure.StructureIndex,
                ComponentContent = bufferBytes.SubBytes(currentIndex, currentIndex + componentDataLength)
            };

            currentIndex += componentDataLength;

            package[structure.StructureName] = component;
        }

        DecodeCommand(package);

        return package;
    }

    public IProtocolPackage EncodeCommand(IProtocolCommand command, Dictionary<string, byte[]> paramBytes = null)
    {
        throw new NotImplementedException();
    }

    public void DoDelive(IProtocolPackage package, IPackageSource source)
    {
        throw new NotImplementedException();
    }

    public IDataConverter<byte> DataConverter { get; set; }

    public void DecodeCommand(IProtocolPackage<byte> package)
    {
        throw new NotImplementedException();
    }

    public void DetectCommand(IProtocolPackage<byte> package, IProtocol matchedProtocol)
    {
        throw new NotImplementedException();
    }
}

2 个答案:

答案 0 :(得分:3)

班级ChargingPileCommandCoder实施IProtocolPackage<byte[]> IProtocolPackage<byte>

答案 1 :(得分:1)

当我将其粘贴到VS中时,我首先会收到有关接口可访问性的警告。

当它们公开时,我收到的消息是ChargingPileProtocolPackage不能分配给IProtocolPackage,事实上这是正确的,它实现了IProtocolPackage。

如下更改DecodeCommand的签名时,警告消失了:

public void DecodeCommand(IProtocolPackage<byte[]> package)