Skip to content

Commit f00b15e

Browse files
committed
renamed SetConstructor to SetCreator and added a test.
1 parent 6f0c8e5 commit f00b15e

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

Bson/Serialization/BsonClassMap.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,17 @@ public BsonMemberMap MapProperty(string propertyName)
816816
}
817817
return MapMember(propertyInfo);
818818
}
819+
/// <summary>
820+
/// Sets the creator for the object.
821+
/// </summary>
822+
/// <param name="creator">The creator.</param>
823+
/// <returns>The class map (so method calls can be chained).</returns>
824+
public BsonClassMap SetCreator(Func<object> creator)
825+
{
826+
this._creator = creator;
827+
return this;
819828

829+
}
820830
/// <summary>
821831
/// Sets the discriminator.
822832
/// </summary>
@@ -869,16 +879,6 @@ public void SetExtraElementsMember(BsonMemberMap memberMap)
869879
_extraElementsMemberMap = memberMap;
870880
}
871881
/// <summary>
872-
/// Sets the constructor for the object
873-
/// </summary>
874-
/// <param name="constructor"></param>
875-
public BsonClassMap SetConstructor(Func<object> constructor)
876-
{
877-
this._creator = constructor;
878-
return this;
879-
880-
}
881-
/// <summary>
882882
/// Adds a known type to the class map.
883883
/// </summary>
884884
/// <param name="type">The known type.</param>

BsonUnitTests/DefaultSerializer/BsonClassMapTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ public A()
6565
fieldMappedByAttribute2 = 10;
6666
}
6767
}
68+
69+
private class B
70+
{
71+
public int A { get; set; }
72+
73+
public B(int a)
74+
{
75+
A = a;
76+
}
77+
}
6878
#pragma warning restore
6979

7080
[Test]
@@ -73,6 +83,21 @@ public void TestMappingPicksUpAllMembersWithAttributes()
7383
var classMap = BsonClassMap.LookupClassMap(typeof(A));
7484
Assert.AreEqual(8, classMap.AllMemberMaps.Count());
7585
}
86+
87+
[Test]
88+
public void TestSetCreator()
89+
{
90+
var classMap = new BsonClassMap<B>(cm =>
91+
{
92+
cm.AutoMap();
93+
cm.SetCreator(() => new B(10));
94+
});
95+
96+
classMap.Freeze();
97+
98+
var instance = (B)classMap.CreateInstance();
99+
Assert.AreEqual(10, instance.A);
100+
}
76101
}
77102

78103
[TestFixture]

0 commit comments

Comments
 (0)