如何在运行时向现有类添加新属性,独立于 C# 中具有读写功能的现有成员

发布于 2024-10-07 08:38:08 字数 49 浏览 0 评论 0 原文

我需要为数据库表中的每一列创建一个属性,但我们事先不知道列数。 主要是为了可重用性。

I need to make a property for each column in a database table but we do not know the number of columns before hand.
It is mainly for reuseability.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心房的律动 2024-10-14 08:38:08

使用字典哈希表 而是

Use a Dictionary or Hashtable rather

失眠症患者 2024-10-14 08:38:08

我不明白这是如何使用(或可能)的,因为属性在设计时更有用,因此您可以轻松找到并使用它们。
如果您能够在运行时创建它们,您如何期望编码器知道属性将存在?编译器甚至看不到属性的名称。
即使此时使用反射,您仍然需要以文本方式指定属性的名称。

因此,使用 DataTable 表示相应的数据库表是给您带来运行时动态感觉的一种方法。
另一种方法是对每行使用带有 Dictionary 的键值对方法,并将它们全部存储在 HashSetList 中,具体取决于在解决方案的其余部分上,代表一个表。

I don't see how that's usable (or possible), because properties are more useful at design-time, so you can easily find and use them.
If you were able to create them at runtime, how do you expect the coder to know a property will exist? The compiler won't even see the name of the property.
Even if you use reflection at that point, you still need to specify the name of the property textually.

Hence, using a DataTable to represent the corresponding database table is one way to give you that feeling of run-time dynamics.
Another way is to use the key-value pair approach with Dictionary for each row, and store them all in a HashSet or a List, depending on the rest of your solution, to represent a table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文