none
Using GetNestedType() how to advance in case of an array? RRS feed

  • Question

  • Hello,

    I have 2 issues.

    1. In my program GetNestedType() works fine as long as all the Nested classes are not an array. With arrays it fails to locate the exact array class. I want to get the value of Temperatures[5]th value. Just as a sanity check Line number 23 works and prints "Fantastic". 

    2. Second issue is that that "f" does not get the name.last as {get; set;}. This worked when the last name was a static declaration. I could not figure out how to make the {get; set;} work. 

    If line 38 prints val as "Fantastic" that will be really fantastic. Thanks in advance and greatly appreciated.

    Thanks

    using Newtonsoft.Json;
    using System;
    using System.Reflection;
    using System.Linq;
    
    class GetLotusSetCmd
    {
        public static void Main()
        {
            string[] dataInArray = new string[10];
            string[,] dataOutArray = new string[10, 10];
            dataInArray[0] = "/Lotus/v1/Chassis/Baseboard/Thermal/Temprature[5]/Status/HealthRollup";
            GetLotusData(dataInArray, dataOutArray);
    
    
        }
        static public void GetLotusData(string[] DataInArray, string[,] DataOutArray)
        {
            // Nested class Thermal
            string jsonThermal = "{\"@odata.context\":\"/Lotus/v1/$metadata#Thermal.Thermal\",\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal\",\"@odata.type\":\"#Thermal.v1_2_0.Thermal\",\"Id\":\"Thermal\",\"Name\":\"Thermal\",\"Temperatures\":[{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Temperatures/0\",\"MemberId\":\"0\",\"Name\":\"BB Lft Rear Temp\",\"SensorNumber\":20,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"ReadingCelsius\":31,\"UpperThresholdNonCritical\":110,\"UpperThresholdCritical\":115,\"LowerThresholdNonCritical\":5,\"LowerThresholdCritical\":0},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Temperatures/1\",\"MemberId\":\"1\",\"Name\":\"Riser 3 Temp\",\"SensorNumber\":23,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"ReadingCelsius\":31,\"UpperThresholdNonCritical\":75,\"UpperThresholdCritical\":80,\"LowerThresholdNonCritical\":5,\"LowerThresholdCritical\":0},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Temperatures/2\",\"MemberId\":\"2\",\"Name\":\"BB P1 VR Temp\",\"SensorNumber\":32,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"ReadingCelsius\":36,\"UpperThresholdNonCritical\":110,\"UpperThresholdCritical\":115,\"LowerThresholdNonCritical\":5,\"LowerThresholdCritical\":0},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Temperatures/3\",\"MemberId\":\"3\",\"Name\":\"Front Panel Temp\",\"SensorNumber\":33,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"ReadingCelsius\":19,\"UpperThresholdNonCritical\":50,\"UpperThresholdCritical\":55,\"UpperThresholdFatal\":60,\"LowerThresholdNonCritical\":5,\"LowerThresholdCritical\":0},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Temperatures/4\",\"MemberId\":\"4\",\"Name\":\"SSB Temp\",\"SensorNumber\":34,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"ReadingCelsius\":46,\"UpperThresholdNonCritical\":98,\"UpperThresholdCritical\":103,\"LowerThresholdNonCritical\":5,\"LowerThresholdCritical\":0},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Temperatures/5\",\"MemberId\":\"5\",\"Name\":\"BB P2 VR Temp\",\"SensorNumber\":35,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Fantastic\"},\"ReadingCelsius\":31,\"UpperThresholdNonCritical\":110,\"UpperThresholdCritical\":115,\"LowerThresholdNonCritical\":5,\"LowerThresholdCritical\":0},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Temperatures/6\",\"MemberId\":\"6\",\"Name\":\"BB BMC Temp\",\"SensorNumber\":36,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"ReadingCelsius\":34,\"UpperThresholdNonCritical\":110,\"UpperThresholdCritical\":115,\"LowerThresholdNonCritical\":5,\"LowerThresholdCritical\":0},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Temperatures/7\",\"MemberId\":\"7\",\"Name\":\"BB Rt Rear Temp\",\"SensorNumber\":37,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"ReadingCelsius\":34,\"UpperThresholdNonCritical\":110,\"UpperThresholdCritical\":115,\"LowerThresholdNonCritical\":5,\"LowerThresholdCritical\":0},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Temperatures/8\",\"MemberId\":\"8\",\"Name\":\"Riser 1 Temp\",\"SensorNumber\":39,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"ReadingCelsius\":37,\"UpperThresholdNonCritical\":75,\"UpperThresholdCritical\":80,\"LowerThresholdNonCritical\":5,\"LowerThresholdCritical\":0},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Temperatures/9\",\"MemberId\":\"9\",\"Name\":\"HSBP 1 Temp\",\"SensorNumber\":41,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"ReadingCelsius\":27,\"UpperThresholdNonCritical\":100,\"UpperThresholdCritical\":105,\"LowerThresholdNonCritical\":5,\"LowerThresholdCritical\":0},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Temperatures/10\",\"MemberId\":\"10\",\"Name\":\"Riser 2 Temp\",\"SensorNumber\":44,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"ReadingCelsius\":29,\"UpperThresholdNonCritical\":75,\"UpperThresholdCritical\":80,\"LowerThresholdNonCritical\":5,\"LowerThresholdCritical\":0},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Temperatures/11\",\"MemberId\":\"11\",\"Name\":\"Exit Air Temp\",\"SensorNumber\":46,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"ReadingCelsius\":46,\"UpperThresholdNonCritical\":80,\"UpperThresholdCritical\":85,\"LowerThresholdNonCritical\":5,\"LowerThresholdCritical\":0},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Temperatures/12\",\"MemberId\":\"12\",\"Name\":\"LAN NIC Temp\",\"SensorNumber\":47,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"ReadingCelsius\":43,\"UpperThresholdNonCritical\":115,\"UpperThresholdCritical\":120,\"LowerThresholdNonCritical\":5,\"LowerThresholdCritical\":0},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Temperatures/13\",\"MemberId\":\"13\",\"Name\":\"PS1 Temperature\",\"SensorNumber\":92,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"ReadingCelsius\":21,\"UpperThresholdNonCritical\":62,\"UpperThresholdCritical\":65},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Temperatures/14\",\"MemberId\":\"14\",\"Name\":\"PS2 Temperature\",\"SensorNumber\":93,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"ReadingCelsius\":21,\"UpperThresholdNonCritical\":62,\"UpperThresholdCritical\":65},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Temperatures/15\",\"MemberId\":\"15\",\"Name\":\"P1 DTS Therm Mgn\",\"SensorNumber\":131,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"ReadingCelsius\":-29,\"UpperThresholdNonCritical\":10,\"UpperThresholdCritical\":15},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Temperatures/16\",\"MemberId\":\"16\",\"Name\":\"P2 DTS Therm Mgn\",\"SensorNumber\":132,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"ReadingCelsius\":-35,\"UpperThresholdNonCritical\":10,\"UpperThresholdCritical\":15},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Temperatures/17\",\"MemberId\":\"17\",\"Name\":\"NVME 1 Therm Mgn\",\"SensorNumber\":145,\"Status\":{\"State\":\"Disabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"ReadingCelsius\":0,\"UpperThresholdCritical\":0,\"MinReadingRangeTemp\":-126},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Temperatures/18\",\"MemberId\":\"18\",\"Name\":\"DIMM Thrm Mrgn 1\",\"SensorNumber\":176,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"ReadingCelsius\":-51,\"UpperThresholdNonCritical\":5,\"UpperThresholdCritical\":10},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Temperatures/19\",\"MemberId\":\"19\",\"Name\":\"DIMM Thrm Mrgn 2\",\"SensorNumber\":177,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"ReadingCelsius\":-55,\"UpperThresholdNonCritical\":5,\"UpperThresholdCritical\":10},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Temperatures/20\",\"MemberId\":\"20\",\"Name\":\"DIMM Thrm Mrgn 3\",\"SensorNumber\":178,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"ReadingCelsius\":-58,\"UpperThresholdNonCritical\":5,\"UpperThresholdCritical\":10},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Temperatures/21\",\"MemberId\":\"21\",\"Name\":\"DIMM Thrm Mrgn 4\",\"SensorNumber\":179,\"Status\":{\"State\":\"Disabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"ReadingCelsius\":0,\"UpperThresholdNonCritical\":5,\"UpperThresholdCritical\":10},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Temperatures/22\",\"MemberId\":\"22\",\"Name\":\"Agg Therm Mgn 1\",\"SensorNumber\":200,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"ReadingCelsius\":-18},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Temperatures/23\",\"MemberId\":\"23\",\"Name\":\"Agg Therm Mgn 2\",\"SensorNumber\":201,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"ReadingCelsius\":-38}],\"Fans\":[{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Fans/0\",\"MemberId\":\"0\",\"Name\":\"System Fan 1\",\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"Reading\":3445,\"ReadingUnits\":\"RPM\",\"LowerThresholdNonCritical\":1961,\"LowerThresholdCritical\":1696,\"MinReadingRange\":0,\"MaxReadingRange\":13515,\"Redundancy\":[{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Redundancy/0\"}]},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Fans/1\",\"MemberId\":\"1\",\"Name\":\"System Fan 2\",\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"Reading\":3445,\"ReadingUnits\":\"RPM\",\"LowerThresholdNonCritical\":1961,\"LowerThresholdCritical\":1696,\"MinReadingRange\":0,\"MaxReadingRange\":13515,\"Redundancy\":[{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Redundancy/0\"}]},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Fans/2\",\"MemberId\":\"2\",\"Name\":\"System Fan 3\",\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"Reading\":3445,\"ReadingUnits\":\"RPM\",\"LowerThresholdNonCritical\":1961,\"LowerThresholdCritical\":1696,\"MinReadingRange\":0,\"MaxReadingRange\":13515,\"Redundancy\":[{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Redundancy/0\"}]},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Fans/3\",\"MemberId\":\"3\",\"Name\":\"System Fan 4\",\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"Reading\":3392,\"ReadingUnits\":\"RPM\",\"LowerThresholdNonCritical\":1961,\"LowerThresholdCritical\":1696,\"MinReadingRange\":0,\"MaxReadingRange\":13515,\"Redundancy\":[{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Redundancy/0\"}]},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Fans/4\",\"MemberId\":\"4\",\"Name\":\"System Fan 5\",\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"Reading\":3445,\"ReadingUnits\":\"RPM\",\"LowerThresholdNonCritical\":1961,\"LowerThresholdCritical\":1696,\"MinReadingRange\":0,\"MaxReadingRange\":13515,\"Redundancy\":[{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Redundancy/0\"}]},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Fans/5\",\"MemberId\":\"5\",\"Name\":\"System Fan 6\",\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"Reading\":3392,\"ReadingUnits\":\"RPM\",\"LowerThresholdNonCritical\":1961,\"LowerThresholdCritical\":1696,\"MinReadingRange\":0,\"MaxReadingRange\":13515,\"Redundancy\":[{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Redundancy/0\"}]}],\"Redundancy\":[{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Redundancy/0\",\"MemberId\":\"0\",\"Name\":\"BaseBoard System Fans\",\"RedundancySet\":[{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Fans/0\"},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Fans/1\"},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Fans/2\"},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Fans/3\"},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Fans/4\"},{\"@odata.id\":\"/Lotus/v1/Chassis/Baseboard/Thermal#/Fans/5\"}],\"Mode\":\"N+m\",\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"Superb\"},\"MinNumNeeded\":5,\"MaxNumSupported\":6}]}";
            var Thermal = JsonConvert.DeserializeObject<Thermal>(jsonThermal);
    
            Console.WriteLine("HealthRollup for 5th Temperature Sensor = {0}", Thermal.Temperatures[5].Status.HealthRollup);
            Str2Cmd("/Thermal/Temprature[5]/Status/HealthRollup");
            Console.ReadLine();
        }
        static void Str2Cmd(string CommandLine)
        {
            string[] names = CommandLine.Trim().Trim('/').Split('/');
            Type t = typeof(GetLotusSetCmd);
            foreach (var n in names.Take(names.Length - 1))
            {
                t = t.GetNestedType(n/*, BindingFlags.*/);
            }
            var f = t.GetField(names.Last());
            object val = f.GetValue(null);
            Console.WriteLine(CommandLine);
            Console.WriteLine(val);
            Console.ReadLine();
        }
    
        public class Thermal
        {
            public string Id { get; set; }
            public class Temperature
            {
                public string MemberId { get; set; }
                public class CStatus
                {
                    public string HealthRollup { get; set; }
                }
                public CStatus Status { get; set; }
            }
            public System.Collections.Generic.List<Temperature> Temperatures { get; set; }
        } // Thermal
    }
    


    Saturday, January 20, 2018 1:51 AM

Answers

  • Check this function:

    static void Str2Cmd( Thermal obj, string CommandLine )
    {
    	string[] names = CommandLine.Trim().Trim( '/' ).Split( '/' );
    	object o = obj;
    
    	foreach( var n in names )
    	{
    		var m = Regex.Match( n, @"(.*)\[(\d+)\]$" );
    		if( m.Success )
    		{
    			var p = o.GetType().GetProperty( m.Groups[1].Value );
    			dynamic a = p.GetValue( o );
    			o = a[int.Parse( m.Groups[2].Value )];
    		}
    		else
    		{
    			var p = o.GetType().GetProperty( n );
    			o = p.GetValue( o );
    		}
    	}
    	Console.WriteLine( CommandLine );
    	Console.WriteLine( o );
    	Console.ReadLine();
    }
    

    Sample usage:

    var Thermal = . . .
    
    Str2Cmd( Thermal, "/Id" );
    Str2Cmd( Thermal, "/Temperatures[5]/Status/HealthRollup" );

    The prefix "/Thermal" is not required

    Saturday, January 20, 2018 7:23 PM

All replies

  • Check this function:

    static void Str2Cmd( Thermal obj, string CommandLine )
    {
    	string[] names = CommandLine.Trim().Trim( '/' ).Split( '/' );
    	object o = obj;
    
    	foreach( var n in names )
    	{
    		var m = Regex.Match( n, @"(.*)\[(\d+)\]$" );
    		if( m.Success )
    		{
    			var p = o.GetType().GetProperty( m.Groups[1].Value );
    			dynamic a = p.GetValue( o );
    			o = a[int.Parse( m.Groups[2].Value )];
    		}
    		else
    		{
    			var p = o.GetType().GetProperty( n );
    			o = p.GetValue( o );
    		}
    	}
    	Console.WriteLine( CommandLine );
    	Console.WriteLine( o );
    	Console.ReadLine();
    }
    

    Sample usage:

    var Thermal = . . .
    
    Str2Cmd( Thermal, "/Id" );
    Str2Cmd( Thermal, "/Temperatures[5]/Status/HealthRollup" );

    The prefix "/Thermal" is not required

    Saturday, January 20, 2018 7:23 PM
  • Hi Viorel,

    It works Hurrah!! It was my mistake, it was a typo between Temperature and Temperatures. I had got it mixed up. Sorry I thought there was an issue. My apologies if I wasted your time. I spent lot of time on your solution and I fully understand what you did, the typo was a blessing in disguise. C# is amazing... 

    best regards

    ananda

    Sunday, January 21, 2018 6:22 AM