源/目的地类型
public struct MyStruct
{
public int FirstText { get; set; }
public int SecondText { get; set; }
}
源/目标JSON
{FirstText:1,SecondText:2}
预期行为
当我在单个exe演示中使用它时: 1-2
实际行为
当我在C#类库中使用它时,它崩溃:
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)
复制步骤
var test = Newtonsoft.Json.JsonConvert.DeserializeObject<MyStruct>("{FirstText:1,SecondText:2}");
System.Console.WriteLine("{0} - {1}", test.FirstText, test.SecondText);
当我在单个exe演示中使用Json.NET时,它工作正常。 但是,当我将其放在C#类库中时,它不起作用。 我在github上发布了一个问题,但我仍然在那儿问,因为我太紧张了,无法等待...
答案 0 :(得分:2)
您提供的JSON似乎无效。
密钥必须用引号引起来。尝试这个:
{ "FirstText": 1, "SecondText": 2 }
编辑:如果您打算对源内部的JSON进行硬编码,请确保对其进行转义,以便可以正确解释。
答案 1 :(得分:0)
带沙盒的AppDomain需要对引用的完全信任,可以通过两种方式给予信任: 1.在GAC中进行注册的组装。 2.使用AppDomain.CreateDomain时,将程序集设置为带有StrongName的fullTrustAssemblies。