[CSHARP-2405] ImmutableTypeClassMapConvention should not consider static properties Created: 03/Oct/18  Updated: 28/Oct/23  Resolved: 06/Nov/19

Status: Closed
Project: C# Driver
Component/s: Serialization
Affects Version/s: 2.7.0
Fix Version/s: 2.9.3

Type: Bug Priority: Major - P3
Reporter: Sergey Rodyushkin Assignee: Robert Stam
Resolution: Fixed Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible

 Description   

Consider the following immutable class:

 

public class C 
{
    public C(int x)
    { 
        X = x; 
    }    
 
    public static C Default { get; } = new C(0);
 
    public int X { get; }
}

 

The ImmutableTypeClassMapConvention refuses to map such a class, because when obtaining the properties of a type, it calls the GetProperties method with no parameters. According to the MSDN documentation, both instance and static properties of a type are returned in this case. 

The suggested fix is to consider instance properties only by setting the correct BindingFlags.

 



 Comments   
Comment by Githook User [ 06/Nov/19 ]

Author:

{'username': 'rbugginsvia', 'email': '31471952+rbugginsvia@users.noreply.github.com', 'name': 'rbugginsvia'}

Message: CSHARP-2405: ImmutableTypeClassMapConvention should not consider static properties
Branch: v2.9.x
https://github.com/mongodb/mongo-csharp-driver/commit/58afad7003ca77484400e1309e46d0266937e24e

Comment by Githook User [ 06/Nov/19 ]

Author:

{'name': 'rbugginsvia', 'username': 'rbugginsvia', 'email': '31471952+rbugginsvia@users.noreply.github.com'}

Message: CSHARP-2405: ImmutableTypeClassMapConvention should not consider static properties
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/da9c1d2e23fbc3b07f6bcde39a22e8d56ff1b18a

Comment by Ross Buggins [ 22/Oct/18 ]

I'm using this in F# and i am having the same issue with a type that has static members. As soon as these are removed then this works. 

 

I've edited line 42 to be:

var properties = typeInfo.GetProperties(BindingFlags.Instance | BindingFlags.Public );

and this now solves this problem.

See PR https://github.com/mongodb/mongo-csharp-driver/pull/350

 

 

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