Trivial C# class with a generic parameter wouldn't compile for no apparent reason

后端 未结 7 870
夕颜
夕颜 2021-01-16 15:15

I want a generic function that would work with types that have Top, Bottom, Right and Rect read-only properties - I have

7条回答
  •  深忆病人
    2021-01-16 15:23

    You haven't specified any conditions for the WhatType type, so it will accept any type, and only know what every type knows, i.e. only what's defined in the Object class.

    If you want to use anything else in the type, you have to specify what it contains, which you do by specifying that it has to implement an interface or inherit from a class:

    internal class MyTemplate where WhatType : BaseType {
    

    Where BaseType would either be an interface or a class where the Left property is defined.

提交回复
热议问题