Property pattern matching in where clause for generics #8090
Answered
by
KennethHoff
klirium
asked this question in
Language Ideas
-
It's more like TypeScript's shape model, but Is it too much doing this? internal class Point
{
public int X { get; set; }
public int Y { get; set; }
}
public static void Main()
{
var o = new Point() { X = 4, Y = 5 };
Gen(o);
}
// What if we does not have access to Point class and cannot implement IPoint interface on it.
// Can we check a shape of a type when passing it to a generic function?
public static void Gen<T>(T point) where T : is { X: int, Y: int}
{
Console.WriteLine(point.X);
} |
Beta Was this translation helpful? Give feedback.
Answered by
KennethHoff
May 1, 2024
Replies: 2 comments 2 replies
-
This sounds like it could be solved via #5497, by defining your own interface and implementing that interface on behalf of whatever you want |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
klirium
-
Things like this maybe need reflection, just like public static string Test<T>(T value) where T : { string Test(); }
{
return value.Test();
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This sounds like it could be solved via #5497, by defining your own interface and implementing that interface on behalf of whatever you want