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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:23:44+00:00 2026-05-15T19:23:44+00:00

I’m trying to use Type.InvokeMember(String, BindingFlags, Binder, Object, array []) with the default binder.

  • 0

I’m trying to use Type.InvokeMember(String, BindingFlags, Binder, Object, array[]) with the default binder.

one of the arguments to the target method in the object array is a reference type set to null. I want the method I’m invoking to instantiate the reference type so that I can continue using it. For example:

using System;

namespace ConsoleApplication6
{
    class A
    {
        public void GetReferenceType(object o)
        {
            o = new object();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            object o = null;
            A a = new A();
            Type t = typeof(A);
            t.InvokeMember("GetReferenceType", System.Reflection.BindingFlags.InvokeMethod, null, a, new object[] { o });

            if (o == null)
            {
                throw new NullReferenceException();
            }
            else
            {
                //do something with o;
            }
        }
    }
}

The workaround is to give A a Property and to access o through that.

Is there another way to do it without changing A?

  • 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-15T19:23:45+00:00Added an answer on May 15, 2026 at 7:23 pm

    Okay, you need to make two changes here:

    • Make your GetReferenceType parameter ref:

      public void GetReferenceType(ref object o)
      

      You have to do this because currently your method is a no-op to the outside world. You should read my article on parameter handling in C#.

    • Use the value within the array after InvokeMember, instead of the original reference:

      A a = new A();
      Type t = typeof(A);
      object[] args = new object[] { null };
      t.InvokeMember("GetReferenceType", BindingFlags.InvokeMethod,
                     null, a, args);
      object o = args[0];
      

    When you create an array e.g. with new object[] { o } that just copies the value of o into the array – it doesn’t associate that array element with the o variable.

    A better solution would be to make GetReferenceType return the new value, however… it’s rarely a good idea to use an out or ref parameter in a void method.

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

Sidebar

Ask A Question

Stats

  • Questions 449k
  • Answers 449k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In my opinion the format of your data should be… May 15, 2026 at 8:15 pm
  • Editorial Team
    Editorial Team added an answer Should there be quotes around timeout value? May 15, 2026 at 8:15 pm
  • Editorial Team
    Editorial Team added an answer If it's for your machine only, manipulating the hosts file… May 15, 2026 at 8:15 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.