2014-09-26 81 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?