Generics: What's a “CONSTRUCTOR constraint”?

后端 未结 2 1830
轮回少年
轮回少年 2021-01-04 23:27

I made a custom TObjectList descendant designed to hold subclasses of a base object class. It looks something like this:

interface
   TMyDataList

        
2条回答
  •  清歌不尽
    2021-01-05 00:01

    You're trying to create an instance of T via T.Create. This doesn't work because the compiler doesn't know that your generic type has a parameterless constructor (remember: this is no requirement). To rectify this, you've got to create a constructor constraint, which looks like this:

    
    

    or, in your specific case:

    
    

提交回复
热议问题