获取类的属性

采用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属性来筛选出符合需要的属性。

访问修饰符和属性对照关系
属性访问修饰符
IsPublicpublic
IsPrivateprivate
IsFamilyprotected
IsAssemblyinternal
IsFamilyAndAssemblyC#不支持
IsFamilyOrAssemblyprotected internal

 

如果内容对您有帮助,可以扫码给我打赏,你的鼓励是我前进的动力。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值