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

后端 未结 7 822
夕颜
夕颜 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:38

    You have to specify the base class that supports .Left in the where clause or you have to cast what to a type that supports the .Left-property:

    internal class MyTemplate where WhatType : YourBaseClassWithLeftProperty
    

    or

    internal class MyTemplate {
      internal static void Work( WhatType what )    {        
        YourBaseClassWithLeftProperty yourInstance=what as YourBaseClassWithLeftProperty;
        if(null != yourInstance){
             int left = yourInstance.Left;    
        }
    
      }
      ...
    

提交回复
热议问题