3

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).


    }

}
eq_
  • 109
  • 6
  • it sounds like you shouldn't attempt to use generics. you're using reflection anyway so just lean on that – Adam Ralph Dec 02 '11 at 14:09
  • 1
    The fact that you are checking the type of your generic parameter is a red flag. Perhaps generics are not the right thing to be using here. – cadrell0 Dec 02 '11 at 14:17

5 Answers5

2

The main questions, in my opinion is, what do you want to do?

If you just want to assign a string to a reference object you could try this:

Generics can be defined during runtime, but it's not verry comfortable and has to be done by reflection ... in my oppinion it is a "no go", but just an oppinion

Try using the object.GetType() to get the current Type of the Object.

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

    static public void Test<T>(ref T obj) {
        Object o = (Object)obj;
        Test2(ref o);
        obj = (T)o;
    }

    static public void Test2(ref object s) {
        if (s.GetType().Equals(typeof(String))) {
            s = "Hello world!";
        }
    }
}
oberfreak
  • 1,799
  • 13
  • 20
  • This doesn't even compile and isn't close to what I need, if I only needed to work for Object / String types it would be simple enough. I need it to work for any type to any type that implements the other type. – eq_ Dec 02 '11 at 14:13
  • 1
    @user1077451: How are you planning to do this without using a giant series of `if`/`else` statements? You seem to want to achieve something with generics that they simply aren't good at. (Generics give you *same behavior* for a variety of types; what you're after appears to be *different behavior* for different types.) – Dan Tao Dec 02 '11 at 14:19
  • i updated the code, sorry for that mistake! It now compiles propperly. Why does this code doesn't solve your problem? you can check the type of object in Test2 and decide which object to assign? – oberfreak Dec 02 '11 at 14:20
  • I do not wish to enforce Test to only accept a reference to an object, it should work on any type, otherwise I need to copy all value types (after the object changed). Object o = anObjectOfAnyType; Test(ref o); anObjectOfAnyType = o; – eq_ Dec 02 '11 at 14:45
  • Yeah, but will it still print "Hello world"? ;) You need to add a obj = (T)(Object)o; at the end of the Test method... – eq_ Dec 02 '11 at 14:56
2

Your comment looks like you already understand how to do it:

MethodInfo mi = typeof(TestClass).GetMethod("Test2");
mi = mi.MakeGenericMethod(new Type[] { obj.GetType() });
object[] args = new object[] { obj };
mi.Invoke(null, args);
obj = (T) args[0];

That's really just turning your comment into code. Does that somehow not do what you want?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Yes I do know how to it like this (it's the way I do things today), but this is an optimization exercise and I'd like to avoid the by value copy (for value types). – eq_ Dec 02 '11 at 14:10
  • @user1077451: That suggests that performance is important for you. What are your performance goals, and what does the current code reach? (I suspect that dynamic methods could improve things, but I wouldn't go there unless you really have to.) – Jon Skeet Dec 02 '11 at 14:13
  • Good question! I don't have a fixed target but my current working implementation was created as a proof of concept, it works perfectly but it's slow. I'm now rewriting trying to use every trick in the book (my limited book), to achieve the same thing as fast as possible, I'd be happy if I could get things working at 20% of the current performance. In this iteration I also added in more flexibility to the system so I'm not really expect to hit the mark in this iteration. For the next round I will consider using dynamic methods and MSIL, but I first wanted to nail the infrastructure. – eq_ Dec 02 '11 at 14:52
  • I.e: First iteration: Every type is handled in a single function with lot's of if / else statements invoking virtual functions here and there + using reflection in some cases. Second iteration: Do optimize code path per initial type (i.e using a Type to Code lookup). Third iteration: Rewrite code for each case using MSIL (because there are to many cases to handle comfortable by hand). – eq_ Dec 02 '11 at 14:54
  • @user1077451: How much of your current time is spent in this bit of code? – Jon Skeet Dec 02 '11 at 14:55
  • Enough ;) Believe me it's necessary to optimize. I've been developing games then more advanced systems as a profession for over fifteen years so I do know what not to spend time on :) I'm quite new to .net / C# though, coming from a C++ world many of the things that are so easy to do unmanaged is extremely annoyingly complex in managed code. I know that rewriting everything in unmanaged code would probably give me a 10x speed boost, but it's about a year of development behind this project from me atm, so going down that route is a big no no (and I'm not allowed to use unmanaged anyway). – eq_ Dec 02 '11 at 15:01
1

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.

Community
  • 1
  • 1
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
  • This is somewhat similar to what I consider, as always my actual problem isn't as simple as the sample. The types I like to support (in Test2) is not static as in your example, rather it's like a Dictionary of Type to "TypeHandler" delegate thingy, then I have someone register what types to handle and with what delegate. Problem is I'm trying to use another approach than to actually use a Dictionary, namely having a static generic class, that has a static field with the delegate (initialized using a static init function). I was hoping to see an improvement in speed over the Dictionary lookup. – eq_ Dec 02 '11 at 14:19
  • But the problem is here you want this class to behave in a certain way based on the *actual* type of a variable. In this case, `o`, for example, is a `string`. But this isn't what generics are based on, which is *declared* types. In this case that means `object` for `o`. So this is why it seems to me personally you are barking up the wrong tree with generics; and I feel oberfreak's answer is actually what's pointing you in the right direction. – Dan Tao Dec 02 '11 at 14:25
  • Yes I somehow agree with you that generics probably isn't the right way of doing things. I've updated my original question with an example closer to what I'm trying to do. – eq_ Dec 02 '11 at 14:35
  • That is conceptually what I need, problem is that I do not want Test1 to accept a reference to an object (alone), it should accept a reference to any type (or I need to pay the copy of value types again). I know that if there is a solution that doesn't require a copy it's not going to be pretty, but hey this is not a beauty contest, is it? ;) – eq_ Dec 02 '11 at 14:41
  • @user1077451: It's not about being pretty. It's about performance. **You simply cannot save on performance by introducing generics and then adding lots of reflection.** The reflection overhead will absolutely *dwarf* the tiny performance gain you get by avoiding the value copies. – Dan Tao Dec 02 '11 at 14:50
  • True to that but I was hoping to be able to cache the resulting method call in a delegate, only paying the price for the reflection path once per Type/Type pair (which isn't to bad atm). I have about 100K calls with ~200 different types so that seemed like a good idea, but maybe I'm probably wrong, quite a newbie at C# / managed code. – eq_ Dec 02 '11 at 15:06
  • These keywords looks interesting, I can at least use them to avoid boxing, still not sure if/how I can use them to solve the current problem. Thanks anyway! – eq_ Dec 02 '11 at 15:27
0

The right way to implement method Test2 would be

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

Or am I missing something here?

matts
  • 21
  • 2
0

If you can change the ref to a regular return, you can cheat massively in 4.0 via dynamic:

dynamic foo = obj;
Test(foo);

that is now:

Test<TheActualTypeOfObj>(obj);

Full example:

static void Main(string[] args)
{
    object o = "Previous value";
    o = Test2((dynamic)o);
    Trace.WriteLine(o);
}

static public T Test2<T>(T s)
{
    if (typeof(T) == typeof(string))
    {
        s = (T)(object)"Hello world!";
    }
    return s;
}

which writes "Hello world!"

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900