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 64k
  • Answers 64k
  • 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 Have you set the delegate of the textfield to be… May 11, 2026 at 10:47 am
  • added an answer You should give X-Ray a try, it works for AS2… May 11, 2026 at 10:47 am
  • added an answer Your MySQL binaries should be somewhere under your XAMPP folder.… May 11, 2026 at 10:47 am

Related Questions

I have two implementations of a method, one for value types and another for
I have two applications written in Java that communicate with each other using XML
I have two arrays of animals (for example). $array = array( array( 'id' =>
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two elements: <input a> <input b onclick=...> When b is clicked, I
I have two identical tables and need to copy rows from table to another.
I have two classes, and want to include a static instance of one class
I have two threads, one needs to poll a bunch of separate static resources
I have two spreadsheets... when one gets modified in a certain way I want
I have two webapplication, one is a simple authenticationsite which can authenticate the logged

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.