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 9215515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:13:16+00:00 2026-06-18T02:13:16+00:00

I was experimenting with the the Type.GetMethod method the other day when I ran

  • 0

I was experimenting with the the Type.GetMethod method the other day when I ran into a problem.

My program is a Windows application. There is one textbox in the form used for output and there are only these two blocks of code in the Form1_Load method:

   MethodInfo info2 = typeof(IEnumerable<int>).GetMethod("GetEnumerator");
   textBox1.Text += info2.ToString();

   MethodInfo info1 = typeof(IEnumerator<int>).GetMethod("MoveNext");
   textBox1.Text += info1.ToString();

The first GetMethod returned the correct information, but the second one returned nothing.

So I went one step further and included a try/catch statement in the code:

        try
        {
            MethodInfo info2 = typeof(IEnumerable<int>).GetMethod("GetEnumerator");
            textBox1.Text += info2.ToString();

            MethodInfo info1 = typeof(IEnumerator<int>).GetMethod("MoveNext");
            textBox1.Text += info1.ToString();
        }
        catch (Exception v)
        {
            textBox1.Text += v.Message;
        }

The second GetMethod now gives me the error:

Object reference not set to an instance of an object.

I am confused. Neither method is static and neither takes any parameter. Why does MoveNext return the error while GetEnumerator doesn’t?

  • 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-18T02:13:17+00:00Added an answer on June 18, 2026 at 2:13 am

    It is because the interface IEnumerator<T> does not have a definition for MoveNext, that is in the interface IEnumerator.

    Example:

       MethodInfo info1 = typeof(IEnumerator).GetMethod("MoveNext");
       textBox1.Text += info1.ToString();
    

    This will work fine,

    You can loop though the Interfaces assigned to IEnumerator<T> and find MoveNext

    MethodInfo info1 = typeof(IEnumerator<int>).GetInterfaces()
                      .Where(i => i.GetMethod("MoveNext") != null)
                      .Select(m => m.GetMethod("MoveNext"))
                      .FirstOrDefault();
    textBox1.Text += info1 != null ? info1.ToString() : string.Empty;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was experimenting with type families yesterday and ran into an obstacle with the
I was experimenting with shell functions the other day, with the objective of overriding
I am experimenting with RavenDB embedded in my application. I am receiving a Type
I just started experimenting with Threads and I ran in to a problem I'm
Was experimenting with some basic http post stuff with php and ran into this
I'm experimenting with ASP.Net Web API, which, by convention, splits controller methods into a
I'm experimenting with std::remove_reference. For example, I can extract an element type an array
I'm experimenting with a python program that will allow you to edit text files,
I've been experimenting with using type families to abstract UI toolkits. I've come unstuck
I'm experimenting with a Zebra TTP8200 thermal printer. For my application I need to

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.