C# 动态设置泛型

C# 动态设置泛型,c#,generics,dynamic,C#,Generics,Dynamic,我有一个关于泛型的问题,我认为这可能是不可能的,但我想我会把它扔出去,希望找到某种解决办法。 我有一个使用泛型的对象,我想动态设置泛型。我的问题是,或者说反射只能让我这么做的原因是,需要将对象作为out参数发送到方法中。有什么建议吗?代码如下 GetConfigurationSettingMessageResponse<DYNAMICALLYSETTHIS> ch; if (MessagingClient.SendSingleMessage(request, out ch)) {

我有一个关于泛型的问题,我认为这可能是不可能的,但我想我会把它扔出去,希望找到某种解决办法。 我有一个使用泛型的对象,我想动态设置泛型。我的问题是,或者说反射只能让我这么做的原因是,需要将对象作为out参数发送到方法中。有什么建议吗?代码如下

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

GetConfigurationSettingMessageResponse ch;
if(MessagingClient.SendSingleMessage(请求,输出通道))
{
如果(ch.ConfigurationData!=null)
{
数据=配置数据;
}
}

与其说是直接回答,不如说是一种变通方法,但您是否必须编写强类型的代码?也许您将其保留为
GetConfigurationSettingMessageResponse
,并在稍后的阶段进行测试,以查看它是什么:

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

void SendSingleMessage(int请求,out GetConfigurationSettingMessageResponse obj)
{
if(obj.InnerObject为Class1)
{...}
否则如果。。。
..
}


class GetConfigurationSettingMessageResponse
{
公共T_innerObject{get;set;}
}

EDITED:本质上,您需要传入对编译时不知道的类型的引用。在这种情况下,我们需要通过反射来消除编译时检查

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;


  }

}
使用系统反射;
公共类响应包装器{
公共静态配置数据GetConfiguration(请求,类型dtype)
{
//在运行时生成类型
类型chtype=typeof(GetConfigurationSettingMessgresponse);
类型gchtype=chtype.MakeGenericType(新类型[]{dtype});
//创建一个实例。注意,您必须了解
//构造函数参数预先设置。如果构造函数没有
//args,使用Activator.CreateIntsance。

//新GetConfigurationSettingMessageResponse 对象ch=Activator.CreateInstance(gchtype); //现在调用SendSingleMessage(假设MessagingClient是 //静态类-因此第一个参数为null。 //现在传入对ch对象的引用。 MethodInfo sendsingle=typeof(MessagingClient).GetMethod(“SendSingleMessage”); Invoke(null,新对象[]{request,ref ch}); //我们已成功呼叫。现在返回ConfigurationData //使用泛型类型查找属性 PropertyInfo chcd=gchtype.GetProperty(“配置数据”); //打电话给救世主。 对象数据=chcd.GetValue(ch,null); //强制转换和返回数据 将数据作为配置数据返回; } }

完成此操作后,可以创建一个helper方法,让您操作ch对象而不是GetProperty部分。

制作一个通用的便利方法,然后使用它如何

void HelperMethod(){

GetConfigurationSettingMessageResponse ch; if(MessagingClient.SendSingleMessage(请求,输出通道)) { …做你的事。 } }
您的
配置数据
属性是否属于
动态设置类型此
?@Stormenet否这是动态设置所需的。幸运的是,它可能会这样做,我将与团队讨论并提出建议。这是我们集中的业务服务器上的一种方法,所有应用程序都在使用该方法在启动。我在维护所有这些设置的应用程序中完成了一项不幸的任务,因此它需要能够获取所有这些设置。我不太确定这是否适用于我正在寻找的内容。我有一个泛型类,而不是一个方法。我需要在方法中发送一个变量作为out参数,其中该变量的类型为泛型类中的泛型更改正在进行中。GetConfigurationSettingMessageResponse ch;MessagingClient.SendSingleMessage(request,out ch);啊,对不起,您可能需要查看:typeof(gconfsetMessrep)。MakeGenericType(新类型[]{dynamic Type}),但我不知道如何将其作为引用传入。唤醒,然后重新编写答案:)实际上它非常有用,我知道如何使用反射来调用方法,但是出于某种原因,我读了几遍你的代码后,产生了创建一个包装器方法的想法,在这里我可以以动态方式发送通用的方法。瞧!这就是我最后做的。非常感谢。
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;


  }

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

}