动态类创建在C# [英] Dynamic class creation in C#
本文介绍了动态类创建在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在运行时可以从DataTable创建一个类,其中ColumnName将是动态类属性?
解决方案
4,你可以这样做
dynamic foo = new ExpandoObject();
//在运行时捕获列名并将其添加为属性
((IDictionary< string,object>)foo).Add(Name,Apple);
Console.WriteLine(foo.Name); //将Apple写入屏幕
不推荐它或任何东西,但它显示它是可能的。 / p>
Is it possible in runtime to create a class from DataTable where ColumnName will be dynamic-class properties?
解决方案
With C# 4, you can do this
dynamic foo = new ExpandoObject();
// mimic grabbing a column name at runtime and adding it as a property
((IDictionary<string, object>)foo).Add("Name", "Apple");
Console.WriteLine(foo.Name); // writes Apple to screen
Not recommending it or anything, but it shows you it is possible.
这篇关于动态类创建在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文