[英]What keywords are legal inside Type.GetType()?
我对文档感到困惑
public class Framework
{
public StreamReader Reader;
public CsvReader Csv;
public string Path { get; set; }
public DataTable Table;
public Framework()
{
//Path = "file.csv";
//Reader = new StreamReader(Path);
//Csv = new CsvReader(Reader, CultureInfo.InvariantCulture);
string[] ColumnNames = { "Name", "Phone", "Address" };
Table = CreateTable(ColumnNames);
}
//args are the name of the columns
public DataTable CreateTable(string[] args)
{
DataTable tbl = new DataTable();
DataColumn column = new DataColumn();
DataRow row;
/* create columns */
column.DataType = Type.GetType("Int32");
column.ColumnName = "ID";
tbl.Columns.Add(column);
for (int i = 0; i < args.Length; i++)
{
column.DataType = Type.GetType("String");
column.ColumnName = args[i];
tbl.Columns.Add(column);
}
row = tbl.NewRow();
row["ID"] = 1;
tbl.Rows.Add(row);
return tbl;
}
}
调试器告诉我GetType()
无法识别'String'
我读到我可以在这里做到这一点。
这个程序是一个 .net 框架项目,它最后所做的只是显示一个datagridview
,点击按钮 function 只有dataGridView1.DataSource = Framework.Table;
该程序甚至没有显示,我做错了什么? 我觉得这方面的信息为零...
你必须写出类型的全名,即
column.DataType = Type.GetType("System.String");
如果总是想获取字符串类型,也可以这样写
column.DataType = typeof(string);
仅当您在编译期间不知道需要哪种类型时,才使用您使用字符串获取类型名称的方法。
您可以使用Type t = typeof(string)
或Type.GetType("System.String")
或typof(int).Assembly.GetType("System.String")
给这样的column.DataType = typeof(Int32);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.