[CSHARP-4003] Support overriden properties with covariant return type Created: 30/Dec/21  Updated: 31/Mar/22

Status: Backlog
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Unknown
Reporter: Gur Galler Assignee: Robert Stam
Resolution: Unresolved Votes: 1
Labels: net5
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

C# 9 introduced return type covariance, and that allows us to override properties with more specific types. MongoDB's driver can't serialize such properties at the moment.

Example:

{}

public class Entity
{
    public virtual object SomeProperty { get; set; }
}

{{And:}}
{}

public class User : Entity
{
    public override string SomeProperty { get; set; }
}

{{The Mongo BSON serializer thinks these are two separate properties, and it throws an exception because there are two properties that map to the same element.}}

 

(BTW, I'm new here so if I did something wrong with this feature request please let me know){}

 



 Comments   
Comment by Robert Stam [ 10/Jan/22 ]

Thanks again for submitting this ticket.

We fully intend to support this new C# feature but it will be in a future release.

I'm transitioning the status of this ticket from Investigating to Open.

 

Comment by Gur Galler [ 07/Jan/22 ]

Oh, yes. Sorry about that.

Obviously covariance is not possible for setters (we don't want to allow any `object` to enter the setter that expects a `string`).

 

This is relevant only for read-only properties. In the context of the MongoDB driver, these will probably be initialized using a constructor parameter with a matching name

Comment by Robert Stam [ 07/Jan/22 ]

Thanks for the information that you are using .NET 5.0. I will use that target framework to investigate further.

Now I'm getting a compiler error on:

public override string SomeProperty { get; set; }

Apparently covariant return types are only allowed on read-only properties.

Can you confirm that you are actually using read-only properties?

 

Comment by Gur Galler [ 07/Jan/22 ]

Because this is a reflection-based feature, there should be no problem implementing it in a way that also supports older runtimes. Just don't assume the types are equal when dealing with a an override

Comment by Gur Galler [ 07/Jan/22 ]

I tested it with .NET 5.0 and it worked. I assume this is the first version to support it, as it was released together with C# 9.0

Comment by Robert Stam [ 07/Jan/22 ]

When I attempt to reproduce this I get a compiler error:

Target runtime doesn't support covariant types in overrides.

So even though C# 9 supports covariant types in overrides, apparently the target frameworks we currently compile and test against don't.

I haven't been able to find out what target frameworks support covariant types in overrides.

Comment by Robert Stam [ 04/Jan/22 ]

Thank you for reporting this.

We will investigate how we might be able to support this in a future release.

Generated at Wed Feb 07 21:46:55 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.