C# 无法将dto列表转换为动态dto列表

C# 无法将dto列表转换为动态dto列表,c#,C#,我怎样才能让它工作?它给我一个错误,不能隐式转换。我的WebApp上有很多DTO,我需要一个占位符变量 注意:这不是我的WebApp上的实际代码,这与我想做的事情是一样的 public class DTO1 { public int Id { get; set; } public string Name { get; set; } } public class DTO2 { public int Id { get; set; } public string Na

我怎样才能让它工作?它给我一个错误,不能隐式转换
。我的WebApp上有很多DTO,我需要一个占位符变量

注意:这不是我的WebApp上的实际代码,这与我想做的事情是一样的

public class DTO1
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class DTO2
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class DTO1Service
{
    public static List<DTO1> GetListOfDTO1()
    {
        return new List<DTO1>
        {
            new DTO1 { Id = 1, Name = "DTO 1" },
            new DTO1 { Id = 2, Name = "DTO 2" }
        };
    }
}

public class DTO2Service
{
    public static List<DTO2> GetListOfDTO2()
    {
        return new List<DTO2>
        {
            new DTO2 { Id = 1, Name = "DTO 1" },
            new DTO2 { Id = 2, Name = "DTO 2" }
        };
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        var entities = new List<dynamic>();

        var serviceType = Console.ReadLine();

        if(serviceType == "1")
            entities = (dynamic)DTO1Service.GetListOfDTO1();
        else if (serviceType == "2")
            entities = (dynamic)DTO2Service.GetListOfDTO2();

        Console.ReadLine();
    }
}
公共类DTO1
{
公共int Id{get;set;}
公共字符串名称{get;set;}
}
公共级DTO2
{
公共int Id{get;set;}
公共字符串名称{get;set;}
}
公共类DTO1服务
{
公共静态列表getListofTo1()
{
返回新列表
{
新DTO1{Id=1,Name=“DTO 1”},
新DTO1{Id=2,Name=“DTO 2”}
};
}
}
公共类DTO2服务
{
公共静态列表GetListOfDTO2()
{
返回新列表
{
新DTO2{Id=1,Name=“DTO 1”},
新DTO2{Id=2,Name=“DTO 2”}
};
}
}
公共课程
{
公共静态void Main(字符串[]args)
{
var entities=新列表();
var serviceType=Console.ReadLine();
如果(服务类型=“1”)
entities=(动态)DTO1Service.GetListofTo1();
else if(服务类型==“2”)
entities=(动态)DTO2Service.getListofTo2();
Console.ReadLine();
}
}

您可以使用
Cast
方法将其显式强制转换为
动态
,如下所示:

if(serviceType == "1")
        entities = DTO1Service.GetListOfDTO1().Cast<dynamic>().ToList();
else if (serviceType == "2")
        entities = DTO2Service.GetListOfDTO2().Cast<dynamic>().ToList();
if(serviceType==“1”)
entities=DTO1Service.GetListofTo1().Cast().ToList();
else if(服务类型==“2”)
entities=DTO2Service.GetListofTo2().Cast().ToList();

你投错了。您正在尝试将
动态
分配给
列表
,而不是列表成员

您可以创建一个新实例并将值传入

if(serviceType == "1")
    entities = new List<dynamic>(DTO1Service.GetListOfDTO1());
else if (serviceType == "2")
    entities = new List<dynamic>(DTO2Service.GetListOfDTO2());

我个人更喜欢第二个选项,因为您已经初始化了变量,只需要填充它,而不是创建一个实例来重新分配它。

根据对问题的评论,我觉得OP需要这样的东西:

public interface IAmDTO {

    int Id { get; set; }

    string Name { get; set; }
}

public class DTO1 : IAmDTO {

    public int Id { get; set; }

    public string Name { get; set; }
}

public class DTO2 : IAmDTO {

    public int Id { get; set; }

    public string Name { get; set; }
}

public class DTO1Service {

    public static List<DTO1> GetListOfDTO1() =>
        new List<DTO1>
        {
        new DTO1 { Id = 1, Name = "DTO 1" },
        new DTO1 { Id = 2, Name = "DTO 2" },
        };
}

public class DTO2Service {

    public static List<DTO2> GetListOfDTO2() =>
        new List<DTO2>
        {
        new DTO2 { Id = 1, Name = "DTO 1" },
        new DTO2 { Id = 2, Name = "DTO 2" },
        };
}
}

public static class DTOExtensions {

public static void DtoHelper(this IEnumerable<IAmDTO> dtoS) {
    foreach(var dto in dtoS) {
        //DO SOMETHING WITH
        var id = dto.Id;
        var name = dto.Name;
    }

}
公共接口IAmDTO{
int Id{get;set;}
字符串名称{get;set;}
}
公共类DTO1:IAmDTO{
公共int Id{get;set;}
公共字符串名称{get;set;}
}
公共类DTO2:IAmDTO{
公共int Id{get;set;}
公共字符串名称{get;set;}
}
公共类DTO1服务{
公共静态列表getListofTo1()=>
新名单
{
新DTO1{Id=1,Name=“DTO 1”},
新DTO1{Id=2,Name=“DTO 2”},
};
}
公共类DTO2服务{
公共静态列表GetListOfDTO2()=>
新名单
{
新DTO2{Id=1,Name=“DTO 1”},
新DTO2{Id=2,Name=“DTO 2”},
};
}
}
公共静态类DTOExtensions{
公共静态无效数据帮助器(此IEnumerable DTO){
foreach(dto中的var dto){
//处理
var id=dto.id;
var name=dto.name;
}
}

}

您在哪一部分出现错误的示例。。?我对
DTO
部分没有把握。。但是您不能将
DT01
强制转换为
DT02
。。只能强制转换到其超类。。在部件
entities=(动态)DTO01Service.GetListOftO01()上啊,我明白了,因为您试图强制转换列表本身,而不是列表成员。我建议您远离dynamic,除非您绝对需要它,而且任何其他解决方案都非常痛苦,就像在MS Office库中一样。在其他情况下,您将丢失编译类型检查。如果你给我们一个明确的定义,你想做什么,我们可能会help@EmrahS嗯,我想为我所有的DTO创建一个助手类。我正在建设一个电子商务项目,我想做一个助手的方法来检查重复的名称和搜索引擎优化网址。嗨,你是正确的我投错了。我只是想知道@shree.pat18解决方案是否更适合选择正确的ASN。在提供的三个选项中,我的第二个建议最适用于提供的示例。
public interface IAmDTO {

    int Id { get; set; }

    string Name { get; set; }
}

public class DTO1 : IAmDTO {

    public int Id { get; set; }

    public string Name { get; set; }
}

public class DTO2 : IAmDTO {

    public int Id { get; set; }

    public string Name { get; set; }
}

public class DTO1Service {

    public static List<DTO1> GetListOfDTO1() =>
        new List<DTO1>
        {
        new DTO1 { Id = 1, Name = "DTO 1" },
        new DTO1 { Id = 2, Name = "DTO 2" },
        };
}

public class DTO2Service {

    public static List<DTO2> GetListOfDTO2() =>
        new List<DTO2>
        {
        new DTO2 { Id = 1, Name = "DTO 1" },
        new DTO2 { Id = 2, Name = "DTO 2" },
        };
}
}

public static class DTOExtensions {

public static void DtoHelper(this IEnumerable<IAmDTO> dtoS) {
    foreach(var dto in dtoS) {
        //DO SOMETHING WITH
        var id = dto.Id;
        var name = dto.Name;
    }

}