Skip to content

Instantly share code, notes, and snippets.

@applejag
Last active July 16, 2020 11:56
Show Gist options
  • Save applejag/73d300529a6515dbd5c50db5530fba57 to your computer and use it in GitHub Desktop.
Save applejag/73d300529a6515dbd5c50db5530fba57 to your computer and use it in GitHub Desktop.

This gist is connected to the #63 issue from jilleJr/Newtonsoft.Json-for-Unity

In Unity, setting the Scripting Backend to IL2CPP in the project settings and then running all tests in a built player via the "Run all in player (StandaloneWindows)" button resulted in the following output:

TestEnsuredWithAttr Failed:Error
System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
  ----> System.TypeInitializationException : The type initializer for 'System.Collections.Generic.List<Tests.Issue63.EnsuredWithAttr>' threw an exception.
  ----> System.ExecutionEngineException : Attempting to call method 'System.Collections.Generic.List`1[[Tests.Issue63+EnsuredWithAttr, Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]::.cctor' for which no ahead of time (AOT) code was generated.

TestEnsuredWithPreservedAttr Failed:Error
System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
  ----> System.TypeInitializationException : The type initializer for 'System.Collections.Generic.List<Tests.Issue63.EnsuredWithPreservedAttr>' threw an exception.
  ----> System.ExecutionEngineException : Attempting to call method 'System.Collections.Generic.List`1[[Tests.Issue63+EnsuredWithPreservedAttr, Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]::.cctor' for which no ahead of time (AOT) code was generated.

The other two tests, "TestEnsuredWithAotHelper" and "TestUntouchedByAotHelper", are not shown because they passed.

using System;
using System.Collections.Generic;
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Utilities;
using NUnit.Framework;
using UnityEngine.Scripting;
namespace Tests
{
public class Issue63
{
private const string JSON = "[{}, {}, {}]";
[OneTimeSetUp]
public static void Setup()
{
AotHelper.EnsureList<EnsuredWithAotHelper>();
}
[Test]
public void TestEnsuredWithAotHelper()
{
// Act
var result = JsonConvert.DeserializeObject<IList<EnsuredWithAotHelper>>(JSON);
// Assert
Assert.IsNotNull(result, "result");
Assert.AreEqual(3, result.Count, "result.Count");
CollectionAssert.AllItemsAreInstancesOfType(result, typeof(EnsuredWithAotHelper));
}
[Test]
public void TestEnsuredWithAttr()
{
// Act
var result = JsonConvert.DeserializeObject<IList<EnsuredWithAttr>>(JSON);
// Assert
Assert.IsNotNull(result, "result");
Assert.AreEqual(3, result.Count, "result.Count");
CollectionAssert.AllItemsAreInstancesOfType(result, typeof(EnsuredWithAttr));
}
[Test]
public void TestEnsuredWithPreservedAttr()
{
// Act
var result = JsonConvert.DeserializeObject<IList<EnsuredWithPreservedAttr>>(JSON);
// Assert
Assert.IsNotNull(result, "result");
Assert.AreEqual(3, result.Count, "result.Count");
CollectionAssert.AllItemsAreInstancesOfType(result, typeof(EnsuredWithPreservedAttr));
}
[Test]
public void TestUntouchedByAotHelper()
{
// Act
Assert.Throws<TargetInvocationException>(() =>
{
JsonConvert.DeserializeObject<IList<UntouchedByAotHelper>>(JSON);
});
}
public struct EnsuredWithAotHelper
{
public decimal MyBProp { get; set; }
}
[AotHelp(typeof(AotEnsure<EnsuredWithAttr>))]
public struct EnsuredWithAttr
{
public string MyAProp { get; set; }
}
[AotHelp(typeof(PreservedAotEnsure<EnsuredWithAttr>))]
public struct EnsuredWithPreservedAttr
{
public string MyAProp { get; set; }
}
public struct UntouchedByAotHelper
{
public bool MyProperty { get; set; }
public float myField;
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface | AttributeTargets.Enum, AllowMultiple = false)]
public class AotHelpAttribute : Attribute
{
public AotHelpAttribute(Type ensureType)
{
}
}
public class AotEnsure<T> where T : new()
{
public AotEnsure()
{
AotHelper.EnsureType<T>();
}
}
[Preserve]
public class PreservedAotEnsure<T> where T : new()
{
public PreservedAotEnsure()
{
AotHelper.EnsureType<T>();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment