【问题标题】:How can I convert a list of Models to another list of Models that inherits from it?如何将模型列表转换为从它继承的另一个模型列表?
【发布时间】:2021-04-03 11:32:30
【问题描述】:

我有一段代码通过库检索一长串成员。

public List<InstinctGuildMember> PrepareGuildMembersList()
        {
            List<GuildMember> warcraftGuildMembers = _warcraftClient.GetGuildRosterAsync("dragonmaw", "instinct", "profile-eu")
                                            .Result.Value.Members.OrderBy(members => members.Rank)
                                            .ThenBy(members => members.Character.Name)
                                            .ToList();

            // We cast a new List from our old List.
            List<InstinctGuildMember> instinctGuildMembers = new List<InstinctGuildMember>(warcraftGuildMembers.Cast<InstinctGuildMember>());

            return instinctGuildMembers;
        }
namespace guild_instinct.Models.GuildData
{
    public class InstinctGuildMember : GuildMember
    {
        public string RankName { get; set; }
    }
}

我想转换为我自己的模型的原因是因为提供的模型没有我需要的所有信息。我的一个朋友建议制作我自己的模型并从库中继承模型。

我在related StackOverflow question 上找到了这个解决方案。但是,我无法让它工作,因为我一直收到错误后的错误

System.InvalidCastException: '无法将'ArgentPonyWarcraftClient.GuildMember'类型的对象转换为'guild_instinct.Models.GuildData.InstinctGuildMember'类型。'

到目前为止,我可能只是缺乏正确做这件事的知识,而答案就在眼前。

我不知道如何继续。我应该深入研究哪些知识来进一步进步?我的方向是否正确,但我是否缺乏使其发挥作用的一些基础知识?我希望我的问题足够简洁,值得帮助。

【问题讨论】:

    标签: c# list model-view-controller model


    【解决方案1】:

    您不能这样转换原始列表,因为GuildMember 实例不是InstinctGuidMember

    您可以使用投影,即将原始列表投影到不同类型的新列表上:

    var instinctGuildMembers = warcraftGuildMembers.Select(x => new InstinctGuildMember(x, rank)).ToList();
    

    要让您的模型包含来自GuildMember 其他数据的数据,有两种方法:

    组合 - 让您的模型拥有 GuildMember 实例。

    class InstinctGuildMember {
        private GuildMember _guildMember;
        private string _rankName;
    
        public InstinctGuildMember(GuildMember guildMember, string rankName)
        {
            _guildMember = guildMember;
            _rankName = rankName;
        }
    }
    

    继承 - 通过继承类型使您的模型成为 GuildMember

    class InstinctGuildMember : GuildMember {
        private string _rankName;
    
        //the constructor here depends on what constructors are available on GuildMember.
    }
    

    【讨论】:

    • 您好,我尝试了您的解决方案,它告诉我 InstinctGuildMember 没有采用 2 个参数的构造函数。看起来合乎逻辑,因为它只是一个模型;我应该为这个投影提供什么样的构造函数?
    • @shinyshark 我为构造函数添加了一个示例。
    【解决方案2】:

    您不能投射,但可以转换。

    List<InstinctGuildMember> instinctGuildMembers =
        warcraftGuildMembers.ConvertAll<InstinctGuildMember>(ConvertFunction);
    

    对于ConvertFunction

    static InstinctGuildMember ConvertFunction(GuildMember x)
    {
        InstinctGuildMember y = new InstinctGuildMember();
        y.prop1 = x.prop1;
        y.prop2 = x.prop2;
        y.RankName = "";
        return y;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多