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

后端 未结 3 638
难免孤独
难免孤独 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:47

    where T : class, ISessionManager
    

    you can go even further

    where T : class, ISessionManager, new()
    

    this will force non abstract class with parameterless ctor to be handed in as T

提交回复
热议问题