Crash in C# Class Library when i use it

262 Views Asked by At

Source/destination types

public struct MyStruct
{
    public int FirstText { get; set; }
    public int SecondText { get; set; }
}

Source/destination JSON

{FirstText:1,SecondText:2}

Expected behavior

When I use it in a single exe demo: 1 - 2

Actual behavior

When I use it in a C# Class Library, it crashes:

System.Reflection.TargetInvocationException: Exception has been thrown by the ta
rget of an invocation. ---> System.Security.SecurityException: Request failed.
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOn
ly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Bo
olean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipChec
kThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean s
kipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   at Newtonsoft.Json.Utilities.LateBoundReflectionDelegateFactory.<>c__DisplayC
lass9`1.<CreateDefaultConstructor>b__7()
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject
(JsonReader reader, JsonObjectContract objectContract, JsonProperty containerMem
ber, JsonProperty containerProperty, String id, Boolean& createdFromNonDefaultCr
eator)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(Js
onReader reader, Type objectType, JsonContract contract, JsonProperty member, Js
onContainerContract containerContract, JsonProperty containerMember, Object exis
tingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInte
rnal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty mem
ber, JsonContainerContract containerContract, JsonProperty containerMember, Obje
ct existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(Jso
nReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type
 objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, Jso
nSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSeriali
zerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)

Steps to reproduce

var test = Newtonsoft.Json.JsonConvert.DeserializeObject<MyStruct>("{FirstText:1,SecondText:2}");

System.Console.WriteLine("{0} - {1}", test.FirstText, test.SecondText);

When I use Json.NET in a single exe demo, it works fine. But when I take it in a C# Class Library, it does not work. I push a issue on github but I still ask there because I am too nervous to wait...

2

There are 2 best solutions below

6
Kieran Devlin On

It would appear that the JSON you are providing is not valid. Keys must be wrapped in quotes. Try this: { "FirstText": 1, "SecondText": 2 }

Edit: If you're planning on hardcoding the JSON inside the source then, make sure to escape it so it can be interpreted correctly.

0
Izaya.Orihara On

Sandboxed AppDomain required full trust with references and it can be given by two way: 1. Registed assembly in GAC. 2. Set assembly is fullTrustAssemblies with StrongName when use AppDomain.CreateDomain.