如何计算C#中的类属性?

问题描述 投票:2回答:1

如何计算这个DataSources类属性?答案应该是'3'

public class DataSources
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Phone { get; set; }
    }
c# asp.net-mvc asp.net-core webforms
1个回答
3
投票

您可以使用System.Reflection命名空间中的类调查类型元数据。在您的情况下,TypeInfo-class可以帮助您获取有关属性的信息。

using System.Linq;

typeof(DataSources).GetProperties().Count();

要么

typeof(DataSources).GetProperties().Length;
© www.soinside.com 2019 - 2024. All rights reserved.