-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CSHARP-476: Added support for calling constructors, factory methods o…
…r arbitrary code to instantiate an object during deserialization.
- Loading branch information
rstam
committed
Jan 16, 2013
1 parent
a5d7325
commit e0f958e
Showing
19 changed files
with
2,244 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
MongoDB.Bson/Serialization/Attributes/BsonConstructorAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* Copyright 2010-2013 10gen Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using System; | ||
|
||
namespace MongoDB.Bson.Serialization.Attributes | ||
{ | ||
/// <summary> | ||
/// Specifies that this constructor should be used for creator-based deserialization. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Constructor)] | ||
public class BsonConstructorAttribute : Attribute, IBsonCreatorMapAttribute | ||
{ | ||
// private fields | ||
private string[] _argumentNames; | ||
|
||
// constructors | ||
/// <summary> | ||
/// Initializes a new instance of the BsonConstructorAttribute class. | ||
/// </summary> | ||
public BsonConstructorAttribute() | ||
{ } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the BsonConstructorAttribute class. | ||
/// </summary> | ||
/// <param name="argumentNames">The names of the members that the creator argument values come from.</param> | ||
public BsonConstructorAttribute(params string[] argumentNames) | ||
{ | ||
_argumentNames = argumentNames; | ||
} | ||
|
||
// public properties | ||
/// <summary> | ||
/// Gets the names of the members that the creator arguments values come from. | ||
/// </summary> | ||
public string[] ArgumentNames | ||
{ | ||
get { return _argumentNames; } | ||
} | ||
|
||
// public methods | ||
/// <summary> | ||
/// Applies a modification to the creator map. | ||
/// </summary> | ||
/// <param name="creatorMap">The creator map.</param> | ||
public void Apply(BsonCreatorMap creatorMap) | ||
{ | ||
if (_argumentNames != null) | ||
{ | ||
creatorMap.SetArguments(_argumentNames); | ||
} | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
MongoDB.Bson/Serialization/Attributes/BsonFactoryMethodAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* Copyright 2010-2013 10gen Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using System; | ||
|
||
namespace MongoDB.Bson.Serialization.Attributes | ||
{ | ||
/// <summary> | ||
/// Specifies that this factory method should be used for creator-based deserialization. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Method)] | ||
public class BsonFactoryMethodAttribute : Attribute, IBsonCreatorMapAttribute | ||
{ | ||
// private fields | ||
private string[] _argumentNames; | ||
|
||
// constructors | ||
/// <summary> | ||
/// Initializes a new instance of the BsonFactoryMethodAttribute class. | ||
/// </summary> | ||
public BsonFactoryMethodAttribute() | ||
{ } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the BsonFactoryMethodAttribute class. | ||
/// </summary> | ||
/// <param name="argumentNames">The names of the members that the creator argument values come from.</param> | ||
public BsonFactoryMethodAttribute(params string[] argumentNames) | ||
{ | ||
_argumentNames = argumentNames; | ||
} | ||
|
||
// public properties | ||
/// <summary> | ||
/// Gets the names of the members that the creator arguments values come from. | ||
/// </summary> | ||
public string[] ArgumentNames | ||
{ | ||
get { return _argumentNames; } | ||
} | ||
|
||
// public methods | ||
/// <summary> | ||
/// Applies a modification to the creator map. | ||
/// </summary> | ||
/// <param name="creatorMap">The creator map.</param> | ||
public void Apply(BsonCreatorMap creatorMap) | ||
{ | ||
if (_argumentNames != null) | ||
{ | ||
creatorMap.SetArguments(_argumentNames); | ||
} | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
MongoDB.Bson/Serialization/Attributes/IBsonCreatorMapAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* Copyright 2010-2013 10gen Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace MongoDB.Bson.Serialization | ||
{ | ||
/// <summary> | ||
/// Represents an attribute used to modify a creator map. | ||
/// </summary> | ||
public interface IBsonCreatorMapAttribute | ||
{ | ||
/// <summary> | ||
/// Applies the attribute to the creator map. | ||
/// </summary> | ||
/// <param name="creatorMap">The creator map.</param> | ||
void Apply(BsonCreatorMap creatorMap); | ||
} | ||
} |
Oops, something went wrong.