动态设置泛型

发布于 2024-11-02 13:48:53 字数 366 浏览 1 评论 0原文

我对泛型有疑问,并认为这可能是不可能的,但我想我会把它扔在那里,希望找到某种解决方案。 我有一个使用泛型的对象,我想动态设置泛型。我的问题是,或者反射只能让我到目前为止的原因是,对象需要作为输出参数发送到方法中。有什么建议吗?代码如下

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

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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绻影浮沉 2024-11-09 13:48:54

如何制作一个通用的便捷方法,然后使用 反射称之为

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

}

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

}
山色无中 2024-11-09 13:48:54

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

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

...

   class GetConfigurationSettingMessageResponse<T>
    {
        public T _innerObject { get; set; }
    }

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; }
    }
暮年慕年 2024-11-09 13:48:54

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

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;


  }

}

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文