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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:53:30+00:00 2026-05-30T17:53:30+00:00

I have been testing out using delegates instead of reflection for some object sorting

  • 0

I have been testing out using delegates instead of reflection for some object sorting and it was working good for string properties, but if I try an Int (or DateTime) it fails and throws

Error binding to target method.

class Program
{
    static void Main(string[] args)
    {
        var sample = new SampleClass() { Num = 13, Text = "Sample" };

        Console.WriteLine(ReadProp(sample,"Text")); //Works
        Console.WriteLine(ReadProp(sample, "classProp")); //Works
        Console.WriteLine(ReadProp(sample, "Num")); //Throws 'Error binding to target method.'
    }

    //Use a Delegate to improve speed of accessing property instead of reflection
    static object ReadProp(SampleClass obj, string propName)
    {
        var method = obj.GetType().GetProperty(propName).GetGetMethod();
        var getForProp = (Func<SampleClass, object>)Delegate.CreateDelegate(typeof(Func<SampleClass, object>), null, method);
        return getForProp(obj);
    }
}

//A sample class for illustration purposes
class SampleClass
{
    public string Text { get; set; }
    public int Num { get; set; }
    public SampleClass classProp { get; set; }
}

My question is: Why does it work for strings or other classes but not for Int or DateTime?

I can see that if I change my Func<SampleClass, object> to Func<SampleClass, int> it will then work for my Int but I expected object would work for both.

  • 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-30T17:53:32+00:00Added an answer on May 30, 2026 at 5:53 pm

    Why does it work for strings or other classes but not for Int or DateTime?

    Because a method which returns an int isn’t a method which returns an object reference. There has to be a boxing conversion – so something’s got to do that, and Delegate.CreateDelegate is trying to provide a delegate which just invokes the delegate and returns the result, with no value conversion involved.

    It’s slightly painful to do, but basically I suspect you should either be building a Func with an appropriate return value and using that directly or you should go via a wrapper delegate which calls the “real” delegate and boxes the result.

    (Note that in your sample code, you’re creating the delegate each time, which is going to be no faster than reflection. Hopefully your real code is more sensible 🙂

    If you look at this code from protobuf-csharp-port you’ll see I’ve got a method to do exactly this – a lambda expression calls the strongly-typed delegate, and then uses the implicit conversion to object (boxing where necessary) to provide the value. You should be able to use something very similar. However, if you’re sorting do you really want a Func<T, object>? If you use a strongly-typed delegate instead, you may be able to avoid all this boxing.

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

Sidebar

Related Questions

I have been using selenium IDE to do some web app testing. I have
I have been working on some customisation for SharePoint 2010 using Visual Studio. My
I have been using CPPUnit as a unit testing framework and am now trying
I have been looking at using TDD and implementing proper testing (only just started
I have been doing TDD and was using it more as unit testing than
I am putting together some ideas for our automated testing platform and have been
I've been trying to implement unit testing and currently have some code that does
I'm using HttpClient for testing and when I have been restarting/redeploying to Tomcat I
I have been trying to add columns to a table using some logic that
I have recently been testing out customer portals on Sites in Salesforce. Ideally I

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.