Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8750457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:52:00+00:00 2026-06-13T12:52:00+00:00

So this has been discussed on S.O. before however I don’t think I have

  • 0

So this has been discussed on S.O. before however I don’t think I have really ever seen an explanation of why this happens, nor can I seem to glean how this is useful to the average developer trying to use reflection in their code.

So check this bit of code out.

var fields = typeof(Person).GetFields(BindingFlags.DeclaredOnly | BindingFlags.NonPublic | BindingFlags.Instance);

If I have a List<int> in my Person object then I will get back a field who’s reflection “Name” is set to List`1, or if I had a dictionary it would be Dictionary`2. On the surface it appears this makes it hard to work with, I can’t say the following

foreach (var fieldInfo in fields.Where(fieldInfo => fieldInfo.FieldType == typeof(List<int>)))
{
     //Do something.
}

The reason this comes up is because I found the following in a project I am currently am working on.

    //Initialize collections
    FieldInfo[] properties = type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.NonPublic | BindingFlags.Instance);
    foreach (FieldInfo f in properties)
    {
       if (f.FieldType.Name == "IList`1" && f.GetValue(obj) == null)
       {
             object value = Container.Resolve(f.FieldType);
             f.SetValue(obj, value);

       }
   }

Then in the configuration for unity I see this happiness

<alias alias="IList`1" type="System.Collections.Generic.IList`1, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

I don’t really know that I know this is bad? Is it? It works of course, but even if this is OK (which causes my suspicious-code-dar to go off) my original question remains. What good does the dimension information about the generic do us as programmers?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:52:01+00:00Added an answer on June 13, 2026 at 12:52 pm

    When you’re dealing with types that are known at compile time, you shouldn’t ever need to deal with the Name of the Type, so names such as Thing`n shouldn’t be an issue.

    I’m not sure what’s happening in your first example, as when I do this:

    class Person
    {
        private List<int> intList;
    }
    

    and this:

    class Program
    {
        static void Main(string[] args)
        {
            var fields = typeof(Person).GetFields(BindingFlags.DeclaredOnly 
                                                | BindingFlags.NonPublic 
                                                | BindingFlags.Instance);
    
            foreach (var fieldInfo in 
                fields.Where(fieldInfo => fieldInfo.FieldType == typeof(List<int>)))
            {
                Console.WriteLine(fieldInfo.ToString());
    
            }
        }
    }
    

    I get what I would expect, namely

    System.Collections.Generic.List`1[System.Int32] intList
    

    In the code sample you give, again the types are known at compile time; better than what you have shown would be

    //Initialize collections
    FieldInfo[] properties = type.GetFields(BindingFlags.DeclaredOnly 
                                          | BindingFlags.NonPublic 
                                          | BindingFlags.Instance);
    foreach (FieldInfo f in properties)
    {
       if(typeof(IList<int>).IsAssignableFrom(f.FieldType)
       && f.GetValue(obj) == null)
       {
             object value = Container.Resolve(f.FieldType);
             f.SetValue(obj, value);
       }
    }
    

    You know what a IList<int> is; you can use IsAssignableFrom or == as appropriate.

    (If you get confused by IsAssignableFrom, as I always do, remember this: if (a is b) is the same test as if (typeof(b).IsAssignableFrom(a.getType())). The order swaps round.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this subject has been discussed before but still have problem with refresh
I know this question has been discussed before, and I know this is such
This is my first post. I know this topic has been discussed before in
I know this topic has been discussed but I think it has some differences.
I understand this has been discussed before but since this post in late 2010
I know this has been discussed ad nauseum...but I have an issue with the
(Not sure is this has been discussed before ...) When building SQL for category
I know this topic has been discussed before on Stack Overflow. But there are
Ever since .net 2.0 release this question has been discussed over a large number
I know this question has been discussed before, but i am interested in doing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.