REST 호출로 인해 JSON 응답에서 json2charp 웹 유틸리티로 생성 된 C# 클래스 집합이 있습니다. 클래스를 사용하여 향후 JSON 응답을 해당 클래스로 deserialize합니다. 모든 것이 훌륭하게 작동합니다. 내부 클래스 중 하나에는 배열 인 속성이 있습니다. 배열 길이 속성을 사용하여 for 루프의에있는 속성을 사용하려고했지만 Length 속성을 현재 범위에서 사용할 수 없습니다. 내부 클래스이기 때문에 이것이라고 추측하고 있습니까?C#의 내부 클래스에서 배열 속성의 길이를 가져 오거나 작동하는 다른 반복 메서드를 가져 옵니까?
이 문제를 해결하려면 CountBreeds이라는 공용 속성을 추가하여 배열 길이 만 반환하면됩니다. 그건 잘 작동합니다. 하지만 내부 클래스의 배열 속성의 Length 속성을 배열의 길이 속성을 노출 할 필요없이 길이 얻으려면 방법이 있는지 궁금하네요? 그렇지 않다면 클래스에 IEnumerable 지원을 추가하지 않고 배열을 반복 할 수있는 방법이 있습니까?
나는 "internal"지정자를 제거 할 수 있지만 가능한 경우 보관하고 싶습니다. 아래의 코드 조각 :
// The internal class I want to iterate.
internal class Breeds
{
[JsonProperty("breed")]
public Breed[] Breed { get; set; }
[JsonProperty("@animal")]
public string Animal { get; set; }
// This property was added to facilitate for loops-that iterate the
// entire array, since the Length propery of the array property
// can not be accessed.
public int CountBreeds
{
get
{
return Breed.Length;
}
}
} // internal class Breeds
// Code that iterates the above class.
// >>>> This doesn't work since the Breeds Length property
// is unavailable in this context.
//
// Add the breeds to the list we return.
for (int i = 0; i < jsonPF.Petfinder.Breeds.Length; i++)
listRet.Add(jsonPF.Petfinder.Breeds.Breed[i].T);
// >>>> This *does* work because I added manually the CountBreeds
// property (not auto-generated by json2csharp).
// Add the breeds to the list we return.
for (int i = 0; i < jsonPF.Petfinder.Breeds.CountBreeds; i++)
listRet.Add(jsonPF.Petfinder.Breeds.Breed[i].T);
'Length'가 표시되지 않아야하는 이유는 없습니다. 배열에서도'IEnumerable'을 구현합니다. –
'Breeds'라고 불리는 단일 품종을 대표하고'Breed'라고 불리는 배열을 포함하고있는 클래스입니까? 나는 그것이 이상한 유일한 사람인가? 이런 종류의 이름을 디자인 할 때 어떤 논리를 사용 했습니까? – walther