【问题标题】:How to count class properties in C#?如何计算 C# 中的类属性?
【发布时间】:2019-04-02 06:14:34
【问题描述】:

这个DataSources类属性怎么算?答案应该是“3”

public class DataSources
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Phone { get; set; }
    }

【问题讨论】:

  • typeof(DataSources).GetProperties().Length?

标签: c# asp.net-mvc asp.net-core webforms


【解决方案1】:

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

using System.Linq;

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

或者

typeof(DataSources).GetProperties().Length;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-04
    • 1970-01-01
    • 2016-06-04
    • 1970-01-01
    • 2020-09-01
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多