2017-01-16 8 views
1

これを動作させるにはどうすればよいですか?それは私にエラーを与えることはできませんimplicitly convert。 WebアプリケーションにDTOがたくさんあり、プレースホルダ変数が必要です。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(); 
    } 
} 
+0

あなたがエラーを取得しその一部上の任意の例を..?私は 'DTO'の部分についてはわかりませんが、' DT01'を 'DT02'にキャストすることはできません..そのスーパークラスにキャストできるのは.. –

+0

' entity =(dynamic)DTO01Service .GetListOfDTO01(); ' – markoverflow

+0

ああ、リストメンバーの代わりにリスト自体をキャストしようとしているからです。 –

答えて

1

あなたはそうのように、明示的dynamicにキャストするCastメソッドを使用することができます。

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

あなたは間違ってそれをキャストしています。あなたは、あなたが新しいインスタンスを作成し、

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

か、単に最初に作成されたインスタンス

if(serviceType == "1") 
    entities.AddRange(DTO1Service.GetListOfDTO1()); 
else if (serviceType == "2") 
    entities.AddRange(DTO2Service.GetListOfDTO2()); 
を移入に値を渡すことができますいずれか

リストメンバーの代わりにList<dynamic>dynamicを割り当てるしようとしています

私は個人的に変数を既に初期化しているので、2番目のオプションを好みます。後で再割り当てするだけでインスタンスを作成するのではなく、変数を移入するだけです。

+0

こんにちは、あなたは間違っているキャスト正しいです。私はちょうど@ shree.pat18ソリューションが正しいasnwersのために選択する方が良いかどうかを知りたがっています – markoverflow

+0

3つのオプションのうち、私の2番目の提案は、提供された例に最もよく当てはまります。 – Nkosi

0

質問にコメントをもとに、私は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; 
    } 

} 

}

関連する問題