C# generics problem - newing up the generic type with parameters in the constructor

前端 未结 7 703
闹比i
闹比i 2020-12-09 04:02

I am trying to create a generic class which new\'s up an instance of the generic type. As follows:

public class HomepageCarousel : List
            


        
7条回答
  •  伪装坚强ぢ
    2020-12-09 04:39

    It's a C# and CLR handicap, you cannot pass an argument to new T(), simple.

    If you're coming from a C++ background this used be NOT-broken and TRIVIAL. PLUS you don't even require an interface/constraint. Breakage all over the place, and without that functional factory 3.0 hack you are forced to do 2-pass initialisation. Managed Blasphemy!

    Do the new T() first and then set the property or pass an exotic initialiser syntax or as all well suggested use the Pony's functional workaround.. All yucky but that's the compiler and runtime idea of 'generics' for you.

提交回复
热议问题