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

前端 未结 3 832
伪装坚强ぢ
伪装坚强ぢ 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 20:54

    You want this:

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

    Problem is you have to use the actual type expression in generics, not a string. You could construct the type using reflection if you really need to use the string.

提交回复
热议问题