MinhTV
MinhTV

Reputation: 9

How do I get the List values of a property of a class?

I'm trying to iterate through the properties of a class. It's working fine until i get a property which is defined as a list. Is there an easy way to handle this because I'm pretty sure, there is but unfortunately I'm not able to find the solution.

When I iterate through the properties the right values for the specific class will be shown, but only if it is a single value, I can't get any list values. if I use the property iteration, the property list will be shown as

System.Collections.Generic.List`1[System.Double].

A little background about my program. I dynamically create a List of my class with different values for the properties, so it's important that I get the right values for the right class.

...

for(int j = 0; j<intensityList.Count; j++) {

                getIntensityofMode(intensityList[j]);
                Report.Log(ReportLevel.Info, "");
                Console.ReadLine();
            }
        }
        public void getIntensityofMode(HelpingClasses.IntensityData mode) {

            Type type = typeof(HelpingClasses.IntensityData);
            PropertyInfo[] properties = type.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                if(property.GetValue(mode, null) != null) {
                    Report.Log(ReportLevel.Info, property.Name + " = " + property.GetValue(mode, null));

                    if(property.PropertyType.IsGenericType &&
                       property.PropertyType.GetGenericTypeDefinition() == typeof(List<>)) {
                        Report.Log(ReportLevel.Info,"its a list");

                        foreach() 
//iterate through the property which is a list and holds several values
                        {
                        }
                    }
                } else {
                    Report.Log(ReportLevel.Info, property.Name + " = NaN");
                }
            }

So I think, I just have to write one line in the foreach loop to get what I'm trying to get but I don't know how to handle it.

//variables
        public string TxVoltagePulseMode{ get; set; }
        public double TxVoltagePulseDB{ get; set; }
        public double TxVoltagePulseVoltage{ get; set; }
        public List<double> TxFocusdepth{ get; set; }

So everything works with the code except for TxFocusdepth.

Upvotes: 0

Views: 576

Answers (1)

CPerson
CPerson

Reputation: 1222

You can ignore the fact that it is a generic list entirely and just treat it as a non-generic IList.

...

for(int j = 0; j<intensityList.Count; j++) {

            getIntensityofMode(intensityList[j]);
            Report.Log(ReportLevel.Info, "");
            Console.ReadLine();
        }
    }

public void getIntensityofMode(HelpingClasses.IntensityData mode) {

    Type type = typeof(HelpingClasses.IntensityData);
    PropertyInfo[] properties = type.GetProperties();
    foreach (PropertyInfo property in properties)
    {
        if(property.GetValue(mode, null) != null) {
            var value = property.GetValue(mode, null);
            // Test if it is IList and cast it if so
            if (value is IList e) {
                // Print the value of elements in the enumerable list
                foreach (var v in e) {
                    Report.Log(ReportLevel.Info, property.Name + " = " + v.ToString());
                }
            } else {
               Report.Log(ReportLevel.Info, property.Name + " = " + value);
            }
        }
    }
}

Upvotes: 1

Related Questions