简体   繁体   English

是否可以创建“类”运行时?

[英]Is it possible to make a “class” runtime?

For the backend of a website, I need to fetch certain datacolumns from a database, depending on which sitesettings the user has. 对于网站的后端,我需要从数据库中获取某些数据列,具体取决于用户拥有的网站设置。 So there is no way of knowing which columns I will need beforehand. 因此,无法预先知道我将需要哪些列。

Is there any way I can make a list of "objects" containing only the different types I need to return to the frontend? 有什么办法可以使“对象”列表仅包含返回前端所需的不同类型?

For example: 例如:

The class students is stored in the database: 班级学生存储在数据库中:

Students
{
string name;
int age;
double averageGrade;
string class;
Student bestFriend;
}

Let's say the user on the site wants to see only the student's name and best friend, how can I put those in an object, and put those objects in a list? 假设网站上的用户只想看到学生的姓名和最好的朋友,我如何将它们放在一个对象中,然后将这些对象放在列表中?

exampleStudent {name, bestFriend};

Thanks in advance! 提前致谢!

One solution would be to store the properties using a dictionary: 一种解决方案是使用字典存储属性:

Dictionary<string, object> student = new Dictionary<string, object>();
student["name"] = "Alex";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM