我有一个linq上下文,我正在查看所有数据表,我正在尝试获取所有表中的字段列表
foreach (var code in ctx.GetType().GetProperties())
{
Console.WriteLine(code.PropertyType + " - " + code.Name + " ");
if (code.PropertyType.ToString().Contains("System.Data.Linq.Table"))
{
//this does not give me what i want here
foreach (var pi in code.PropertyType.GetType().GetProperties())
{
Console.WriteLine(pi.Name);
}
}
}这并不能为我提供每个表中的列。
有什么想法吗?
简单地说,我正在尝试获取所有属性,而我所拥有的只是要获取其属性的对象的propertyInfo。
-Hurricane
发布于 2009-06-12 16:30:50
foreach (var code in ctx.GetType().GetProperties())
{
Console.WriteLine(code.PropertyType + " - " + code.Name + " ");
if (code.PropertyType.ToString().Contains("System.Data.Linq.Table"))
{
//this does not give me what i want here
foreach (var pi in code.PropertyType.GetGenericArguments()[0].GetProperties())
{
Console.WriteLine(pi.Name);
}
}
}把它改成那个。您在System.Data.Linq.Table上进行了反射,但请记住,DataContext上的属性是表,其中T是表示数据库中实际表的类。因此,您需要在T上进行反思。
https://stackoverflow.com/questions/987608
复制相似问题