1

I have the following class called HIP

using System;

namespace Shared
{
    public class HIP
    {
        public HIP ()
        {
        }
        public double data_source { get; set; }
        public string hid { get; set; }
        public double wid { get; set; }
        public double psn { get; set; }
    }
}

And I got the oData and adding each properties to the List as follows:

        var client= new ODataClient(settings);

        var packages =await client.For("HIP").FindEntriesAsync();

        protected List<HIP> hcp = new List<HIP>();

        foreach (var package in packages)
        {
            hcp.Add(new HIP {wid=Convert.ToSingle(package["wid"])});
            hcp.Add(new HIP {hid=package["hid"].ToString()});
            hcp.Add(new HIP {psn=Convert.ToSingle(package["psn"])});
            hcp.Add(new HIP {data_source=Convert.ToSingle(package["data_source"])});

        }

My question is how to get foreach operation in optimal/better way. Now, I have 4-5 properties and I could type each property names as follows package["wid"],package["hid"],package["psn"],package["data_source"]; however what if I have a tens of properties. I would like to know is there a better way to iterate.

casillas
  • 16,351
  • 19
  • 115
  • 215
  • 2
    you could use reflection... – Daniel A. White May 05 '15 at 21:48
  • Could you please elaborate (by giving an example) your idea? – casillas May 05 '15 at 21:48
  • @ilyasUyanik still not clear what you trying to do, you should add new item in list for every property from class HIP readed from package? – Victor May 05 '15 at 21:51
  • as @DanielA.White said, you can iterate through class meta data using reflection, for e.g. trhough it's public properties typeof(HIP).GetProperties() .. – Victor May 05 '15 at 21:52
  • possible duplicate of [C# calling all properties, for each loop](http://stackoverflow.com/questions/29514987/c-sharp-calling-all-properties-for-each-loop) – Robert McKee May 05 '15 at 21:53
  • possible duplicate of http://stackoverflow.com/questions/531384/how-to-loop-through-all-the-properties-of-a-class – Robert McKee May 05 '15 at 21:54
  • @ilyasUyanik looks like package indexer threw the error, are you sure that original code doesn't throw error as well as class HIP doesn't contains any other properties you published? – Victor May 05 '15 at 22:26
  • @ilyasUyanik do not get angry we are just want to know were the problem and also that we understands each other, so what about second part of my question about other properties in HIP class? – Victor May 05 '15 at 22:31
  • @ilyasUyanik I'm asking because I'm pretty convinced that error occurred in parsing, mention Convert.Single this method will work only for floating point numbers, so if you have any other types of properties other then double in "HIP" class you should adopt it some how to associate parse action with property type – Victor May 05 '15 at 22:36
  • I asking not about wid - the target property, but about the source values from package[smth], this is the root cause, let's do a trick, wrap your new HIP { wid = Convert.ToSingle(package[prop.Name]) } into {try{ new HIP { wid = Convert.ToSingle(package[prop.Name]) } } catch{ trhow new Exception(package[prop.Name])} } – Victor May 05 '15 at 22:49
  • that was you posted that behavior in your question, and that what I asked you in my first comments – Victor May 05 '15 at 22:53
  • I now see that you have corrected the source code – Victor May 05 '15 at 22:54

1 Answers1

1

You can use reflection to do something along the lines of:

var hcp = new List<HIP>();

var hipProperties = typeof(HIP).GetProperties();

hcp.AddRange(hipProperties.Select(prop => 
{
    var hip = new HIP();

    prop.SetValue(hip, Convert.ChangeType(package[prop.Name], prop.PropertyType), null);

    return hip;
}).ToList();

The above code will generate a list of HIP objects with only a single property set on each. I believe you might want to create a single HIP object with all its properties set for each package, like this:

var client = new ODataClient(settings);
var packages = await client.For("HIP").FindEntriesAsync();

var hcp = new List<HIP>();
var properties = typeof(Hip).GetProperties();

foreach (var p in packages)
{
    var hip = new HIP();

    foreach (var prop in properties)
    {
        prop.SetValue(hip, Convert.ChangeType(p[prop.Name], prop.PropertyType), null);
    }

    hcp.Add(hip);
}
RagtimeWilly
  • 5,265
  • 3
  • 25
  • 41
  • Do you have any clue of the following question http://stackoverflow.com/questions/30103964/system-type-does-not-contain-a-definition-for-getproperties – casillas May 07 '15 at 14:26