Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get properties of a class in an array

Tags:

c#

I have a student class with the following structure:

    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;}
    }

How can I get those properties of Student class in an array like

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

And the types of these properties in separate array like

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

Please , explain it with chunk of code.

like image 623
Bhushan Firake Avatar asked Dec 04 '22 01:12

Bhushan Firake


1 Answers

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

That will give you an array of PropertyInfo. You can then do this to get just the names:

properties.Select(x => x.Name).ToArray();
like image 84
Levi Botelho Avatar answered Dec 20 '22 23:12

Levi Botelho