2011-09-20 3 views
0

이 할당을 줄일 수있어서 단일 할당을 할 수 있습니까?텍스트가 클래스를 가리키는 클래스로 목록 채우기

x는 익명 목록입니다. 당신이 tiles.ToList() 를 사용하여 목록을 작성하거나 기존 사용 채울 수 거기에서

x => List {String name, int x, int y} 

하고 목록

_tiles.AddRange(from tile in x 
where tile.Type.ToLower() == "start".ToLower() 
select new Start(this) 
{ 
Position = new Vector2(tile.X, tile.Y), 
Texture = _texture2D 

}); 

_tiles.AddRange(from tile in x 
where tile.Type.ToLower() == "One".ToLower() 
select new OneTouch(this) 
{ 
Position = new Vector2(tile.X, tile.Y), 
Texture = _texture2D 

}); 

_tiles ...

_tiles.AddRange(from tile in x 
where tile.Type.ToLower() == "Two".ToLower() 
select new ReverseTouch(this) 
{ 
Position = new Vector2(tile.X, tile.Y), 
Texture = _texture2D 
+0

시작, 원터치 및 리버스 터치는 공통 조상으로부터 상속합니까? –

+0

그래, 모두 Tile 클래스에서 상속 받았다. –

답변

0
IEnumerable<Tile> tiles = x.Select(tile => 
{ 
    Tile currentTile = null; 

    switch (tile.Type.ToLower()) 
    { 
    case "start": 
     currentTile = new Start(this); 
     break; 

    case "One": 
     currentTile = new OneTouch(this); 
     break; 

    case "Two": 
     currentTile = new Start(this) 
     break; 

    ... 

    default: 
     // You know what to do... 
    } 

    currentTile.Position = new Vector2(tile.X, tile.Y), 
    currentTile.Texture = _texture2D 

    return currentTile; 
}); 

_tiles.AddRange(tiles);

+0

이렇게 간단 사전 을 사용할 수있는 방법이 없다. –

+0

네,하지만 다른 질문입니다 ... x.GroupBy (tile => tile.Type.ToLower())는 유형별로 엔티티를 그룹화합니다 –

관련 문제