Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, April 28, 2015 12:41 PM
Hi,
according to this scenario :
public class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public Category Category { get; set; }
public List<ProductItems> CategoryList { get; set; }
}
public class Category
{
public int CategoryId { get; set; }
public string CategoryName { get; set; }
}
public class ProductItems
{
public int ProductItemsId { get; set; }
public string ProductItemsName { get; set; }
}
public class PropertyResult
{
public string PropertyName { get; set; }
public string PropertyType { get; set; }
public string PropertyValue { get; set; }
}
I would like to get all properties and child properties and child of child properties and child of child of child .......................
So I would like to buid a list of all properties ( hierachically) and assign it to :
var result = new List<PropertyResult>
{
//TODO: put result here
};
Best regards.
All replies (3)
Tuesday, April 28, 2015 2:50 PM
why?
have you considered using reflection?
Study: https://msdn.microsoft.com/en-us/library/aky14axb(v=vs.110).aspx "Type.GetProperties Method"
and also: http://stackoverflow.com/questions/737151/how-to-get-the-list-of-properties-of-a-class
Sample:
void Main()
{
Product myProduct = new Product();
foreach(var prop in myProduct.GetType().GetProperties()) {
Console.WriteLine("{0}={1}", prop.Name, prop.GetValue(myProduct, null));
}
}
public class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public Category Category { get; set; }
public List<ProductItems> CategoryList { get; set; }
}
public class Category
{
public int CategoryId { get; set; }
public string CategoryName { get; set; }
}
public class ProductItems
{
public int ProductItemsId { get; set; }
public string ProductItemsName { get; set; }
}
public class PropertyResult
{
public string PropertyName { get; set; }
public string PropertyType { get; set; }
public string PropertyValue { get; set; }
}
output:
ProductId=0
ProductName=
Category=
CategoryList=
dienguis, once you have the properties of an instance of one object, you'll likely need to use recursion.
FWIW, it seems like a lot of work, so again, i ask "Why"?
see http://weblogs.asp.net/gerrylowry/clarity-is-important-both-in-question-and-in-answer
Thursday, June 11, 2015 3:51 AM
Hi, I would like to iterate trough my Product object and display values as follow :
if property type is value type (string, int double, ...), I dispay propertyName and Value.
if property type is Collection ( List, Ienumerable, ....), I recursively iterate trough list .
if property type is class, I go into class and display all child propertis of class that are string or int type.
public class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public Category Category { get; set; }
public List<ProductItems> CategoryList { get; set; }
}
public class Category
{
public int CategoryId { get; set; }
public string CategoryName { get; set; }
}
public class ProductItems
{
public int ProductItemsId { get; set; }
public string ProductItemsName { get; set; }
}
public class PropertyResult
{
public string PropertyName { get; set; }
public string PropertyType { get; set; }
public string PropertyValue { get; set; }
}
Applied to product class, my algorithm must display result as follow :
propertyName, PropertyValue
ProductId,1
ProductName, lemon
Category.CategoryId,4
Category.CategoryName, vegetables
ProductItems.ProductItemsId,4
ProductItems.ProductItemsName, xxxx
ProductItems.ProductItemsId,5
ProductItems.ProductItemsName, yyyy
Best regards
Thursday, June 11, 2015 8:55 PM
Add a method to each class called ToString(). In product it would look something like
public override string ToString()
{
return String.Format("ProductId,{0}\nProductName,{1},\n{2},\n{3}",
ProductId,
ProductName,
Category.ToString(),
CategoryList.Select(c=>c.ToString()).Aggregate((current,next)=> current + ",\n" + next)
);
}
Every other class will have something similar. You will need to tweek the commas and newlines to get the exact result that you want.
var productDetails = someProduct.ToString();