I want a generic function that would work with types that have Top
, Bottom
, Right
and Rect
read-only properties - I have
C# generics are not templates; they are provided at runtime, not by compiler magic. As such, there is no duck-typing. Two options:
where WhatType : ISomeInterface
constraint, where ISomeInterface
has a Left {get;}
dynamic
(which provides duck-typing)i.e.
internal class MyTemplate<WhatType> where WhatType : ISomeInterface {
internal static void Work( WhatType what )
{
int left = what.Left;
}
};
interface ISomeInterface {
int Left { get; }
}
or:
internal class MyTemplate<WhatType> {
internal static void Work( WhatType what )
{
int left = ((dynamic)what).Left;
}
};