jeudi 11 mai 2017

Using GetValue method in instance method of a class in C#

I have simple class with method ToHtmlString() and I want to enumerate all properties of this object(reference). Why I can't take the value. I see I need to pass object to GetValue method in order runtime to know which value of which reference reflection will get, but it seems like I CAN'T do it like that? How can I use GetValue while enumerating all properties of this instance?

    public string ToHtmlString()
    {
        var source = this;
        var html = new StringBuilder();

        foreach (var property in this.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance))
        {
            html.AppendLine(
                new GenericItemView(
                    (property.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute)?.Description,
                    property.GetValue(source).ToString());
        }

        return html.ToString();
    }





Aucun commentaire:

Enregistrer un commentaire