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

  • Home
  • SEARCH
  • 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 6934659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:59:21+00:00 2026-05-27T11:59:21+00:00

I’d like to recover type information using reflection. I have public Foo(object coll, string

  • 0

I’d like to recover type information using reflection. I have

public Foo(object coll, string tValue)
{
   var x = col1 as IList;
   if (x != null)
      x.Action();

   var y = col1 as IDictionary;
   if (y != null)
      y.Action();
}

But would like to have

public Foo(object coll, string tValue)
{
   var x = col1 as IList<TValue>;
   if (x != null)
      x.Action();

   var y = col1 as IDictionary<int, TValue>;
   if (y != null)
      y.Action();
}

Is it possible to arrive at and use generic interfaces instead of the old-school non-generic collection interfaces, given only the contained class name?

Once the local variable type is established, I’d like to avoid paying the reflection and dynamic invocation penalties when looping over the collection.

Another example, maybe clearer:

     var list = new Dictionary<int, MyObject>();
     list.Add(100, new MyObject());

     object listObject = list;

     var x = listObject as IDictionary<int, dynamic>;
     if (x != null)
     {
        foreach (var entry in x)
        {
           Console.WriteLine(entry.Key);
           Console.WriteLine(entry.Value);
        }
     }

x is null…

  • 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-05-27T11:59:22+00:00Added an answer on May 27, 2026 at 11:59 am

    I’m not sure if you intend to call a method on generic collection for every item in the collection or if you just want the values from the generic collection.

    For your third block of code, you could continue to use the non-generic interface and use an enumerator.

            var x = listObject as IDictionary;
            if (x != null)
            {
                var en = x.GetEnumerator();
                while(en.MoveNext())
                {
                    Console.WriteLine(en.Key);
                    Console.WriteLine(en.Value);
                }
            }
    

    If you intend to call a method without knowing the exact generic types for the generic IList or IDictionary, then you’ll have to use MethodInfo.Invoke. Cache the MethodInfo outside of the loop for a small performance boost.
    CreateDelegate would be faster, but you’ll need to know the exact generic types. You could get around that with expression trees, but the amount of code to maintain might not be worth the performance gain. Check out MagicMethod in Jon Skeets article
    Making reflection fly and exploring delegates

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
i got an object with contents of html markup in it, for example: string
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have thousands of HTML files to process using Groovy/Java and I need to
I have some data like this: 1 2 3 4 5 9 2 6
I want to count how many characters a certain string has in PHP, but

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.