2011-04-14 59 views
2

尷尬的標題我知道,最好在代碼中解釋。給定一組類:鑑於一個受限制的泛型方法,我可以調用一個非泛型方法來傳遞泛型參數的實際類型。

public abstract class MyBaseType 
{ 
    public string Message { get; set; } 
} 

public class MySuperType : MyBaseType 
{ 
    public string AdditionalInfo { get; set; } 
} 

public class MyOtherSuperType : MyBaseType 
{ 
    public DateTime Started { get; set; } 
    public DateTime Finished { get; set; } 
} 

有沒有編寫調用傳遞泛型類型,而解釋傳遞類型爲實際類型而不是基本類型非泛型方法泛型方法的一種方式。也就是說,我想寫的是這樣的:因爲T被解釋爲基本型

public void OutputFormattedTypeInfo<T>(T theType) where T : MyBaseType 
{ 
    OutputFormattedTypeInfo(theType as T); 
} 

public void OutputFormattedTypeInfo(MySuperType theType) 
{ 
    System.Console.WriteLine(String.Format("{0} and {1}", theType.Message, theType.AdditionalInfo)); 
} 

public void OutputFormattedTypeInfo(MyOtherSuperType theType) 
{ 
    System.Console.WriteLine(String.Format("{0} - Start: {1}, End: {2}", theType.Message, theType.Started, theType.Finished)); 
} 

但顯然theType。我知道我可以用這樣的反思:

Type type = typeof(MyBaseTypeDisplayFormatter); 
    MethodInfo method = type.GetMethod(
            "FormatSpecific", 
            BindingFlags.Instance | BindingFlags.NonPublic, 
            null, 
            new[] { update.GetType() }, 
            null); 

    return (MyBaseTypeDataItem)method.Invoke(this, new object[] { update }); 

但它只是感覺不雅。有沒有更好的辦法?

回答

1

正如Aliostad說,你試圖做的不再是通用的,只是使用重載會更好。看起來你正在嘗試在C++中做類似於模板特化的事情,其中​​根據泛型來調用不同的方法。

下面是一個例子,我使用反射實現了一種通用專業化,如果重載方法不適合您,也許可以應用類似的模式。如果你可以緩存反射結果並且只調用一次GetMethod,那麼結果不會太慢。裏面的一類通用的T有一個調用方法:

if (_serializeDataToStream == null) 
    _serializeDataToStream = (Action<BinaryWriter, int, T[]>)GetTypeSpecificSerializationMethod(); 

_serializeDataToStream(writer, _size, _data); 

凡GetTypeSpecific方法使用反射來創建一個委託

/// <summary> 
/// Returns a delegate that points at the static type specific serialization method 
/// </summary> 
/// <returns></returns> 
private Delegate GetTypeSpecificDeserializationMethod() 
{ 
    if (typeof(T) == typeof(double)) 
    { 
     MethodInfo method = this.GetType().GetMethod("DeserializeDouble", BindingFlags.Static | BindingFlags.NonPublic); 
     return Delegate.CreateDelegate(typeof(Action<BinaryReader, int, T[]>), method); 
    } 
    else if (typeof(T) == typeof(ushort)) 
    { 
     MethodInfo method = this.GetType().GetMethod("DeserializeUshort", BindingFlags.Static | BindingFlags.NonPublic); 
     return Delegate.CreateDelegate(typeof(Action<BinaryReader, int, T[]>), method); 
    } 
    else if (typeof(T) == typeof(DateTime)) 
    { 
     MethodInfo method = this.GetType().GetMethod("DeserializeDateTime", BindingFlags.Static | BindingFlags.NonPublic); 
     return Delegate.CreateDelegate(typeof(Action<BinaryReader, int, T[]>), method); 
    } 
    else if (typeof(T) == typeof(bool)) 
    { 
     MethodInfo method = this.GetType().GetMethod("DeserializeBool", BindingFlags.Static | BindingFlags.NonPublic); 
     return Delegate.CreateDelegate(typeof(Action<BinaryReader, int, T[]>), method); 
    } 

    throw new NotImplementedException("No deserialization method has been setup for type " + typeof(T).FullName); 
} 

/// <summary> 
/// Serialize double[] to BinaryWriter 
/// </summary> 
/// <param name="writer"></param> 
/// <param name="size"></param> 
/// <param name="data"></param> 
private static void SerializeDouble(BinaryWriter writer, int size, double[] data) 
{ 
    for (int i = 0; i < size; i++) 
    { 
     writer.Write(data[i]); 
    } 
} 
1

這裏的問題是你沒有真正表達一般性。你必須實現OutputFormattedTypeInfo對於每一個基本類型,所以你不如忘了泛型方法和簡單地使用重載你並不需要仿製藥在這裏):

public void OutputFormattedTypeInfo(BaseType theType) 
{ 
    // ... 
} 

public void OutputFormattedTypeInfo(MySuperType theType) 
{ 
    // ... 
} 

public void OutputFormattedTypeInfo(MyOtherSuperType theType) 
{ 
    // ... 
} 
+0

-1這將始終與調用基類的重載的方法,從來沒有專門的班級。 – 2011-04-15 12:22:10

+0

你錯了我的朋友。如果你通過一隻長頸鹿,並且你有長頸鹿和動物的重載,它將使用正確的過載。 – Aliostad 2011-04-15 12:23:28

+0

是的,但是如果你正在處理它們的集合,比如說IEnumerable ,那麼它將成爲動物,無論是長頸鹿還是袋熊。但是,對於具有正確派生類型的單個T,您是正確的。 – 2011-04-15 12:36:28

相關問題