Open Generic as a type parameter #6290
Unanswered
TonyValenti
asked this question in
Q&A
Replies: 2 comments 1 reply
-
Effectively, what you're asking for is covariance. As you probably know, you can use covariance if you change public interface IParent<out TChild> where TChild : ChildBase {
TChild Child { get; }
} Then your condition would be: object Input = SomeMethod();
if (Input is IParent<ChildBase> V1) {
V1.Child.Date = DateTimeOffset.UtcNow;
} If you can't change |
Beta Was this translation helpful? Give feedback.
0 replies
-
Actually I think what's closer to what the OP wants is existential types: Given public interface IParent
{
type TChild : ChildBase;
TChild Child { get; }
} You could then perform a regular type check (because the generic type doesn't form part of the parent type directly) and the following simply becomes legal: if (SomeMethod() is IParent v1)
{
v1.Child.Date = DateTimeOffset.UtcNow;
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Let's say I have the following code:
Given the above,
Parent.Child.Date
is always valid to get or set.How would I write the following code?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions