2014-09-26 84 views
1

我不知道該怎麼解釋這一點,我想下面的方法來工作,但它給了我錯誤說泛型幫助需要

as' operator cannot be used with a non-reference type parameter T」。考慮添加`class'或引用類型約束。

任何人都可以幫我解決這個問題嗎?

public T GetRoot<T>() { 
    if (this is T) 
     return this as T; 
    if (parent != null) 
     return parent.GetRoot<T>(); 
    return null; 
} 

回答

3
public T GetRoot<T>() where T:Class 
{ 
} 
0

我想你發現問題是要傳遞的類型。 作出這樣的類型可能爲空的,然後再試一次

T?