需要循环一个动态变量 [英] Need to loop through a dynamic variable
问题描述
>我有100个存储过程,返回不确定的数据(它可以返回1到n行的单行或多行)
我正在使用Entity框架,它返回一个类对象或列表<>对象。
我需要的是创建一个函数,在其中我可以传递任何StoredProcedure的输出
,无论它生成的数据结构如何HTML(我直接返回UI)我也需要访问字段名称。
所以如果我的sp返回
var result1 = DB.getServerDetailsBackupDefincation(servername);
var result2 = DB.getServerDetailsStorageDevice(servername);
我想在一个函数中发送结果,这将导致结果为动态或对象。
私有静态无效循环(对象结果)
{
}
private static void loop(动态结果){
}
现在我必须访问结果列表中的所有项目,以便我可以遍历它
并创建一个包含列名的标题
如果它们存在于结果中则为行。
到目前为止我是能够获得列名。
PropertyInfo [] propertyInfo = obj.GetType()。GetProperties();
Type type = propertyInfo [2] .GetMethod.ReturnType; /访问列表类型
PropertyInfo [] propertyInfo1 = type.GetProperties();
foreach(PropertyInfo中的PropertyInfo p)
{
Console.WriteLine(p.Name); //
}
我希望你明白我的需要
谢谢
我尝试了什么:
private static void loop(object obj)
{
string xPrev =;
PropertyInfo [] propertyInfo = obj.GetType()。GetProperties();
类型type = propertyInfo [2] .GetMethod.ReturnType;
PropertyInfo [] propertyInfo1 = type.GetProperties();
foreach(PropertyInfo p in propertyInfo1)
{
Console.WriteLine(p.Name);
string propertyName = string.Join(。,new string [] {xPrev,p.Name});
if(!propertyName.Contains( 父母))
{
类型propertyType = p.PropertyType;
if(!propertyType.ToS tring()。StartsWith(MyCms))
{
Console.WriteLine(string.Join(。,new string [] {xPrev,p.Name })。TrimStart(new char [] {'。'}));
}
else
{
xPrev = string.Join(。,new string [] {xPrev,p.Name});
}
}
}
}
}
公共类Emp >
{
public int id {get;组; }
公共字符串名称{get;组; }
}
公共班学生
{
public int studentID {get;组; }
public string studentName {get;组; }
}
static void Main(string [] args)
{
List< emp> emp1 = new List< emp>(){new Emp(){id = 1,name =AAA},new Emp(){id = 2,name =BBB}};
列表< student> student1 = new List< student>(){new Student(){studentID = 1,studentName =S1},new Student(){studentID = 2,studentName =S2}};
loop(emp1);
// loop(student1);
Console.ReadLine();
}
>I have 100 stored procedure which returns data which is uncertain (it could return 1 to n numbers of columns with single or multiple rows)
I am using Entity framework which returns me a class object or a list<> of the object.
What I needed is to create a single function in which I can pass the output of my any StoredProcedure
and regardless the structure of the data it generates an HTML (Which I return directly to UI) Also I need to access field name too.
So if my sp returns
var result1 = DB.getServerDetailsBackupDefincation(servername);
var result2 = DB.getServerDetailsStorageDevice(servername);
I want to send result in a fucntion which will accpet result either as dynamic or as object .
private static void loop(object result)
{
}
private static void loop(dynamic result){
}
And now I have to access all the item in the list in result so I can loop through it
and create a header with column name
And rows if they exist in the result.
So far I am able to get the column name.
PropertyInfo[] propertyInfo = obj.GetType().GetProperties();
Type type=propertyInfo[2].GetMethod.ReturnType; /to access list type
PropertyInfo[] propertyInfo1 = type.GetProperties();
foreach (PropertyInfo p in propertyInfo1)
{
Console.WriteLine(p.Name); //
}
I hope you understand what I need
Thanks
What I have tried:
private static void loop(object obj)
{
string xPrev = "";
PropertyInfo[] propertyInfo = obj.GetType().GetProperties();
Type type=propertyInfo[2].GetMethod.ReturnType;
PropertyInfo[] propertyInfo1 = type.GetProperties();
foreach (PropertyInfo p in propertyInfo1)
{
Console.WriteLine(p.Name);
string propertyName = string.Join(".", new string[] { xPrev, p.Name });
if (!propertyName.Contains("Parent"))
{
Type propertyType = p.PropertyType;
if (!propertyType.ToString().StartsWith("MyCms"))
{
Console.WriteLine(string.Join(".", new string[] { xPrev, p.Name }).TrimStart(new char[] { '.' }));
}
else
{
xPrev = string.Join(".", new string[] { xPrev, p.Name });
}
}
}
}
}
public class Emp
{
public int id { get; set; }
public string name { get; set; }
}
public class Student
{
public int studentID { get; set; }
public string studentName { get; set; }
}
static void Main(string[] args)
{
List<emp> emp1= new List<emp>() { new Emp() { id = 1, name = "AAA" }, new Emp() { id = 2, name = "BBB" } };
List<student> student1= new List<student>() { new Student() { studentID = 1, studentName = "S1" }, new Student() { studentID = 2, studentName = "S2" } };
loop(emp1);
//loop(student1);
Console.ReadLine();
}
推荐答案
找到解决方案
Found the Solution
public static void GenerateHtml(object obj)
{
string htmlData = "<table class='table table-bordered table - responsive table - hover'>";
StringBuilder sbHeader = new StringBuilder();
StringBuilder sbBody = new StringBuilder();
foreach (PropertyInfo pi in obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
if (typeof(IList).IsAssignableFrom(pi.PropertyType))
{
IList elms = (IList)pi.GetValue(obj, null);
if (elms != null)
{
sbHeader.Append(htmlData + "<thead><tr class='table-info'>");
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(elms[0]))
{
sbHeader.Append("<th>" + descriptor.Name+"</th>");
}
sbHeader.Append("</tr></thead>");
sbBody.Append("<tbody>");
for (int i = 0; i < elms.Count; i++)
{
sbBody.Append("<tr class='table-active'>");
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(elms[i]))
{
//string name=descriptor.Name;
//object value=descriptor.GetValue(elms[i]);
//Console.WriteLine("{0}={1}", name, value);
sbBody.Append("<td>" + Convert.ToString(descriptor.GetValue(elms[i])) + "</td>");
}
sbBody.Append("</tr>");
}
sbBody.Append("<tbody></table>");
}
}
else
{
Console.WriteLine(pi.Name + "=" + pi.GetValue(obj, null));
}
}
Console.WriteLine(sbHeader.ToString() + sbBody.ToString());
}
这篇关于需要循环一个动态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!