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

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

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

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

最佳答案

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

using System.Linq;

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

或者

typeof(DataSources).GetProperties().Length;

关于c# - 如何计算 C# 中的类属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55468043/

相关文章:

c# - 使用 JasperReports Server API 从运行报告中检索 PDF

c# - 在接口(interface)中创建 IRepository<T> 列表

c# - 私有(private)类成员中关键字 'this' 的正确用法是什么?

c# - 从文件夹中删除文件

c# - 如何使用 C# ASP.NET Core 应用程序通过 Elastic Beanstalk 设置对特定文件夹的权限?

asp.net-core - 升级到 ASP.NET Core MVC 2.2 后默认路由操作不起作用

c# - ASP.NET C# MVC : How do I live without ViewState?

c# - 我可以假设我通过 POST 发送值的顺序将模型绑定(bind)到 Asp.net MVC 中相同顺序的数组吗?

c# - 导出到 Excel .xlsx 文件

c# - 升级到 netcoreapp2.1 后发布到 Azure 时出错