-2

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...

Klaus Gütter
  • 11,151
  • 6
  • 31
  • 36
  • The function & struct & class are all Public. – Izaya.Orihara Jan 04 '19 at 15:15
  • The function which call Newtonsoft.Json.JsonConvert.DeserializeObject is set [PermissionSet(SecurityAction.Assert, Unrestricted = true)]. – Izaya.Orihara Jan 04 '19 at 15:17
  • Maybe you need to add more information. I just built a minimum example of what I understood you are trying and it worked just fine. Can you post the structure of your solution? – Onkel Toob Jan 04 '19 at 15:17
  • First I use C++ ExecuteInDefaultAppDomain to execute Sandbox.Bootstrap (a C# class library) and then I call AppDomain.Load to load a assembly and call MethodBase.Invoke to trigger the new lib's function.Final I use Newtonsoft.Json.JsonConvert.DeserializeObject and get exception... – Izaya.Orihara Jan 04 '19 at 15:29
  • This seems to be very relevant information since you're dealing with a `SecurityException`. Unfortunately, I have zero expertise in this and therefore can't help from here on. – Onkel Toob Jan 04 '19 at 15:35
  • This question needs more context. Please provide a better description on what you're try to achieve and what you have tried. – Kieran Devlin Jan 04 '19 at 15:53
  • See the answer below. I change a way to code them but still get exception. – Izaya.Orihara Jan 04 '19 at 15:57
  • What I have tried is just use JSON.NET to resolve a struct like MyStruct in C# class library but failed. – Izaya.Orihara Jan 04 '19 at 15:58
  • AppDomain.Load and Invoke the specific assembly's function. The function is supposed to resolve a json file. – Izaya.Orihara Jan 04 '19 at 16:00

2 Answers2

2

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.

Kieran Devlin
  • 1,373
  • 1
  • 12
  • 28
  • I write a JSON file: { "123": { "FirstText": 1, "SecondText": 2 } } – Izaya.Orihara Jan 04 '19 at 15:50
  • My new code is: JToken value; if (JObject.Parse(Encoding.Default.GetString(Properties.Resources.MyTest)).TryGetValue("123", out value)) { System.Console.WriteLine(value.ToString()); var obj = JsonConvert.DeserializeObject(value.ToString()); System.Console.WriteLine("{0} - {1}", obj.FirstText, obj.SecondText); } – Izaya.Orihara Jan 04 '19 at 15:51
  • Output is: { "FirstText": 1, "SecondText": 2 } 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) – Izaya.Orihara Jan 04 '19 at 15:53
  • at System.Activator.CreateInstance(Type type, Boolean nonPublic) at System.Activator.CreateInstance(Type type) at Newtonsoft.Json.Utilities.LateBoundReflectionDelegateFactory.<>c__DisplayC lass9`1.b__7() at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject (JsonReader reader, JsonObjectContract objectContract, JsonProperty containerMem ber, JsonProperty containerProperty, String id, Boolean& createdFromNonDefaultCr eator) – Izaya.Orihara Jan 04 '19 at 15:54
  • 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) – Izaya.Orihara Jan 04 '19 at 15:54
  • 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) – Izaya.Orihara Jan 04 '19 at 15:54
0

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.