C# 读取列表成员值时动态获取类成员 使用系统; 使用System.Collections.Generic; 公共课程 { 公立班学生 { 公共字符串名称{get;set;} 公共字符串年龄{get;set;} } 公共静态void Main() { List

C# 读取列表成员值时动态获取类成员 使用系统; 使用System.Collections.Generic; 公共课程 { 公立班学生 { 公共字符串名称{get;set;} 公共字符串年龄{get;set;} } 公共静态void Main() { List ,c#,dynamic-list,C#,Dynamic List,读取列表成员值时动态获取类成员 使用系统; 使用System.Collections.Generic; 公共课程 { 公立班学生 { 公共字符串名称{get;set;} 公共字符串年龄{get;set;} } 公共静态void Main() { List st=新列表(); 学生s=新学生(); s、 name=“Aarif”; s、 年龄=“25”; 圣加达(s); 对象dyn=“name”; 字符串名称=st[0]。名称; //我想在下面的一行中动态读取成员名称,请帮助我 字符串名称1=st

读取列表成员值时动态获取类成员
使用系统;
使用System.Collections.Generic;
公共课程
{
公立班学生
{
公共字符串名称{get;set;}
公共字符串年龄{get;set;}
}
公共静态void Main()
{
List st=新列表();
学生s=新学生();
s、 name=“Aarif”;
s、 年龄=“25”;
圣加达(s);
对象dyn=“name”;
字符串名称=st[0]。名称;
//我想在下面的一行中动态读取成员名称,请帮助我
字符串名称1=st[0].dyn;
Console.WriteLine(“Hello”+Name1);
}
}

Hi,欢迎来到stack overflow。有关如何提问和相应更新问题的更多详细信息,请参阅链接。作为旁白,我强烈建议遵循.NET命名约定,例如,对于类型和属性,
Student
Name
Age
,然后对于局部变量,
Name
name1
using System;
using System.Collections.Generic;

public class Program
{
    public class student
    {
        public string name {get;set;}
        public string age {get;set;}
    }

    public static void Main()
    {
        List<student> st = new List<student>();
        student s = new student();
        s.name = "Aarif";
        s.age = "25";
        st.Add(s);

        object dyn = "name";

        string Name = st[0].name;
            //I want to read name member dynamically here in below line so help me please
            string Name1 = st[0].dyn;
        Console.WriteLine("Hello "+ Name1);
    }
}