我怎么通过C#的内部属性迭代 [英] how do I iterate through internal properties in c#

查看:133
本文介绍了我怎么通过C#的内部属性迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 公共类识别TestClass 
{
公共字符串property1 {搞定;组; }
公共字符串property2 {搞定;组; }

内部字符串property3 {搞定;组; }
内部字符串property4 {搞定;组; }
内部字符串property5 {搞定;组; }
}



我可以通过用下面的循环特性迭代,但它仅示出了公共属性。我需要的所有属性。

 的foreach(在的PropertyInfo财产typeof运算(识别TestClass).GetProperties())
$ { b $ b //做些什么
}


解决方案

你需要指定你不只是需要的公共属性,使用超载接受 的BindingFlags

 的foreach(在的PropertyInfo财产typeof运算(识别TestClass)
.GetProperties(BindingFlags.Instance |
BindingFlags.NonPublic可|
BindingFlags.Public))
{
//做些什么
}

添加 BindingFlags.Static 如果您希望包括静态属性。



该参数的超载只返回公共属性。


public class TestClass
{
        public string property1 { get; set; }
        public string property2 { get; set; }

        internal string property3 { get; set; }
        internal string property4 { get; set; }
        internal string property5 { get; set; }
}

I can iterate through the properties with the following loop, but it only shows public properties. I need all the properties.

foreach (PropertyInfo property in typeof(TestClass).GetProperties())
{
    //do something
}

解决方案

You need to specify that you don't just need the public properties, using the overload accepting BindingFlags:

foreach (PropertyInfo property in typeof(TestClass)
             .GetProperties(BindingFlags.Instance | 
                            BindingFlags.NonPublic |
                            BindingFlags.Public))
{
    //do something
}

Add BindingFlags.Static if you want to include static properties.

The parameterless overload only returns public properties.

这篇关于我怎么通过C#的内部属性迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆