只反射类属性成员只含Get
public class MyUser
{
private int _Id=1;
public int Id
{
get { return _Id; }
}
private static int _Age;
public static int Age
{
get { return _Age; }
set { _Age = value; }
}
}
BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.GetProperty;
Type type = typeof(MyUser);
foreach (System.Reflection.PropertyInfo property in type.GetProperties(flags))
{
Console.WriteLine(property.Name);
}
请教大家一下,
我只想反射类属性成员含Get方法的,上面的方法Id和Age都被获取到了??
谢谢!
------解决方案--------------------BindingFlags flags = BindingFlags.Public
------解决方案-------------------- BindingFlags.Instance
------解决方案-------------------- BindingFlags.Static;
Type type = typeof(MyUser);
foreach (string propertyName in type.GetMethods(flags).Where(x => x.Name.StartsWith("get_")
------解决方案-------------------- x.Name.StartsWith("set_")).GroupBy(x => x.Name.Substring(4)).Where(x => x.Count() == 2).Select(x => x.Key))
{
Console.WriteLine(propertyName);
}
------解决方案--------------------PropertyInfo.CanWrite