你好,我目前遇到以下问题。我想将一个类型作为参数传递给控制器,并使用它来反序列化xml字符串。我试过的东西看起来像这个
var typeAsString = typeof(List<ProjectReportDto>).GetType().FullName;
//... pass value to controller
// in the method I got the string
var type = Type.GetType(typeAsString );
var mySerializer = new XmlSerializer(type);
// I got a Exceptrion -> System.RuntimeType is inaccessible due to its protection level. Only public types can be processed.
类别项目报告D到
[Serializable]
public class ProjectReportDto
{
public string ShortName { get; set; }
public string LongName { get; set; }
public string Description { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
public string CustomerName { get; set; }
}
Xml字符串
<?xml version="1.0" encoding="UTF-16"?>
<ArrayOfProjectReportDto xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ProjectReportDto>
<ShortName>Piper .NET</ShortName>
<LongName>The new Internet</LongName>
<Description>awsome</Description>
<StartDate>2022-08-19T00:00:00</StartDate>
<EndDate>2023-02-25T00:00:00</EndDate>
</ProjectReportDto>
<ProjectReportDto>
<ShortName>Nucleus</ShortName>
<LongName>Nucleus Portal </LongName>
<Description>Make the world a better place </Description>
<StartDate>2022-08-19T00:00:00</StartDate>
<EndDate>2026-09-26T00:00:00</EndDate>
</ProjectReportDto>
</ArrayOfProjectReportDto
我已经搜索了这个问题,但没有找到对我有帮助的东西。也许用这种方式读取xml是不可能的?
您调用variable.GetType()
来获取变量的类型,但对类型本身调用GetType()
将返回"System.Runtime;它是不可访问的(不是公共的(。
更换
var typeAsString = typeof(List<ProjectReportDto>).GetType().FullName;
带
var typeAsString = typeof(List<ProjectReportDto>).FullName;