2021-07-28

Load class dynamically get property values one of which is a class

First of all, of course I have searched around for about two days but just can't get this working if it's possible.

This helped a little: Get property value from C# dynamic object by string (reflection?) As well as this: C# Iterate through Class properties

Background information: The class was generated using a JSON string from TMDB. API keys are free so if you want to see the raw json, get a key and use this: https://api.themoviedb.org/3/tv/456?api_key=YOURKEY&append_to_response=season/1,season/2,season/3&language=en-US I had 20 seasons on it but shortened it here. Only 20 at a time is allowed. And yes, I can get one season at a time and make 27 api calls but 2 is better. Getting one season works just fine, no help needed for that.

Here is part of the class that the json generated using VS2015 special paste with a few adjustments I made:

namespace TvDb.Tv
{
class MultiSeasons
{      
 
    public Season[] seasons { get; set; }    
    public Season1 season1 { get; set; }
    public Season2 season2 { get; set; }
    public Season3 season3 { get; set; }

    //and so on
}

public class Season1 : CommonProperties { }
public class Season2 : CommonProperties { }
public class Season3 : CommonProperties { }

//and so on

public class CommonProperties
{
    public string _id { get; set; }
    public string air_date { get; set; }
    public Episode[] episodes { get; set; }
    public string name { get; set; }
    public string overview { get; set; }
    public string poster_path { get; set; }
    public int season_number { get; set; }
}

public class Season
{
    public string air_date { get; set; }
    public int episode_count { get; set; }
    public int id { get; set; }
    public string name { get; set; }
    public string overview { get; set; }
    public string poster_path { get; set; }
    public int season_number { get; set; }
}

public class Episode
{
    public string air_date { get; set; }
    public int episode_number { get; set; }
    public int id { get; set; }
    public string name { get; set; }
    public string overview { get; set; }
    public string production_code { get; set; }
    public int season_number { get; set; }
    public string still_path { get; set; }
    public float vote_average { get; set; }
    public int vote_count { get; set; }
}

}

I can get the information I need from deserializing the json, but as can be seen, I know it can be better written getting the classes dynically. I just can't seem to find a way....

        string json = "It's stored in a file for testing";
        //the json from TMDB returns wrong to name or find classes
        //such as season/1, season/2, etc.
        json = json.Replace("season/", "season");

        MultiSeasons ser = JsonConvert.DeserializeObject<MultiSeasons>(json);

        DataTable dt = new DataTable();
        //columns added here


        //int numOfSeasons = ser.number_of_seasons;

        //Type type = Type.GetType("TvDb.Tv.Series, TvDb");
        string str = "TvDb.Tv.Season1" + ", " + "TvDb";

        //dynamic d = 
        Type type = Type.GetType(str);

        string st = "";
        if (type != null)
        {
            var cls = Utilities.Classes.PropertyReader.getProperties(type);
            //PropertyReader site https://answers.unity.com/questions/1092425/how-to-get-a-list-of-variables-in-a-class.html


            for (int i = 0; i < cls.Length; i++)
            {

                if(cls[i].name == "episodes")
                {
                    //Episode e = 
                }

                var nameOfProperty = cls[i].name;
                var propertyInfo = cls.GetType().GetProperty(nameOfProperty);
                var value = propertyInfo.GetValue(cls, null);


                //st += cls.GetType().GetProperty(cls[i].name).GetValue(cls, null);
                //st += cls[i].name.ToString() + Environment.NewLine;
               
            }
        }

        DataRow dr;           

        //write 50 seperate foreach statements? I don't think so !!!!!!!!

        if (ser.season1 != null)
        {
            foreach (var r in ser.season1.episodes)
            {
                dr = dt.NewRow();
                dr["id"] = ser.id;
                dr["firstAired"] = r.air_date;
                dr["season"] = r.season_number;
                //and so on                    
                dt.Rows.Add(dr);
            }
        }

        if (ser.season2 != null)
        {
            foreach (var r in ser.season2.episodes)
            {
                dr = dt.NewRow();
                dr["id"] = ser.id;
                dr["firstAired"] = r.air_date;
                dr["season"] = r.season_number;
                //and so on
                dt.Rows.Add(dr);
            }
        }

Any input is appreciated or point me to a post that I missed.

EDIT: For those who want to see the json, I found a place to view it...

https://codebeautify.org/online-json-editor/cbaebcbd



from Recent Questions - Stack Overflow https://ift.tt/3zF69Xv
https://ift.tt/eA8V8J

No comments:

Post a Comment