lundi 8 mai 2023

Blazor - Reflection issue

I'm trying to get the data values out of an object. I can get the properties from the Class just fine. But when I ask for the values, it fails. Any thoughts on why the GetValues line isn't working?

@inject HttpClient Http
@using System.Reflection

<p>@str</>

@code {
    public string? str;

    protected override async Task OnInitializedAsync()
    {
        stats = await Http.GetFromJsonAsync<Statistics[]>("sample-data/CoreDataCompany2023E.json");

        Type type = typeof(Statistics);

        var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);

        foreach (var prop in properties)
        {
            str += prop.GetValue(stats);
        }    
    }
}

@code{
    public class Statistics
    {
        public string Year { get; set; }  
        public int Volume { get; set; }
    };

Statistics[] stats = new Statistics[]
    {
        new Statistics
        {
            Year = "2023",
            Volume = 1225000
        }
    };
}




Aucun commentaire:

Enregistrer un commentaire