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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:49:52+00:00 2026-05-12T06:49:52+00:00

Can you create a delegate of an instance method without specifying the instance at

  • 0

Can you create a delegate of an instance method without specifying the instance at creation time? In other words, can you create a “static” delegate that takes as it’s first parameter the instance the method should be called on?

For example, how can I construct the following delegate using reflection?

Func<int, string> = i=>i.ToString();

I’m aware of the fact that I can use methodInfo.Invoke, but this is slower, and does not check for type-correctness until it is called.

When you have the MethodInfo of a particular static method, it is possible to construct a delegate using Delegate.CreateDelegate(delegateType, methodInfo), and all parameters of the static method remain free.

As Jon Skeet pointed out, you can simply apply the same to make an open delegate of an instance method if the method is non-virtual on a reference type. Deciding which method to call on a virtual method is tricky, so that’s no so trivial, and value-types look like they don’t work at all.

For value types, CreateDelegate exhibits really weird behavior:

var func37 = (Func<CultureInfo,string>)(37.ToString);
var toStringMethod = typeof(int).GetMethod("ToString", BindingFlags.Instance | BindingFlags.Public, null, new Type[] {typeof(CultureInfo) }, null);
var func42 = (Func<CultureInfo,string>)Delegate.CreateDelegate(typeof(Func<CultureInfo,string>), 42, toStringMethod,true);
Console.WriteLine( object.ReferenceEquals(func37.Method,func42.Method)); //true
Console.WriteLine(func37.Target);//37
Console.WriteLine(func42.Target);//42
Console.WriteLine(func37(CultureInfo.InvariantCulture));//37
Console.WriteLine(func42(CultureInfo.InvariantCulture));//-201040128... WTF?

Calling CreateDelegate with null as the target object throws a binding exception if the instance method belonged to a value type (this works for reference types).

Some follow-up years later: The incorrectly-bound target that caused func42(CultureInfo.InvariantCulture); to return "-201040128" instead of "42" in my example was memory corruption that could have allowed remote code execution (cve-2010-1898); this was fixed in 2010 in the ms10-060 security update. Current frameworks correctly print 42! That doesn’t make answering this question any easier, but explains the particularly weird behavior in the example.

  • 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-12T06:49:53+00:00Added an answer on May 12, 2026 at 6:49 am

    You’ve actually chosen a particularly tricky example, for two reasons:

    • ToString() is a virtual method inherited from object but overridden in Int32.
    • int is a value type, and there are weird rules with Delegate.CreateDelegate() when it comes to value types and instance methods – basically the first effective parameter becomes ref int rather than int

    However, here’s an example for String.ToUpper, which doesn’t have either of those problems:

    using System;
    using System.Reflection;
    
    class Test
    {
        static void Main()
        {
            MethodInfo method = typeof(string).GetMethod
                ("ToUpper", BindingFlags.Instance | BindingFlags.Public,
                 null, new Type[]{}, null);
    
            Func<string, string> func = (Func<string, string>)
                Delegate.CreateDelegate(typeof(Func<string, string>),
                                        null,
                                        method);
    
            string x = func("hello");
    
            Console.WriteLine(x);
        }
    }
    

    If that’s good enough for you, great… if you really want int.ToString, I’ll have to try a bit harder 🙂

    Here’s an example for a value type, using a new delegate type which takes its first parameter by reference:

    using System;
    using System.Reflection;
    
    public struct Foo
    {
        readonly string value;
    
        public Foo(string value)
        {
            this.value = value;
        }
    
        public string DemoMethod()
        {
            return value;
        }
    }
    
    class Test
    {
        delegate TResult RefFunc<TArg, TResult>(ref TArg arg);
    
        static void Main()
        {
            MethodInfo method = typeof(Foo).GetMethod
                ("DemoMethod", BindingFlags.Instance | BindingFlags.Public,
                 null, new Type[]{}, null);
            RefFunc<Foo, string> func = (RefFunc<Foo, string>)
                Delegate.CreateDelegate(typeof(RefFunc<Foo, string>),
                                        null,
                                        method);
    
            Foo y = new Foo("hello");
            string x = func(ref y);
    
            Console.WriteLine(x);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As mentioned in the blog post provided by Justin, it… May 13, 2026 at 1:45 am
  • Editorial Team
    Editorial Team added an answer You can rescue errors as normal. For example: begin ActiveRecord::Base.connection.execute(some_query)… May 13, 2026 at 1:45 am
  • Editorial Team
    Editorial Team added an answer That means you are trying to call non-const member function… May 13, 2026 at 1:45 am

Related Questions

After googling and landing on SO and having read this other question Is it
How can I call a method on a form from a method called from
I'm designing a delegate method that is called when the remote server needs input
I'm developing for the iPhone, and I have a class DataManager , that is

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.