我錯過了一個技巧,我認爲我不能相信我從來沒有這樣做過。但是,如何使用as關鍵字來施放泛型類型?鑄造泛型類型「爲T」,同時強制執行類型T
[Serializable]
public abstract class SessionManager<T> where T : ISessionManager
{
protected SessionManager() { }
public static T GetInstance(HttpSessionState session)
{
// Ensure there is a session Id
if (UniqueId == null)
{
UniqueId = Guid.NewGuid().ToString();
}
// Get the object from session
T manager = session[UniqueId] as T;
if (manager == null)
{
manager = Activator.CreateInstance<T>();
session[UniqueId] = manager;
}
return manager;
}
protected static string UniqueId = null;
}
線T manager = session[UniqueId] as T;
引發以下錯誤:
The type parameter 'T' cannot be used with the 'as' operator because it does not have a class type constraint nor a 'class' constraint
現在,我想知道這樣做的原因;我沒有實際告訴編譯器T是一個類。如果我更換:
public abstract class SessionManager<T> where T : ISessionManager
與
public abstract class SessionManager<T> where T : class
...然後代碼成功生成。
但是我的問題是這樣的;我如何在泛型類型上同時實現類和ISessionManager強制?我希望有一個非常簡單的答案。
編輯: 我想補充我曾嘗試:where T : ISessionManager, class
,原來我已經無法正確讀取我的編譯器錯誤。簡單地說,只需在ISessionManager
之前安排課程即可解決問題。我沒有看到的錯誤是:
"The 'class' or 'struct' constraint must come before any other constraints".
沉默寡言的時刻結束。
順便說一句,你不應該使用'的CreateInstance'這裏。你應該添加一個'new()'約束,並簡單地在代碼中使用'new T()'。 –
2009-11-12 12:24:09
永遠不會知道!謝謝。 – GenericTypeTea 2009-11-12 12:34:19