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

The Archive Base Latest Questions

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

I am attempting to invoke a method on a ref class instance through reflection,

  • 0

I am attempting to invoke a method on a ref class instance through reflection, which returns a native pointer.

Example ref class header:

public ref class MyRefClass : public IDisposable
{
public:

MyNativeType* GetNativeInstance();

//Rest of the header...
}

here is an example of a failed attempt at reflection

void InvokeTheMethod(Object^ obj)
{
     MyNativeType* myNative;
     GetNativeInvoker^ del = (GetNativeInvoker^) Delegate::CreateDelegate(GetNativeInvoker::typeid, obj, "GetNativeInstance");

     //get pointer and use if bind succeeds myNative = del();
     //else handle the case where the Object does not have GetNativeInstance()
}

using this delegate

delegate MyNativeType* GetNativeInvoker();

When trying to create the delegate the bind fails with ArgumentException even if object is an instance of a ref class that has the method “GetNativeInstance” (like MyRefClass). This problem has to be solved without knowing anything about obj’s type at compile time other than the fact it is an Object^.

  • 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-23T14:17:16+00:00Added an answer on May 23, 2026 at 2:17 pm

    The problem is that you are not using Reflection. Delegate::CreateDelegate() is only allowed on delegate types, yours isn’t one. Use Reflection to fix, Type::GetMethod() returns a method. Like this (minus cleanup):

    using namespace System;
    using namespace System::Reflection;
    
    class MyNativeType {};
    
    public ref class MyRefClass
    {
        MyNativeType* instance;
    public:
        delegate void* GetNativeInvoker();
    
        MyRefClass() { instance = new MyNativeType; }
        MyNativeType* GetNativeInstance() { return instance; }
    
        static void Test() {
            MyRefClass^ obj = gcnew MyRefClass;
            MethodInfo^ mi = obj->GetType()->GetMethod("GetNativeInstance");
            Object^ result = mi->Invoke(obj, nullptr);
            void* ptr = Pointer::Unbox(result);
            System::Diagnostics::Debug::Assert(ptr == obj->GetNativeInstance());
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to use an HttpDelete object to invoke a web service's delete method.
I'm passing an object reference to a Util class. I'm attempting to invoke a
I am attempting to invoke the hasNext method in a Velocity template, in order
I'm attempting to pass an std::function via a method like so: class dispatch {
Attempting to use the data series from this example no longer passes the JSONLint
I'm attempting to P/Invoke a C library from F#, and have encountered a peculiar
I am attempting to create an Expression that will invoke a specific generic overloaded
Im attempting to bind to the output of a method. Now I've seen examples
I am attempting to create an enhanced Intent class (I call it DataIntent )
I'm attempting to speed up a collection of scripts that invoke subshells and do

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.