当 T 可以是多个 new () 类型时,如何生成一个方法?

目前正在尝试对我拥有的方法进行基因改造。对泛型了解不多,如果看起来很愚蠢,请见谅......


我目前的方法:


public SimpleGenericModel GetSimpleModelFromMultiNode(FicheArticleViewModel model, string alias) 

        {

            var listeItems = CurrentPage.GetPropertyValue<IEnumerable<IPublishedContent>>(alias);

            var result = new SimpleGenericModel();


            if (!CurrentPage.HasValue(alias)) return result;


            foreach (var item in listeItems)

            {

                result.Id = item.Id.ToString();

                result.Nom = item.Name;

            }


            return result;

        }

问题是,现在 SimpleGenericModel 也可以是 RegionModel 和 SecteurDActiviteModel ......


我试过这个:


 public T GetSimpleModelFromMultiNode<T> (FicheArticleViewModel model, string alias) where T: new()

        {

            var listeItems = CurrentPage.GetPropertyValue<IEnumerable<IPublishedContent>>(alias);


            var result = new T();


            //if (typeof(T) == typeof(SimpleGenericModel))

            //{

            //    result = new SimpleGenericModel();

            //}

            //else if (typeof(T) == typeof(RegionViewModel))

            //{

            //    result = new RegionViewModel();


            //}

            //else if (typeof(T) == typeof(SecteurDActiviteViewModel))

            //{

            //    result = new SecteurDActiviteViewModel();

            //}


            if (!CurrentPage.HasValue(alias)) return result;


            foreach (var item in listeItems)

            {

                result.Id = item.Id.ToString();

                result.Nom = item.Name;

            }


            return result;

        }

但我有点迷失了......任何帮助将不胜感激...... :)


MM们
浏览 94回答 2
2回答

一只萌萌小番薯

假设RegionModel&SecteurDActiviteModel将共享Id&Nom属性,我建议您创建一个ISomeInterface并在您的类型上实现它,RegionModel : ISomeInterface. 没有必要像您正在做的那样返回实际类型result = new SimpleGenericModel();- 类型将在编译时由T您传递给方法知道。使用新接口,您可以像这样约束方法:public T GetSimpleModelFromMultiNode<T> (FicheArticleViewModel model, string alias) where T: new(), ISomeInterface,所以你的结构应该是这样的:public T GetSimpleModelFromMultiNode<T>(string model, string alias) where T : ISomeInterface, new(){&nbsp; &nbsp; var listeItems = CurrentPage.GetPropertyValue<IEnumerable<IPublishedContent>>(alias);&nbsp; &nbsp; var result = new T();&nbsp; &nbsp; if (!CurrentPage.HasValue(alias)) return result;&nbsp; &nbsp; foreach (var item in listeItems)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; result.Id = item.Id.ToString();&nbsp; &nbsp; &nbsp; &nbsp; result.Nom = item.Name;&nbsp; &nbsp; }&nbsp; &nbsp; return result;}public interface ISomeInterface{&nbsp; &nbsp; int Id { get; set; }&nbsp; &nbsp; string Nom { get; set; }}public class RegionModel : ISomeInterface{&nbsp; &nbsp; //Your implementation goes here}public class SecteurDActiviteModel : ISomeInterface{&nbsp; &nbsp; //Your implementation goes here}还有一件事:你一遍又一遍地将相同的属性映射到同一个对象,如果你想要的是T从listeItems. 您应该像这样更改代码:public IEnumerable<T> GetSimpleModelFromMultiNode<T>(string model, string alias) where T : ISomeInterface, new(){&nbsp; &nbsp; var listeItems = CurrentPage.GetPropertyValue<IEnumerable<IPublishedContent>>(alias);&nbsp; &nbsp;&nbsp; &nbsp; if (!CurrentPage.HasValue(alias)) return Enumerable.Empty<T>();&nbsp; &nbsp; return listeItems.Select(i => new T&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Id = i.Id.ToString(),&nbsp; &nbsp; &nbsp; &nbsp; Nom = i.Name,&nbsp; &nbsp; });}编辑:如果您的 listeItems 将具有单个值,则只需执行FirstOrDefault()orSingleOrDefault()即可获取项目和地图,如下所示:public T GetSimpleModelFromMultiNode<T>(string model, string alias) where T : ISomeInterface, new(){&nbsp; &nbsp; var listeItem = CurrentPage.GetPropertyValue<IEnumerable<IPublishedContent>>(alias).FirstOrDefault();&nbsp; &nbsp;&nbsp; &nbsp; if (!CurrentPage.HasValue(alias)) return new T();&nbsp; &nbsp; if (listeItem == null)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return new T();&nbsp; &nbsp; }&nbsp; &nbsp; return new T&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Id = listeItem.Id.ToString(),&nbsp; &nbsp; &nbsp; &nbsp; Nom = item.Name&nbsp; &nbsp; };}

HUWWW

如果你定义一个接口:interface INomAndId{&nbsp; &nbsp; int Id { get; set; }&nbsp; &nbsp; string Nom&nbsp; { get; set; }}并在您的模型中实现它:class SimpleGenericModel : INomAndId{&nbsp; &nbsp; public int Id { get; set; }&nbsp; &nbsp; public string Nom { get; set; }}class RegionViewModel : INomAndId{&nbsp; &nbsp; public int Id { get; set; }&nbsp; &nbsp; public string Nom { get; set; }}class SecteurDActiviteViewModel : INomAndId{&nbsp; &nbsp; public int Id { get; set; }&nbsp; &nbsp; public string Nom { get; set; }}class FicheArticleViewModel : INomAndId{&nbsp; &nbsp; public int Id { get; set; }&nbsp; &nbsp; public string Nom { get; set; }}您可以将接口用作约束:where T: INomAndId, new()这将允许您填充新对象:public T GetSimpleModelFromMultiNode<T> (FicheArticleViewModel model, string alias) where T: INomAndId, new(){&nbsp; &nbsp; var listeItems = CurrentPage.GetPropertyValue<IEnumerable<IPublishedContent>>(alias);&nbsp; &nbsp; var result = new T();&nbsp; &nbsp; if (!CurrentPage.HasValue(alias)) return result;&nbsp; &nbsp; foreach (var item in listeItems)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; result.Id = item.Id.ToString();&nbsp; &nbsp; &nbsp; &nbsp; result.Nom = item.Name;&nbsp; &nbsp; }&nbsp; &nbsp; return result;}
打开App,查看更多内容
随时随地看视频慕课网APP