Why I cannot use Type.GetType() in generics?

前端 未结 3 838
伪装坚强ぢ
伪装坚强ぢ 2021-01-15 20:49

I am trying to get rid of some references by using string representation of certain types. But compiler is not letting me do it the way I want in case of generic methods. I

3条回答
  •  悲哀的现实
    2021-01-15 21:06

    You can't use a variable in the place of the generic type argument (between < and >). It has to be a type determined at compile time.

    But in your example this is already the case. Can't you just use

    foreach (var component in container.Components.OfType()) {
    ...
    }
    

提交回复
热议问题