0

I have a question with generics, and think this may be impossible but thought Id throw it out there in hopes of finding some sort of solution. I have an object that uses generics, I want to set the generic on the fly. My problem is, or the reason why reflection can only get me so far, is that the object needs to be sent into a method as an out param. Any suggestions? The code is below

GetConfigurationSettingMessageResponse<DYNAMICALLYSETTHIS> ch;
if (MessagingClient.SendSingleMessage(request, out ch))
{
    if (ch.ConfigurationData != null)
    {
        data = ch.ConfigurationData;
    }
}
Lizzard
  • 193
  • 2
  • 12

3 Answers3

1

How about make a generic convenience method and then use reflection to call it.

void HelperMethod<TType>(){
    GetConfigurationSettingMessageResponse<TType> ch;
    if (MessagingClient.SendSingleMessage(request, out ch))
    {
        ... //Do your stuff.
    }

}
Community
  • 1
  • 1
jbtule
  • 31,383
  • 12
  • 95
  • 128
0

More of a workaround than a direct answer, but do you have to write the code so strongly typed? Perhaps you leave it as an GetConfigurationSettingMessageResponse<object> and test at a later stage to see what it is:

void SendSingleMessage(int request, out GetConfigurationSettingMessageResponse<object> obj)
{
    if (obj.InnerObject is Class1)
    {...}
    else if...
..
}

...

   class GetConfigurationSettingMessageResponse<T>
    {
        public T _innerObject { get; set; }
    }
Carlos P
  • 3,928
  • 2
  • 34
  • 50
  • Unfortunately yes it probably does, I'll talk to the team and suggest this though. It is a method on our centralized business server that all applications are hitting to get their settings on startup. I got the unfortunate task of working in the application that maintains all of these settings so it needs to be able to get all of them. – Lizzard Apr 20 '11 at 20:42
0

EDITED: Essentially, you need to pass in a reference to a type you can't know at compile time. In which case we need to defeat the compile time checking with reflection.

using System.Reflection;

public class ResponseWrapper {

  public static ConfigurationData GetConfiguration( Request request, Type dtype  )
  {
    // build the type at runtime
    Type chtype = typeof(GetConfigurationSettingMessgeResponse<>);
    Type gchtype = chtype.MakeGenericType( new Type[] { dtype } );

    // create an instance. Note, you'll have to know about your 
    // constructor args in advance. If the consturctor has no 
    // args, use Activator.CreateIntsance.

    // new GetConfigurationSettingMessageResponse<gchtype>
    object ch = Activator.CreateInstance(gchtype); 


    // now invoke SendSingleMessage ( assuming MessagingClient is a 
    // static class - hence first argument is null. 
    // now pass in a reference to our ch object.
    MethodInfo sendsingle = typeof(MessagingClient).GetMethod("SendSingleMessage");        
    sendsingle.Invoke( null, new object[] { request, ref ch } );

    // we've successfulled made the call.  Now return ConfigurtationData

    // find the property using our generic type
    PropertyInfo chcd = gchtype.GetProperty("ConfigurationData");
    // call the getter.
    object data = chcd.GetValue( ch, null );

    // cast and return data
    return data as ConfigurationData;


  }

}

Once you've done this, you could create a helper method to let you maniupulate the ch object instead of the GetProperty part.

IanNorton
  • 7,145
  • 2
  • 25
  • 28
  • I'm not too sure this applies to what I am looking for. I have a generic class, rather than a method. I need to send a variable as an out param in a method, where the variable is of Type of the Generic Class where the generic changes on the fly. – Lizzard Apr 20 '11 at 22:40
  • GetConfigurationSettingMessageResponse ch; MessagingClient.SendSingleMessage(request, out ch); – Lizzard Apr 20 '11 at 22:41
  • Ah, sorry, You might want to look at : typeof(GConfSetMessResp<>).MakeGenericType( new Type[] { dynamic type } ) But I have no idea how you would pass that in as a reference. – IanNorton Apr 20 '11 at 23:23
  • woke up, and re-wrote answer :) – IanNorton Apr 21 '11 at 05:58
  • Actually it was very useful, I've known about using reflection to invoke methods, but for some reason reading through your code a couple times gave me the idea to create a wrapper method where I can send in the Generic dynmically. – Lizzard Apr 21 '11 at 15:29