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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:41:04+00:00 2026-06-07T23:41:04+00:00

The following code executes without error and prints In some , which means that

  • 0

The following code executes without error and prints “In some“, which means that the statement

m[0].Invoke(o, args);

invokes the function some which is a member of the foo class, on object o and affects its public variable i. But when we uncomment the last line of the code and try to compile it, it produces an error. Why??

using System;
using System.Reflection;

class foo
{
    public int i;
    public foo(int ii = 0)
    {
        i = ii;
    }
    public void some(int ii)
    {
        i = ii;
        Console.WriteLine("In some ");
    }
}

class main
{
    static public void Main()
    {
        foo f = new foo();
        object o = new foo();

        Type t = typeof(foo);


        object[] args = new object[1];
        args[0] = 9;
        MethodInfo[] m = t.GetMethods();
        m[0].Invoke(o, args);
        //Console.WriteLine(o.i);
    }
}
  • 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-07T23:41:06+00:00Added an answer on June 7, 2026 at 11:41 pm

    But when we uncomment the last line of the code and try to compile it, it produces an error. Why??

    Because you’ve declared o as System.Object, which doesn’t have a variable i defined on it as far as the compiler is concerned. You’d need to either cast it to the known type, or use reflection to retrieve this value.

    For example:

    // You can cast here, since you know the type
    foo oAsFoo = o as foo;
    Console.WriteLine(oAsFoo.i);
    

    Alternatively, use reflection to get the value:

    FieldInfo field = t.GetField("i");
    Console.WriteLine(field.GetValue(o));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just discovered that the following code executes without error (Chrome): console.log(fa); function fa(){}
I have the following thread code which executes correct first time. After that from
In the Groovy console, the following code executes without error: class F { private
I have the following line of code that executes properly except for one hitch.
The following Python code executes normally without raising an exception: class Foo: pass class
I'm writing some code which takes a file, passes that file to one of
I have used following code to create a simple PDF file. It executes fine
I have tomcat 6.0 with 2 webapps. One of them executes the following code:
I got error Access Violation when the following code is execute : void NoAction()
I have reduced the problem to the following basic function which should simply print

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.