无法从方法组转换为ushort

时间:2017-01-12 02:39:31

标签: c#

我想在工厂类中创建一个字典,用MessageId函数映射MessageCreate

private Dictionary<ushort, MessageWithBody> messagesList = new Dictionary<ushort, MessageWithBody>()
        {
            {A1Message.getMessageId,  A1Message.CreateMessage},
            {A2Message.getMessageId,  A2Message.CreateMessage}, 

        };

上述删除错误导致cannot convert from method group to ushort

A1MessageA2Message都来自MessageWithBody

1 个答案:

答案 0 :(得分:1)

更新至:

var messagesList = new Dictionary<ushort, MessageWithBody>()
{
    {(ushort)A1Message.getMessageId(),  A1Message.CreateMessage()},
    {(ushort)A2Message.getMessageId(),  A2Message.CreateMessage()},  
};

您需要确保调用方法。