0

Basically I want to make a reflected version of a class in List So I can iterate through the list, then iterate through its properties as well.

I have gotten to where I can iterate through the properties of a single instance of a class, but I cant seem to figure out a solution for a List<> of reflected classes.

basically I want List[i].property[j] = GetData(c);

Below is the code I was messing with to get the List of reflected classes and their properties.

Type reportType = typeof(ReportController);
Type reportListType = typeof(List<ReportController>);

PropertyInfo[] properties = reportListType.GetProperties();

I also tried =typeof(List<>); but passing in the reportType, for whatever reason the formatting won't show it on SO.

Below is the references I have accessed.
C# instantiate generic List from reflected Type
https://learn.microsoft.com/en-us/dotnet/api/system.type.makegenerictype?view=netframework-4.8
Set object property using reflection

any and all help is appreciated.

Why is it so painful to get help on stackoverflow? I mean seriously, if it so easy for you that you took the time to comment, why don't you through up some code and get a free answer?

EDIT: Below will give me an array of classes, but I cannot get the properties enumerated, it just gives me the properties of the array type.


Type reportType = typeof(ReportController).MakeArrayType();

PropertyInfo[] properties = reportType.GetProperties();
Adam Schneider
  • 275
  • 1
  • 5
  • 18
  • Could you try to show more code and explain what you mean? You see to be contradicting yourself... – NetMage Sep 16 '19 at 21:35
  • How am I contradicting myself? – Adam Schneider Sep 16 '19 at 21:37
  • A `List<>` can't exist; "a `List<>` of reflected classes" would be a `List` not a e.g. `List` which is a list of multiple `ReportController` objects. Which do you want? "do the same thing on its properties" - what does "its" refer to? What does "do the same thing" mean? I see no way `=typeof(List<>)` would be work in your code and there is no passing to happen with it. – NetMage Sep 16 '19 at 21:41
  • 1
    `reportListType.GetProperties()` is going to give you the properties of `List`, so you'll get things like `Count`, which all Lists have. I don't think that's what you're going for. `reportType.GetProperties()` will give you the reflected properties found on `ReportController`. But it sounds like you've got a ways to go. I recommend using LINQPad to better visualize the results of your exploration with the reflection classes. – StriplingWarrior Sep 16 '19 at 21:42
  • 1. My code clearly show me trying to get a List and I clearly stated I wanted the outcome to be List[i].properties[j] = GetData. Meaning I want to simultaniously iterate through a list of reflected classes and their respective properties – Adam Schneider Sep 16 '19 at 21:44
  • StriplingWarrior. I noticed that when I tried to get the typeof(List) it literally only gave me the typeof(List) which I quess is to be expected. and trying to create a new list with List didn't exactly go over well – Adam Schneider Sep 16 '19 at 21:47
  • The problem I am trying to solve is I am parsing an enormous file and in parsing this file I perform CRUD operations on a database. The code would be cleaned up alot more with reflection and I just couldn't figure out how to make a list of reportController so I could then enumerate its properties and then I could iterate through class instances and their respective properties. – Adam Schneider Sep 16 '19 at 21:49
  • John Wu. When I try to create a reflected List the only properties I get are of List which makes sense because I created a generic List it just so happened to be of a type of Class. I can't seem to get any further than that. – Adam Schneider Sep 16 '19 at 21:55
  • StriplingWarrior. I dont see how LINQPad would help me. The issue I am having isn't with SQL. You also seem to know the answer. This isn't for school work if that is your worry, this is for my actual job with Cincinnati Insurance. – Adam Schneider Sep 16 '19 at 22:16
  • @AdamSchneider Don't reflect the list. Iterate over the list and obtain your object instance, then iterate through the properties of the (single) instance. You know how to do both of those things, so just put them together. – John Wu Sep 16 '19 at 22:26

1 Answers1

0

You already have the code you need for a single instance, so put that in a separate method:

public void Foo(ReportController input)
{
    Type reportType = typeof(ReportController);
    PropertyInfo[] properties = reportType.GetProperties();
    //Do something with the properties
}

Once that is done, you just need to iterate over your list and call it.

foreach (var item in list)
{
    Foo(item);
}
John Wu
  • 50,556
  • 8
  • 44
  • 80