vendredi 17 juillet 2015

Convert to html (nested ul li) from list in C#

I have a list like the following (sample):

Convert to html (nested ul li) from list in C#

But this list is just a sample for logic. The list should be dynamic. List field may have more than 3 and The list can have a collection of the child (data format like json)

I want to convert nested ul-li html tags. I think, I can do that with reflection like following. But I am using to the reflection first time. My code is like following for now. What should I do for that?

    public static string ConvertToHtml<T>(IEnumerable<T> list) where T : class
    {
        StringBuilder html = new StringBuilder();
        foreach (var item in list)
        {
            Type itemType = item.GetType();
            if (itemType.IsClass)
            {
                FieldInfo[] fieldInfo = itemType.GetFields(BindingFlags.Public | BindingFlags.Instance); // Field?
                if (fieldInfo.Any())
                {
                    foreach (var field in fieldInfo)
                    {
                        var name = field.Name;
                        var value = field.GetValue(item);
                    }
                }
                PropertyInfo[] propertyInfo = itemType.GetProperties(BindingFlags.Public | BindingFlags.Instance); // Property?
                if (propertyInfo.Any())
                {
                    foreach (var property in propertyInfo)
                    {
                        var name = property.Name;
                        var value = property.GetValue(item);
                    }
                }

            }
        }
        return string.Empty;
    }





Aucun commentaire:

Enregistrer un commentaire