在运行时创建动态类

发布于 2025-01-03 08:39:20 字数 212 浏览 6 评论 0原文

任何人都可以在运行时创建动态类。我有一个 dictionary 它包含数据表的所有列及其数据类型,我的计划是创建一个基于字典的动态类。表示数据表的列名是类的属性。创建 list 并绑定到网格后,

如果您有代码,将会非常有帮助

can any body have idea for create dynamic class at run time.i have one dictionary<string,object> which is contains datatable's all columns with it's datatype my plan is to create a dynamic class base on dictionary. means datatable's column name is property of class. after create list<dynamic class> and bind to grid

it's grate help if you have code for it

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

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

发布评论

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

评论(1

故事灯 2025-01-10 08:39:20

Silverlight 上的元编程相当有限,但是 TypeBuilder 可能就是您正在寻找的。不过,一个更简单的选择是使用 ExpandoObjectdynamic,但坦率地说:您还不如只使用字典。我不确定我是否会为此费心进行元编程。

如果您确实走这条路,您可以通过以下方式获得一个新的空列表:

IList list = (IList)Activator.CreateInstance(
       typeof(List<>).MakeGenericType(newType));

meta-programming on Silverlight is fairly limited, but TypeBuilder is probably what you are looking for. An easier option, though, is to use ExpandoObject and dynamic, but frankly: you might as well just use the dictionary. I'm not sure I'd bother going to the trouble of meta-programming for this.

If you do go that route, you can get a new empty list via:

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