0

I am on the receiving end of a class that contains several lists. A stripped down version is something like:

public class DS : FTSAction
    {
    public IList<FlsGroup> FLS_GROUP;
    public IList<FlsMcCity> FLS_MC_CITY;
    public IList<FlsMcCounty> FLS_MC_COUNTY;
    public IList<FlsCode> FLS_CODE;
    public DS();
    public int? Id { get; set; }
    public string Name { get; set; }
    }

I am trying to iterate through the "properties" of the class in order to pull out and do some processing on only the IList objects in the object.

Having stolen some code from elsewhere here in StackOverflow, I have this:

private DS UpdateDataSet = DSClient.GetResult(); //Fills out the object

foreach ( PropertyInfo pi in typeof( DataSynch ).GetProperties( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic ) )
    {
    var _item_type = Nullable.GetUnderlyingType( pi.PropertyType) ?? pi.PropertyType;
    if ( _item_type == typeof( IList<> ) )
        {
        var l = pi.GetValue( UpdateDataSet, null );
        }
    }

The trouble is that while I see "Id" and "Name" in the outer loop, which are skipped because they're not IList types, I never see any of the ILists at all. They're simply not part of what I get with the foreach loop. I'm not sure, but I suspect that they're objects in their own right and that's why they're not picked up by the loop, but I don't know.

So the question is - How do I get to the IList items?

Thanks

E Devitt
  • 67
  • 9
  • 4
    If I remember right GetProperties() will only return properties with a getter and/or setter otherwise it's considered as private. Try to add the Bindingflags BindingFlags.NonPublic | BindingFlags.Instance – Niklas Jun 04 '20 at 19:35
  • How does `Nullable` related to `IList<>`? – Pavel Anikhouski Jun 04 '20 at 19:36
  • As Niklas said, you only have two properties in your sample class. Is that also the case with the actual class? – 41686d6564 stands w. Palestine Jun 04 '20 at 19:40
  • Niklas : I saw that in another example and tried it. I have edited the code to reflect what you suggested, but it still only shows the two fields... – E Devitt Jun 04 '20 at 19:42
  • Ahmed: I wondered if the ILists were "properties" qua properties, but if they're not, what are they and how do I get at them? I tried searching using the more generic term "fields" but all that came back was how to find Properties. – E Devitt Jun 04 '20 at 19:43
  • 2
    my last idea would be to add the getter and setter. But if this wouldn't work also, i'm out – Niklas Jun 04 '20 at 19:44
  • Pavel: No idea. It's not even getting into the loop body except for "Id" and "Name", so I haven't had a problem with that (yet). – E Devitt Jun 04 '20 at 19:45
  • 6
    @EDevitt They are fields. To get them, you need to use `GetFields()` instead of `GetProperties()`. However, I don't think that's what you want. You probably want to convert them to properties by adding the getters/setters. All that being said, note that `_item_type` will merely tell you that the field/propery is an `IList` of something. In order to get the generic type of the IList, you need [an extra step](https://stackoverflow.com/q/1043755/8967612). – 41686d6564 stands w. Palestine Jun 04 '20 at 19:49

1 Answers1

0

Thank you all for your help. It turns out that the answer was to go back to the programmer making the classes in the larger class and have them add {get;set;} to each of the ILists within the class. That exposed them, allowing me to code

        foreach ( PropertyInfo pi in typeof(DataSynch).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) )
            {
            //Only do data lists (type IList). Ignore anything else in the structure/class.
            Type _item_type = Nullable.GetUnderlyingType(pi.PropertyType) ?? pi.PropertyType;
            if ( _item_type.UnderlyingSystemType.Name == typeof(IList<>).UnderlyingSystemType.Name )
                {
                dynamic l = pi.GetValue( UpdateDataSet, null ); //Get the list itself.
                TransferOneTable( pi.Name, l );
                }
            }

Thank you all!

E Devitt
  • 67
  • 9