>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();
}