Skip to content
This repository has been archived by the owner on Dec 30, 2020. It is now read-only.

Commit

Permalink
Add support for HashSets.
Browse files Browse the repository at this point in the history
  • Loading branch information
lanwin committed May 15, 2010
1 parent 38cc8ec commit 901e83d
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions source/MongoDB/Serialization/Builders/ArrayBuilder.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace MongoDB.Serialization.Builders
{
Expand Down Expand Up @@ -35,15 +36,25 @@ public void AddProperty(string name, object value)
public object BuildObject()
{
if(IsDocumentArray)
{
var type = GetResultListType();
return GetTypedList();

return type == typeof(object) ? _elements : CreateTypedList(type);
}
if(_elementType.IsGenericType)
return Activator.CreateInstance(_elementType, GetTypedList());

return _elements.ToArray();
}

/// <summary>
/// Gets the typed list.
/// </summary>
/// <returns></returns>
private object GetTypedList()
{
var type = GetResultListType();

return type == typeof(object) ? _elements : CreateTypedList(type);
}

/// <summary>
/// Gets the type of the property.
/// </summary>
Expand All @@ -54,6 +65,12 @@ public Type GetPropertyType(string name)
return _elementType;
}

/// <summary>
/// Gets a value indicating whether this instance is document array.
/// </summary>
/// <value>
/// <c>true</c> if this instance is document array; otherwise, <c>false</c>.
/// </value>
private bool IsDocumentArray{
get{ return _elementType == null;}
}
Expand All @@ -70,11 +87,10 @@ private Type GetResultListType()

Type commonType = null;

foreach(var obj in _elements)
foreach(var objType in from obj in _elements
where obj != null
select obj.GetType())
{
if(obj == null)
continue;
var objType = obj.GetType();
if(commonType == null)
commonType = objType;
else if(commonType != objType)
Expand Down

0 comments on commit 901e83d

Please sign in to comment.