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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:01:44+00:00 2026-05-10T23:01:44+00:00

I have two implementations of a method, one for value types and another for

  • 0

I have two implementations of a method, one for value types and another for reference types:

public static Result<T> ImplRef(T arg) where T : class {...} public static Result<T> ImplVal(T arg) where T : struct {...} 

I want to write a method which calls the correct implementation like this

public static Result<T> Generic(T arg) {     if (typeOf(T).IsValueType)          return ImplVal(arg);     else          return ImplRef(arg); } 

Obviously, the above implementation doesn’t compile. How can I do this with minimum of reflection?

  • 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. 2026-05-10T23:01:44+00:00Added an answer on May 10, 2026 at 11:01 pm

    The idea with generics is usually to do the same logic with whichever inputs you are given, although obviously you need to be practical. Personally, I’d probably use two different methods, rather than brute-force them into the same method, but that would make it hard to call from a generic method just knowing about T. There is no way of satisfying the : class / : struct from static code, although the MakeGenericMethod approach might work, but will be an order of magnitude slower.

        // slow; use with caution     public static Result<T> Generic<T>(T arg) {         if (typeof(T).IsValueType)             return (Result<T>)typeof(Program).GetMethod('ImplVal')                 .MakeGenericMethod(typeof(T))                 .Invoke(null, new object[] {arg});         else             return (Result<T>)typeof(Program).GetMethod('ImplRef')                 .MakeGenericMethod(typeof(T))                 .Invoke(null, new object[] { arg });     } 

    (substitute typeof(Program) with the type that hosts the methods)

    The alternative (as Jon notes) is to cache the (typed) delegate to the method:

    public static Result<T> Generic<T>(T arg) {     return Cache<T>.CachedDelegate(arg); }  internal static class Cache<T> {     public static readonly Func<T, Result<T>> CachedDelegate;     static Cache()     {         MethodInfo method;         if (typeof(T).IsValueType)             method = typeof(Program).GetMethod('ImplVal')                 .MakeGenericMethod(typeof(T));         else             method = typeof(Program).GetMethod('ImplRef')                 .MakeGenericMethod(typeof(T));         CachedDelegate = (Func<T, Result<T>>)Delegate.CreateDelegate(             typeof(Func<T, Result<T>>), method);     } } 

    More work, but will be plenty quick. The static constructor (or you could use a property/null check) ensures we only do the hard work once.

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

Sidebar

Ask A Question

Stats

  • Questions 77k
  • Answers 77k
  • 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
  • added an answer If you start a thread like this: System.Threading.Thread mythread =… May 11, 2026 at 3:35 pm
  • added an answer I haven't encountered a list but if one existed it… May 11, 2026 at 3:35 pm
  • added an answer HKEY_CLASSES_ROOT\.[extension] default key contains a REG_SZ string, that is the… May 11, 2026 at 3:35 pm

Related Questions

I'm curious about the UserDetailsManager interface. It's not mentioned in the reference guide and
I am trying to generate equivalent MD5 hashes in both JavaScript and .Net. Not
I have created my own Tree implementation for various reasons and have come up
I'm currently writing an implementation of a JDBC driver ( yes, you read that

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.