2012-12-18 2 views
0

img.Group.Contents을 반복하고 galleryImage에 값을 쓰는 가장 깨끗한 방법은 무엇이라고 생각하십니까 ??? 사물?이 매핑을 어떻게 단순화 하시겠습니까? 객체에 배열

galleryImage.TinyImage = new myModel.Image._Img(); 

galleryImage.TinyImage.Url = img.Group.Contents[0].Url; 
galleryImage.TinyImage.FileSize = img.Group.Contents[0].FileSize; 
galleryImage.TinyImage.Type = img.Group.Contents[0].Type; 
galleryImage.TinyImage.Medium = img.Group.Contents[0].Medium; 
galleryImage.TinyImage.Width = img.Group.Contents[0].Width; 
galleryImage.TinyImage.Height = img.Group.Contents[0].Height; 
galleryImage.TinyImage.Hash = img.Group.Contents[0].Hash; 

galleryImage.Thumbnail.Url = img.Group.Contents[1].Url; 
galleryImage.Thumbnail.FileSize = img.Group.Contents[1].FileSize; 
galleryImage.Thumbnail.Type = img.Group.Contents[1].Type; 
galleryImage.Thumbnail.Medium = img.Group.Contents[1].Medium; 
galleryImage.Thumbnail.Width = img.Group.Contents[1].Width; 
galleryImage.Thumbnail.Height = img.Group.Contents[1].Height; 
galleryImage.Thumbnail.Hash = img.Group.Contents[1].Hash; 

galleryImage.SmallImage.Url = img.Group.Contents[2].Url; 
galleryImage.SmallImage.FileSize = img.Group.Contents[2].FileSize; 
galleryImage.SmallImage.Type = img.Group.Contents[2].Type; 
galleryImage.SmallImage.Medium = img.Group.Contents[2].Medium; 
galleryImage.SmallImage.Width = img.Group.Contents[2].Width; 
galleryImage.SmallImage.Height = img.Group.Contents[2].Height; 
galleryImage.SmallImage.Hash = img.Group.Contents[2].Hash; 

galleryImage.MediumImage.Url = img.Group.Contents[3].Url; 
galleryImage.MediumImage.FileSize = img.Group.Contents[3].FileSize; 
galleryImage.MediumImage.Type = img.Group.Contents[3].Type; 
galleryImage.MediumImage.Medium = img.Group.Contents[3].Medium; 
galleryImage.MediumImage.Width = img.Group.Contents[3].Width; 
galleryImage.MediumImage.Height = img.Group.Contents[3].Height; 
galleryImage.MediumImage.Hash = img.Group.Contents[3].Hash; 

galleryImage.LargeImage.Url = img.Group.Contents[4].Url; 
galleryImage.LargeImage.FileSize = img.Group.Contents[4].FileSize; 
galleryImage.LargeImage.Type = img.Group.Contents[4].Type; 
galleryImage.LargeImage.Medium = img.Group.Contents[4].Medium; 
galleryImage.LargeImage.Width = img.Group.Contents[4].Width; 
galleryImage.LargeImage.Height = img.Group.Contents[4].Height; 
galleryImage.LargeImage.Hash = img.Group.Contents[4].Hash; 

galleryImage.ExtraLargeImage.Url = img.Group.Contents[5].Url; 
galleryImage.ExtraLargeImage.FileSize = img.Group.Contents[5].FileSize; 
galleryImage.ExtraLargeImage.Type = img.Group.Contents[5].Type; 
galleryImage.ExtraLargeImage.Medium = img.Group.Contents[5].Medium; 
galleryImage.ExtraLargeImage.Width = img.Group.Contents[5].Width; 
galleryImage.ExtraLargeImage.Height = img.Group.Contents[5].Height; 
galleryImage.ExtraLargeImage.Hash = img.Group.Contents[5].Hash; 

답변

1

나는 galleryImage.??? 개체가 모두 동일한 유형이라고 가정합니까?

그렇다면, 그들을 위해 배열을 선언 :

다음
var list = new [] { 
    galleryImage.TinyImage, galleryImage.Thumbnail, galleryImage.SmallImage, 
    galleryImage.MediumImage, galleryImage.LargeImage, 
    galleryImage.ExtraLargeImage }; 

할 수 있습니다에 대한 루프 A를 그들을 통해 루프 :

for (int i=0; i<6; i++) { 
    list[i].Url = img.Group.Contents[i].Url; 
    list[i].FileSize = img.Group.Contents[i].FileSize; 
    list[i].Type = img.Group.Contents[i].Type; 
    list[i].Medium = img.Group.Contents[i].Medium; 
    list[i].Width = img.Group.Contents[i].Width; 
    list[i].Height = img.Group.Contents[i].Height; 
    list[i].Hash = img.Group.Contents[i].Hash; 
} 
+0

나는 최종 코드를 별도의 답변으로 게시했지만 본질적으로 사용자의 방향을 사용했습니다. 감사. –

5

기능은 반복 작업을 단순화 할 수있는 좋은 방법을 제공합니다

void ConfigureImage(MyImageType img, int pos) { 
    img.Url = img.Group.Contents[pos].Url; 
    img.FileSize = img.Group.Contents[pos].FileSize; 
    img.Type = img.Group.Contents[pos].Type; 
    img.Medium = img.Group.Contents[pos].Medium; 
    img.Width = img.Group.Contents[pos].Width; 
    img.Height = img.Group.Contents[pos].Height; 
    img.Hash = img.Group.Contents[pos].Hash; 
} 

이 기능을 사용하면 코드를 6 줄만 다시 쓸 수 있습니다 :

ConfigureImage(galleryImage.TinyImage, 0); 
ConfigureImage(galleryImage.Thumbnail, 1); 
ConfigureImage(galleryImage.SmallImage, 2); 
ConfigureImage(galleryImage.MediumImage, 3); 
ConfigureImage(galleryImage.LargeImage, 4); 
ConfigureImage(galleryImage.ExtraLargeImage, 5); 
1

Blorgbeard의 대답 @ 바탕으로

int ix = 0; 
foreach(var dst in new [] { galleryImage.TinyImage, galleryImage.Thumbnail, etc }) { 
    src = img.Group.Contents[ix]; 
    dst.Url = src.Url; 
    dst.FileSize = src.FileSize; 
    dst.Type = src.Type; 
    dst.Medium = src.Medium; 
    dst.Width = src.Width; 
    dst.Height = src.Height; 
    dst.Hash = src.Hash; 
    ix++; 
} 
0

같은 아마 뭔가가 여기 내 마지막 코드입니다. imgType 배열이 알려지지 않은 길이가 될 것이기 때문에 조금 수정해야했습니다. 내가 for 루프를 사용했다면, "Index Out of Bounds Exception"을 던질 것입니다.

var imgType = new[] { 
    galleryImage.TinyImage = new SmugMugGalleryModel.Image._Img(), 
    galleryImage.Thumbnail = new SmugMugGalleryModel.Image._Img(), 
    galleryImage.SmallImage = new SmugMugGalleryModel.Image._Img(), 
    galleryImage.MediumImage = new SmugMugGalleryModel.Image._Img(), 
    galleryImage.LargeImage = new SmugMugGalleryModel.Image._Img(), 
    galleryImage.ExtraLargeImage = new SmugMugGalleryModel.Image._Img(), 
    galleryImage.TwoExtraLargeImage = new SmugMugGalleryModel.Image._Img(), 
    galleryImage.ThreeExtraLargeImage = new SmugMugGalleryModel.Image._Img(), 
    galleryImage.OriginalImage = new SmugMugGalleryModel.Image._Img(), 
}; 

var count = 0; 
foreach (var i in img.Group.Contents) 
{ 
    imgType[count].Url = i.Url; 
    imgType[count].FileSize = i.FileSize; 
    imgType[count].Type = i.Type; 
    imgType[count].Medium = i.Medium; 
    imgType[count].Width = i.Width; 
    imgType[count].Height = i.Height; 
    imgType[count].Hash = i.Hash; 
    count++; 
} 
관련 문제