1.概要
返回为当前 System.Type 的所有公共属性。
2.代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace 属性实验
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("属性实验");
A a = new A();
Type type = a.GetType();
PropertyInfo[] propertyInfos = type.GetProperties();
foreach (PropertyInfo propertyInfo in propertyInfos) {
Console.WriteLine(propertyInfo.Name);
}
Console.ReadKey();
}
}
class A {
public int Ma{set;get;}
public int Mb { set; get; }
public int mc;
private int Md { set; get; }
protected int Me { set; get; }
}
}
3.结果
属性实验
Ma
Mb
4.分析

本文介绍了一个简单的C#程序,使用反射来获取并打印指定类型的公共属性名称。通过实例化一个A类的对象,并调用GetType()方法结合GetProperties()方法,展示了如何遍历并输出这些属性。
958

被折叠的 条评论
为什么被折叠?



