采用SQL脚本的形式插入或查询数据时,有时候需要列出所有的字段名。而采用固定代码写死的方式无疑是不利于扩展的。这个时候可以考虑利用反射获取类的所有属性,然后将属性名称拼接起来的形式去处理。利用返回PropertyInfo数组的两个GetProperties方法,其中需要注意的是,有时候因为框架或其他原因,并不需要返回所有属性,这时候就可以利用Linq进行一些筛选处理。
不多说,直接上代码。工具类和Class类代码如下:
using System;
using System.Linq;
using System.Reflection;
using System.Text;
namespace WindowsFormsApp1
{
public class RefelctHelper
{
public static string AllPublicFields<T>()
{
PropertyInfo[] propertyInfos = typeof(T).GetProperties();
StringBuilder stringBuilder = new StringBuilder();
foreach (PropertyInfo propertyInfo in propertyInfos)
{
stringBuilder.Append(propertyInfo.Name + ",");
}
string fields = stringBuilder.ToString();
fields = fields.Substring(0, fields.LastIndexOf(','));
return fields;
}
public static string WithoutVirtualFields<T>()
{
PropertyInfo[] propertyInfos = typeof(T).GetProperties().Where(x => !x.GetMethod.IsVirtual).ToArray();
StringBuilder stringBuilder = new StringBuilder();
foreach (PropertyInfo propertyInfo in propertyInfos)
{
stringBuilder.Append(propertyInfo.Name + ",");
}
string fields = stringBuilder.ToString();
fields = fields.Substring(0, fields.LastIndexOf(','));
return fields;
}
}
public class HasVirtualProperties : NormalClass
{
public virtual Guid Id { get; set; }
}
public class HasNoSetProperties : NormalClass
{
public string Comment
{
get { return $"Name:{Name}Address:{Address}"; }
}
}
public class NormalClass
{
public string Name { get; set; }
public string Nation { get; set; }
public string Address { get; set; }
}
}
调用位置代码:
Console.WriteLine($"Class {nameof(NormalClass)} Properties:{ RefelctHelper.AllPublicFields<NormalClass>()}");
Console.WriteLine($"Class {nameof(NormalClass)} Properties:{RefelctHelper.AllPublicFields<HasNoSetProperties>()}");
Console.WriteLine($"Class {nameof(NormalClass)} Properties:{RefelctHelper.AllPublicFields<HasVirtualProperties>()}");
Console.WriteLine($"Class {nameof(NormalClass)} Properties(excluded virtual properties):{RefelctHelper.WithoutVirtualFields<NormalClass>()}");
Console.WriteLine($"Class {nameof(NormalClass)} Properties(excluded virtual properties):{RefelctHelper.WithoutVirtualFields<HasNoSetProperties>()}");
Console.WriteLine($"Class {nameof(NormalClass)} Properties(excluded virtual properties):{RefelctHelper.WithoutVirtualFields<HasVirtualProperties>()}");
输出结果如下:
将Name访问修饰符改为protected、Nation访问修饰符改为internal,输出如下:
不难看出GetProperties()方法会返回类的所有公开属性,包括父类继承而来的、没有Set属性的、虚属性等。如果想获得所有属性可以使用以下方法:
public static string AllFields<T>()
{
PropertyInfo[] propertyInfos = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
StringBuilder stringBuilder = new StringBuilder();
foreach (PropertyInfo propertyInfo in propertyInfos)
{
stringBuilder.Append(propertyInfo.Name + ",");
}
string fields = stringBuilder.ToString();
fields = fields.Substring(0, fields.LastIndexOf(','));
return fields;
}
可以利用PropertyInfo类的返回MethodInfo类型的GetMethod属性来筛选出符合需要的属性。
属性 | 访问修饰符 |
---|---|
IsPublic | public |
IsPrivate | private |
IsFamily | protected |
IsAssembly | internal |
IsFamilyAndAssembly | C#不支持 |
IsFamilyOrAssembly | protected internal |
如果内容对您有帮助,可以扫码给我打赏,你的鼓励是我前进的动力。