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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:34:53+00:00 2026-05-28T01:34:53+00:00

I have a static method: public class Example { //for demonstration purposes – just

  • 0

I have a static method:

public class Example
{
    //for demonstration purposes - just returns default(T)
    public static T Foo<T>() { return default(T); }
}

And I need to be able to invoke it using a Type parameter calls to which could be numerous, so my standard pattern is to create a thread-safe cache of delegates (using ConcurrentDictionary in .Net 4) which dynamically invoke the Foo<T> method with the correct T. Without the caching, though, the code is this:

static object LateFoo(Type t) 
{ 
  //creates the delegate and invokes it in one go
  return (Func<object>)Delegate.CreateDelegate( 
    typeof(Func<object>), 
    typeof(Example).GetMethod("Foo", BindingFlags.Public | BindingFlags.Static). 
      MakeGenericMethod(t))(); 
}

This is not the first time I’ve had to do this – and in the past I have use Expression trees to build and compile a proxy to invoke the target method – to ensure that return type conversion and boxing from int -> object (for example) is handled correctly.

Update – example of Expression code that works

static object LateFoo(Type t)
{
  var method = typeof(Example)
               .GetMethod("Foo", BindingFlags.Public | BindingFlags.Static)
               .MakeGenericMethod(t); 
  //in practise I cache the delegate, invoking it freshly built or from the cache
  return Expression.Lambda<Func<IField, object>>(Expression.Convert(
    Expression.Call(method), typeof(object))).Compile()();
}

What’s slightly amusing is that I learned early on with expressions that an explicit Convert was required and accepted it – and in lieu of the answers here it does now make sense why the .Net framework doesn’t automatically stick the equivalent in.

End update

However, this time I thought I’d just use Delegate.CreateDelegate as it makes great play of the fact that (from MSDN):

Similarly, the return type of a delegate is compatible with the return type of a method if the return type of the method is more restrictive than the return type of the delegate, because this guarantees that the return value of the method can be cast safely to the return type of the delegate.

Now – if I pass typeof(string) to LateFoo method, everything is fine.

If, however, I pass typeof(int) I get an ArgumentException on the CreateDelegate call, message: Error binding to target method. There is no inner exception or further information.

So it would seem that, for method binding purposes, object is not considered more restrictive than int. Obviously, this must be to do with boxing being a different operation than a simple type conversion and value types not being treated as covariant to object in the .Net framework; despite the actual type relationship at runtime.

The C# compiler seems to agree with this (just shortest way I can model the error, ignore what the code would do):

public static int Foo()
{
    Func<object> f = new Func<object>(Foo);
    return 0;
}

Does not compile because the Foo method ‘has the wrong return type’ – given the CreateDelegate problem, C# is simply following .Net’s lead.

It seems to me that .Net is inconsistent in it’s treatment of covariance – either a value type is an object or it’s not; & if it’s not it should not expose object as a base (despite how much more difficult it would make our lives). Since it does expose object as a base (or is it only the language that does that?), then according to logic a value type should be covariant to object (or whichever way around you’re supposed to say it) making this delegate bind correctly. If that covariance can only be achieved via a boxing operation; then the framework should take care of that.

I dare say the answer here will be that CreateDelegate doesn’t say that it will treat a box operation in covariance because it only uses the word ‘cast’. I also expect there are whole treatises on the wider subject of value types and object covariance, and I’m shouting about a long-defunct and settled subject. I think there’s something I either don’t understand or have missed, though – so please enlighten!

If this is unanswerable – I’m happy to delete.

  • 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-28T01:34:54+00:00Added an answer on May 28, 2026 at 1:34 am

    You can only convert a delegate in this way if the parameters and return value can be converted using a representation conserving conversion.

    • Reference types can only be converted to other reference types in this way
    • Integral values can be converted to other integer values of the same size (int, uint, and enums of the same size are compatible)

    A few more relevant blog articles:

    This dichotomy motivates yet another classification scheme for conversions (†). We can divide conversions into representation-preserving conversions (B to D) and representation-changing conversions (T to U). (‡) We can think of representation-preserving conversions on reference types as those conversions which preserve the identity of the object. When you cast a B to a D, you’re not doing anything to the existing object; you’re merely verifying that it is actually the type you say it is, and moving on. The identity of the object and the bits which represent the reference stay the same. But when you cast an int to a double, the resulting bits are very different.

    This is why covariant and contravariant conversions of interface and delegate types require that all varying type arguments be of reference types. To ensure that a variant reference conversion is always identity-preserving, all of the conversions involving type arguments must also be identity-preserving. The easiest way to ensure that all the non-trivial conversions on type arguments are identity-preserving is to restrict them to be reference conversions.
    http://blogs.msdn.com/b/ericlippert/archive/2009/03/19/representation-and-identity.aspx

    “but how can a value type, like int, which is 32 bits of memory, no more, no less, possibly inherit from object? An object laid out in memory is way bigger than 32 bits; it’s got a sync block and a virtual function table and all kinds of stuff in there.” Apparently lots of people think that inheritance has something to do with how a value is laid out in memory. But how a value is laid out in memory is an implementation detail, not a contractual obligation of the inheritance relationship! When we say that int inherits from object, what we mean is that if object has a member — say, ToString — then int has that member as well.
    http://ericlippert.com/2011/09/19/inheritance-and-representation/

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

Sidebar

Related Questions

I have a static method on a public class. Example: public class MyClass {
I have the following class: public abstract class AbstractParent { static String method() {
First let's establish this. I have public abstract class Foo { public static void
I have this static method on C# : public class MyClass { public MyClass()
I have a static method which sets a variable: static String[] playersNames; public static
I have the following method public static void SerializeToXMLFile(Object obj,Type type, string fileName) {
Welcome! I have a recursive public static method named less that takes a tree
the ToSelectList method I have: public static IList<SelectListItem> ToSelectList<T>(this IEnumerable<T> itemsToMap, Func<T, string> textProperty,
I have this factory method in java: public static Properties getConfigFactory() throws ClassNotFoundException, IOException
Let's say we have a method signature like public static function explodeDn($dn, array &$keys

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.