C# Json未反序列化列表<;T>;从LabView程序调用时
长话短说,我有一个dll,它的主要工作是从LabView程序中获取发送到它的数据,然后用这些数据处理一些后端工作 一旦数据被发送到dll(通过调用具有多个参数的.NET对象的方法-见下文)并正确解析为.NET对象,如果出现任何“坏”情况(连接到db,任何其他无法持久化数据),我将获取数据并以json文件的形式将其序列化到计算机上的本地文件夹。我正在使用C# Json未反序列化列表<;T>;从LabView程序调用时,c#,dll,json.net,labview,C#,Dll,Json.net,Labview,长话短说,我有一个dll,它的主要工作是从LabView程序中获取发送到它的数据,然后用这些数据处理一些后端工作 一旦数据被发送到dll(通过调用具有多个参数的.NET对象的方法-见下文)并正确解析为.NET对象,如果出现任何“坏”情况(连接到db,任何其他无法持久化数据),我将获取数据并以json文件的形式将其序列化到计算机上的本地文件夹。我正在使用Newtonsoft.Json 6.0.8作为本项目的Json方面 public class MainClass { public voi
Newtonsoft.Json 6.0.8
作为本项目的Json方面
public class MainClass
{
public void saveData(string[] someNames, string[] someValues)
{
// here I parse the data passed in, and create the .NET object I work with try
{
// here I write data to the database, do whatever else I need to do
}
catch (System.Exception ex)
{
// if ANYTHING goes wrong with the persist of data, write json to local file
}
}
}
以下是我序列化数据的方式:
private void writeParentToJson(ParentClass parent)
{
using (var printer = new StreamWriter(filePath, false))
{
var izer = new JsonSerializer();
izer.TypeNameHandling = TypeNameHandling.All;
izer.Serialize(printer, parent);
}
}
[JsonObject]
public class ParentClass
{
[JsonProperty("childThings")]
public List<ChildClass> listOfChildClass { get; set; }
public ParentClass()
{
}
public ParentClass(List<ChildClass> listOfChildClass)
{
this.listOfChildClass = listOfChildClass;
}
}
[JsonObject]
public class ChildClass
{
[JsonProperty("childName")]
public string name { get; set; }
[JsonProperty("childValue")]
public string value { get; set; }
public ChildClass()
{
}
public ChildClass(string name, string value)
{
this.name = name;
this.value = value;
}
}
private void writeParentToJson(父类父类)
{
使用(var printer=newstreamwriter(filePath,false))
{
var izer=新的JsonSerializer();
iter.typenameholling=typenameholling.All;
序列化(打印机、父级);
}
}
[JsonObject]
公共类父类
{
[JsonProperty(“儿童用品”)]
儿童类{get;set;}的公共列表列表列表
公共父类()
{
}
公共父类(儿童类列表)
{
this.listOfChildClass=listOfChildClass;
}
}
[JsonObject]
公营儿童班
{
[JsonProperty(“childName”)]
公共字符串名称{get;set;}
[JsonProperty(“childValue”)]
公共字符串值{get;set;}
公共儿童班()
{
}
公共子类(字符串名称、字符串值)
{
this.name=名称;
这个值=值;
}
}
发生这种情况时,所有内容都可以很好地写入json文件。当我再次读取数据时,我是这样做的:
public ParentClass loadParentFromJson()
{
using (var reader = new JsonTextReader(new StreamReader(filePath)))
{
var izer = new JsonSerializer();
izer.TypeNameHandling = TypeNameHandling.All;
return izer.Deserialize<ParentClass>(reader);
}
}
公共父类loadParentFromJson()
{
使用(var reader=newjsontextreader(newstreamreader(filePath)))
{
var izer=新的JsonSerializer();
iter.typenameholling=typenameholling.All;
返回反序列化(读取器);
}
}
当我创建.NET项目并使用此dll时。。。一切工作都完美无缺。问题是,使用LabView的人没有这么幸运。当dll将数据写入文件时,一切工作正常,但读回数据是我们遇到问题的地方
更具体地说,会发生以下情况:内部异常:Newtonsoft.Json.JsonSerializationException:在程序集“mscorlib,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089”中找不到类型“System.Collections.Generic.List`1[[MyDll.ChildClass,MyDll]]”
我对LabView一无所知,使用LabView的人也对.NET一无所知。如有任何见解,将不胜感激
LabView程序只调用该方法,dll执行json的所有读/写操作。。这就是为什么我不知道它对我来说是如何工作的,但当从LabView程序调用时就不知道了
所有计算机都安装了.NET 4.0 framework。。我见过其他人有这个例外,但它只有在从LabView程序调用时才会失败。。我不知道为什么。每次在LabVIEW上使用新安装时,我都会用到一些应用程序注释: 这些描述了如何设置LabVIEW.exe.config文件以允许使用第三部分.Net程序集(以及不同的.Net版本),最终我得到了一个LabVIEW.exe.config,如下所示:
<?xml version ="1.0" encoding="utf-8" ?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0.30319"/>
</startup>
<runtime>
<loadFromRemoteSources enabled="true" />
</runtime>
</configuration>
看起来LabView没有使用正确的运行时。我没有想到这一点,但是LabView的家伙刚刚检查过,他正在运行2013,默认为.NET 4.0,并且项目中没有任何配置文件可以更改默认值。我将尝试将加载方法更改为使用dynamic
,并查看该方法是否正确解析。可能相关: