Casting generic type “as T” whilst enforcing the type of T

后端 未结 3 691
难免孤独
难免孤独 2021-02-12 12:17

I\'m missing a trick here I think and can\'t believe I\'ve never done this before. However, how can I cast a generic type using the as keyword?

[Serializable]
pu         


        
3条回答
  •  长情又很酷
    2021-02-12 12:55

    Read up on Constraints on Type Parameters in C#.

    In this particular case, you must ensure that T is a class:

    public abstract class SessionManager
        where T : class, ISessionManager
    

提交回复
热议问题