2014-04-25 2 views
0

폴더의 속성과 함께 폴더에 포함 된 파일 목록의 배열을 만들려고합니다. 배열에는 파일의 속성이 열 머리글로 포함됩니다. 예를 들어;동일한 개체의 유형 및 인스턴트에 대한 속성 목록이 다릅니 까?

[Name, size, Path  ] 
[File A, 234, c:\temp ] 
[File B, 632, c:\temp\a] 
[File C, 3455, c:\temp\a] 

FileInfo 유형의 속성을 읽으면 속성 이름을 하드 코딩하지 않고 각 파일의 속성을 읽고 싶었습니다. 그래서 여기에 내가 한 일이 있습니다.

FileInfo[] files = IOUtil.GetFileList(); //<< returnes an array of FileInfo 

//Get a list of public properties for the FileInfo type 
Type t = typeof(FileInfo); 
foreach (var p in t.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) 
{ 
    Console.Write(p.Name); 
    // outputs names such as: Module, Assembly, RuntimeTypeHandle, MethodBase, Type, Type, String etc. 
} 

//loop through the files from FileInfo[] and read the attributes 
foreach (FileInfo f in files){ 
    foreach (var attr in f.GetType().GetProperties(BindingFlags.Public|BindingFlags.Instance)) 
    { 
    Console.Write(attr.PropertyType.Name + ", "); //print the attribute names again 
    // outputs names such as: Name, Length, DirectoryName, Directory, IsReadOnly, Exists, FullName, Extension etc. 
    } 
} 

나는 첫 번째 foreach 문에 나는 순간의 속성을지고있어 두 번째 유형의와의 속성을 얻고 이해,하지만 그들은 모두 같은 속성을 포함해야합니까? 누구든지 왜 각 목록의 속성이 다른지 설명 할 수 있습니까?

+0

개체의 속성이 해당 형식의 속성과 어떻게 같을 수 있습니까? 그것은 그들이 같은 존재가 될 것입니까? –

답변

0

작은 버그가 있거나 잘못 이해하고 있습니다. 여기 Type의 속성을 열거 : 물론 Type

FileInfo f = ... 
f.GetType().GetProperties(...) 

에서 객체 자체하지만 ... 유형의 설명되지 않습니다 :

Type t = typeof(FileInfo); 
t.GetType().GetProperties(...) 

여기 동안 FileInfo의 속성을 열거합니다. 그들은 동일하지 않습니다 - 자동차의 청사진은 자동차가 아닙니다.

+0

의미가 있습니다. t.GetType(). GetProperties (...)를 t.GetProperties (...)로 변경하면 객체 자체와 동일한 속성 열거가 반환됩니다. 감사 – Sivakanesh

관련 문제