폴더의 속성과 함께 폴더에 포함 된 파일 목록의 배열을 만들려고합니다. 배열에는 파일의 속성이 열 머리글로 포함됩니다. 예를 들어;동일한 개체의 유형 및 인스턴트에 대한 속성 목록이 다릅니 까?
[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 문에 나는 순간의 속성을지고있어 두 번째 유형의와의 속성을 얻고 이해,하지만 그들은 모두 같은 속성을 포함해야합니까? 누구든지 왜 각 목록의 속성이 다른지 설명 할 수 있습니까?
개체의 속성이 해당 형식의 속성과 어떻게 같을 수 있습니까? 그것은 그들이 같은 존재가 될 것입니까? –