c# - 如何获取数组中类的属性

标签 c#

我有一个具有以下结构的学生类(class):

    public sealed class Student
    {
       public string Name {get;set;}
       public string RollNo {get;set;}
       public string standard {get;set;}
       public bool IsScholarshipped {get;set;}
       public List<string> MobNumber {get;set;}
    }

我怎样才能得到这些属性 学生 像这样的数组中的类

     arr[0]=Name;
     arr[1]=RollNo; 
      .
      .
      .
     arr[4]=MobNumber

并且这些属性的类型在单独的数组中,例如

     arr2[0]=string;
     arr2[1]=string;
      .
      .
      .
     arr2[4]=List<string> or IEnumerable

请用代码块解释它。

最佳答案

var type = model.GetType();
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

这将为您提供一组 PropertyInfo。然后您可以执行此操作以仅获取名称:

properties.Select(x => x.Name).ToArray();

关于c# - 如何获取数组中类的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13513024/

相关文章:

c# - Server.Transfer 到 HttpHandler

c# - C++ 调用托管 COM 对象找不到相关程序集

c# - 我可以使用什么打印机通过 .Net 应用程序进行按需工业打印?

c# - 奇怪的 System.Format 异常

c# - .NET和负载均衡器?

c# - 如果 StreamWriter 变量在 if 语句内初始化并作为参数传递给 Console.SetOut(),它是否会过早地被 GC 处理?

c# - 如何将大中频条件转换为小中频

c# - 为什么我们在使用 New 操作时不添加 try catch

c# - MDI 窗体不能改变光标

c# - 转换到基类是否涉及装箱和拆箱