好吧,我已经用谷歌搜索了近两天,并且尝试了几乎所有与此错误相关的解决方案,但没有任何效果。大多数问题都是关于 Click-once 应用程序、JSON、Web 应用程序等的安全设置。但对于普通的旧 winforms 应用程序来说却没有任何问题。
这是完整的错误
System.TypeAccessException:尝试通过方法“Microsoft.Xml.Serialization.GenerateAssembly.XmlSerializationWriterSystemSetup.Write3_SystemSetup(System.Object)”访问类型“DataFacture.Common.Globals+SystemSetup”失败。
在 Microsoft.Xml.Serialization.GenerateAssembly.XmlSerializationWriterSystemSetup.Write3_SystemSetup(对象 o)
这是“SystemSetup”类的简化版本
public class SystemSetup
{
private string machineId, windowsVersion;
public SystemSetup() { }
public SystemSetup(string machineId, string windowsVersion)
{
this.machineId = machineId;
this.windowsVersion = windowsVersion
}
public string MachineID { get { return machineId; } set { machineId = value; } }
public string WindowsVersion{ get { return windowsVersion; } set { windowsVersion= value; } }
}
现在,我尝试使用以下代码在“writer.Serialize(wfile, objectSerializer);”处生成 SystemSetup 对象的 XML行,出现错误
public static void WriteXML(Object objectSerializer, String XMLPath, String FileName)
{
try
{
if (XMLPath.Substring(XMLPath.Length - 1, 1) != @"/")
XMLPath = String.Format("{0}\\", XMLPath);
XmlSerializer writer = null;
Type objectType = objectSerializer.GetType();
switch (objectType.Name)
{
case "SystemSetup":
writer = new XmlSerializer(typeof(Globals.SystemSetup));
break;
}
var wfile = new System.IO.StreamWriter(String.Format("{0}{1}", XMLPath, FileName));
writer.Serialize(wfile, objectSerializer);
wfile.Close();
}
catch (Exception ex)
{
ErrorHandler.ShowErrorMessage(ex);
}
}
这是一个winforms应用程序。它不是点击一次。我没有对任何集会实施任何安全限制。此外,我从这里调用的也没有第三方集会。
编辑:以上内容位于相同的命名空间中,但位于单独的类文件中。如果我将它们放入一个类文件中,它就可以工作。不确定这是否有帮助