How can a dynamic be used as a generic?

前端 未结 6 458
南旧
南旧 2021-01-02 06:08

How can I use a dynamic as a generic?

This

var x = something not strongly typed;
callFunction();

and this

         


        
6条回答
  •  独厮守ぢ
    2021-01-02 07:06

    You can't. The whole point of generics is compile-time safety which means that they must be known at compile-time. And the whole point of dynamics is that you don't need to know the exact type at compile time and use runtime dispatching => it's the absolutely exact opposite of generics. So don't waste your time => once you get the dynamic/reflection path you can forget about generics and compile-time safety. You will have to walk that path till the end.

    So to answer your question:

    What can I do to x to make it legitimate enough to be used in ?

    The only thing you could do is to use a type that is known at compile-time, otherwise you cannot use generics.

提交回复
热议问题