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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:03:25+00:00 2026-05-27T05:03:25+00:00

below is a code example and the question, please note that I can NOT

  • 0

below is a code example and the question, please note that I can NOT use C# 4.0 and the dynamic keyword.

static class TestClass
{
    static void Main(string[] args)
    {
        Object o = "Previous value";
        Test(ref o);
        Trace.WriteLine(o);
    }

    static public void Test<T>(ref T obj)
    {
        //  The goal is to somehow invoke Test2 with the real type of obj, i.e the type in obj.GetType() 

        //  1st try:
        Test2(ref obj); // This doesn't work because the type in Test2 will be the same as T here.

        //  2nd try:
        MethodInfo mi = typeof(TestClass).GetMethod("Test2");
        mi = mi.MakeGenericMethod(new Type[] { obj.GetType() });
        mi.Invoke(null, new Object[] { obj }); // obj is no longer by reference so we need to store the object array and copy back the result after the call

        //  3rd try, successful implementation by the smartest mind of stack overflow :)
    }

    static public void Test2<T>(ref T s)
    {
        if (typeof(T) == typeof(String))
        {
            s = (T)(Object)"Hello world!";
        }
    }
}

I also tried some more methods using Delegate.CreateDelegate but without any luck.
Is this at all possible?

Edit: I’m not afraid of using a dynamic method (and MSIL assembler) but my knowledge in this area is very limited.

Edit2: Here’s an example that is closer to what I really trying to do:

public static class TypeHandler<T>
{
    public delegate void ProcessDelegate(ref T value);

    public static readonly ProcessDelegate Process = Init();

    private static ProcessDelegate Init()
    {
        //  Do lot's of magic stuff and returns a suitable delegate depending on the type
        return null;
    }
}


static class TestClass
{
    static public void Main(string[] args)
    {
        Object o = "Previous value";
        Test(ref o);
        Trace.WriteLine(o);
    }

    static public void Test<T>(ref T obj)
    {
        if (obj is T)
        {
        //  Optimized, common case
            TypeHandler<T>.Process(ref obj);
            return;
        }
        Type t = obj.GetType();
        //  How to call the delegate found in TypeHandler<t>.Process ? (I can get delegate but I can't call it).


    }

}
  • 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-27T05:03:26+00:00Added an answer on May 27, 2026 at 5:03 am

    Update 3: OK, since you’re fine with an ugly solution, you may want to check out the undocumented __refvalue and __makeref keywords.


    It seems your issue is that you want to be able to specify the type for a ref object parameter to be converted or changed to.

    The problem with that is that you can’t just arbitrarily assign a variable of any type T to a string, for example. So you’d need to pass in a ref object or ref string for that to work at all.

    It looks to me like oberfreak nailed down what you were trying to achieve (I originally failed to notice you had initialized o as a string and thus clearly wanted its actual type to influence the behavior of the Test2 function). His answer has the right approach for you.

    Update: you mention in a comment that what you’re trying to do is have dynamic behavior which could be achieved using a dictionary. I’m guessing that looks something like this?

    Update 2: updated this example based on your updated example.

    public static class TypeHandler // note: get rid of generic T parameter
    {
        delegate void ProcessDelegate(ref object obj); // again, not generic
    
        static Dictionary<Type, ProcessDelegate> processors = new Dictionary<Type, ProcessDelegate>()
        {
            { typeof(string), (ref object obj) => { obj = "Hello, world!"; } }
            // etc.
        };
    
        public static void Process(ref object obj)
        {
            processors[obj.GetType()].Invoke(ref obj);
        }
    }
    

    That should work. But you can’t really achieve the same thing with generics because there’s no way to do this (as you know):

    //          not allowed
    //               |
    //          -----------
    //         |           |
    TypeHandler<o.GetType()>.Process(ref o);
    

    If there were, then you’d be all set. But the only possible way you can do that is by using reflection, which is ugly and expensive for something like this and would clearly defeat your intention of keeping this simple and ensuring good performance.

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

Sidebar

Related Questions

I have a basic PHP question, take the code below for example, let's say
I know how to do this... I'll give example code below. But I can't
Below is an example class hierarchy and code. What I'm looking for is a
My question can better be explained via example code. I am using POCO with
(Note, the code below are just examples. Please don't comment on the why this
In the example code below, I have a question about the List. My prof
The example code below works as as a server process. But when I add
UPDATE 2011.09.13 This bug has been resolved by Adobe. The example code below now
I have example of code below. <script type=text/javascript src=assets/scripts/somescript.php>. </script> So, will my browser
Given the example source code below, is it possible for someone to see the

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.