2011-04-20 121 views
0

我有一個泛型的問題,並認爲這可能是不可能的,但想到Id拋出它,希望找到某種解決方案。 我有一個使用泛型的對象,我想設置通用即時。我的問題是,或反射只能讓我到目前爲止的原因是該對象需要作爲一個外部參數發送到一個方法。有什麼建議麼?代碼是低於動態設置泛型

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

什麼是實際問題? – 2011-04-20 20:19:57

+0

是您的'DYNAMICALLYSETTHIS'類型的'ConfigurationData'屬性? – Stormenet 2011-04-20 20:20:23

+0

@Stormenet沒有這是什麼動態的設置 – Lizzard 2011-04-25 15:32:04

回答

1

如何使通用便利方法,然後使用reflection to call it

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

} 
+0

瞧!這就是我結束了doig。非常感謝。 – Lizzard 2011-04-21 15:30:38

0

更多的解決方法,而不是直接的答案,但你必須寫代碼如此強烈的類型?也許你把它作爲一個GetConfigurationSettingMessageResponse<object>和測試在後一階段,看看它是什麼:

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

...

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

不幸的是,它可能會,我會和團隊交談,並提出這個問題。這是我們的集中式業務服務器上的一種方法,即所有應用程序都會在啓動時啓動它們的設置。我在應用程序中工作的不幸任務是維護所有這些設置,因此它需要能夠獲取所有這些設置。 – Lizzard 2011-04-20 20:42:24

0

編輯:從本質上講,你需要在引用傳遞的類型你無法在編譯時知道。在這種情況下,我們需要用反射來挫敗編譯時間檢查。

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; 


    } 

} 

一旦你做到了這一點,你可以創建一個輔助方法,讓你maniupulate的通道對象,而不是使用getProperty部分。

+0

我不太確定這適用於我所尋找的。我有一個泛型類,而不是一個方法。我需要發送一個變量作爲一個方法中的出參數,其中的變量是通用類的類型,其中通用變化是即時的。 – Lizzard 2011-04-20 22:40:54

+0

GetConfigurationSettingMessageResponse ch; MessagingClient.SendSingleMessage(request,out ch); – Lizzard 2011-04-20 22:41:36

+0

對不起,您可能想看看: typeof(GConfSetMessResp <>)。MakeGenericType(new Type [] {dynamic type}) 但我不知道如何將它作爲參考傳入。 – IanNorton 2011-04-20 23:23:14