簡體   English   中英

如何計算C#中的類屬性?

[英]How to count class properties in C#?

如何計算此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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM