开发者

Type passing to a generic method

I'm trying to do something pretty sp开发者_运维百科ecific

public class EnableComponent <T> : ScriptableEvent
{
    public EnableComponent (GameObject body, bool state = true)
    {
        body.GetComponent<T>().enabled = state;
    }
}

Now this doesn't work, gives something like "The type 'T' cannot be used as type parameter 'T' in the generic type or method."

I tried to do a bit of searching, and the best I found was a short discussion on why this doesn't work in java.

I've tried implementing the same thing by passing a Type parameter but couldn't quite figure it out. I'm pretty sure this would be the way to go, just not exactly sure how to get it to work.

Just hoping someone has a suggestion on a way to implement the above logic.

Thanks in advance.


Look at the Constraints on Type Parameter

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜