Skip to content

Commit

Permalink
Add support for Unity types
Browse files Browse the repository at this point in the history
  • Loading branch information
doctorpangloss committed Feb 12, 2016
1 parent d3388ac commit ae07b9f
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions JsonFx/TypeCoercionUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
\*---------------------------------------------------------------------------------*/

#endregion License

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
Expand Down Expand Up @@ -127,23 +129,33 @@ internal object ProcessTypeHint(

internal Object InstantiateObject(Type objectType, out Dictionary<string, MemberInfo> memberMap)
{
if (objectType.IsInterface || objectType.IsAbstract || objectType.IsValueType)
if (objectType.IsInterface || objectType.IsAbstract)
{
throw new JsonTypeCoercionException(
String.Format(TypeCoercionUtility.ErrorCannotInstantiate, objectType.FullName));
}

ConstructorInfo ctor = objectType.GetConstructor(Type.EmptyTypes);
Object result = null;

if (ctor == null)
{
throw new JsonTypeCoercionException(
String.Format(TypeCoercionUtility.ErrorDefaultCtor, objectType.FullName));
if (objectType.IsValueType) {
try {
result = FormatterServices.GetUninitializedObject(objectType);
} catch {
throw new JsonTypeCoercionException(
String.Format(TypeCoercionUtility.ErrorCannotInstantiate, objectType.FullName));
}
} else {
throw new JsonTypeCoercionException(
String.Format(TypeCoercionUtility.ErrorDefaultCtor, objectType.FullName));
}
}
Object result;
try
{
// always try-catch Invoke() to expose real exception
result = ctor.Invoke(null);
result = result ?? ctor.Invoke(null);
}
catch (TargetInvocationException ex)
{
Expand Down

0 comments on commit ae07b9f

Please sign in to comment.