遍历对象并获取属性 [英] loop through object and get properties
问题描述
我有返回操作系统属性的列表的方法。喜欢通过属性循环,做一些处理每个one.All id属性是字符串
我如何通过对象循环
C#
// TEST1和TEST2,所以你可以看到一个简单的例子属性 - 虽然这些都不是问题
字符串的一部分测试1 = OS_Result.OSResultStruct.OSBuild;
字符串测试2 = OS_Result.OSResultStruct.OSMajor;
//这里是我想什么,能够做
的foreach(字符串s在OS_Result.OSResultStruct)
{
//得到字符串做一些工作......
字符串测试= S;
// ......
}
您可以用反射的:
//获取字符串类型
VAR stringProps = OS_Result $性能的A名单b $ b .OSResultStruct
.GetType()
.GetProperties()
。凡(p =指p .PropertyType == typeof运算(字符串));
的foreach(在stringProps VAR道具){
//使用PropertyInfo对象中提取相应的值
//从OS_Result.OSResultStruct对象
串VAL =(字符串)道具.GetValue(OS_Result.OSResultStruct);
:
}
[通过的马修·沃森]
我已经采取了增加了进一步的代码示例,基于上面的代码的自由。
您可以通过书面形式将返回一个方法推广解决方案的的IEnumerable<串>
任何对象类型:
公共静态的IEnumerable< KeyValuePair<字符串,字符串>> StringProperties(obj对象)
{由对在obj.GetType
()返回的GetProperties()
,其中p.PropertyType == typeof运算(字符串)
选择新KeyValuePair<字符串,串>(p.Name,(串)p.GetValue(OBJ));
}
你还可以用泛型进一步概括它:
公共静态的IEnumerable< KeyValuePair<字符串,T>> PropertiesOfType< T>(obj对象)
{由对在obj.GetType
()返回的GetProperties()
,其中p.PropertyType == typeof运算(T)
选择新的。 KeyValuePair<字符串,T>(p.Name,(T)p.GetValue(OBJ));
}
使用这个第二种形式,超过遍历对象的所有字符串属性您可以这样做:
的foreach(VAR财产PropertiesOfType<串>(myObject的)){
变种名称=属性。键;
VAR VAL = property.Value;
:
}
i have a method that returns a list of operating system properties. Id like to loop through the properties and do some processing on each one.All properties are strings
How do i loop through the object
C#
// test1 and test2 so you can see a simple example of the properties - although these are not part of the question
String test1 = OS_Result.OSResultStruct.OSBuild;
String test2 = OS_Result.OSResultStruct.OSMajor;
// here is what i would like to be able to do
foreach (string s in OS_Result.OSResultStruct)
{
// get the string and do some work....
string test = s;
//......
}
You can do it with reflection:
// Obtain a list of properties of string type
var stringProps = OS_Result
.OSResultStruct
.GetType()
.GetProperties()
.Where(p => p.PropertyType == typeof(string));
foreach (var prop in stringProps) {
// Use the PropertyInfo object to extract the corresponding value
// from the OS_Result.OSResultStruct object
string val = (string)prop.GetValue(OS_Result.OSResultStruct);
...
}
[EDIT by Matthew Watson] I've taken the liberty of adding a further code sample, based on the code above.
You could generalise the solution by writing a method that will return an IEnumerable<string>
for any object type:
public static IEnumerable<KeyValuePair<string,string>> StringProperties(object obj)
{
return from p in obj.GetType().GetProperties()
where p.PropertyType == typeof(string)
select new KeyValuePair<string,string>(p.Name, (string)p.GetValue(obj));
}
And you can generalise it even further with generics:
public static IEnumerable<KeyValuePair<string,T>> PropertiesOfType<T>(object obj)
{
return from p in obj.GetType().GetProperties()
where p.PropertyType == typeof(T)
select new KeyValuePair<string,T>(p.Name, (T)p.GetValue(obj));
}
Using this second form, to iterate over all the string properties of an object you could do:
foreach (var property in PropertiesOfType<string>(myObject)) {
var name = property.Key;
var val = property.Value;
...
}
这篇关于遍历对象并获取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!