Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-在另一个类中调用/写入反序列化方法_C#_Xml_Serialization_Deserialization - Fatal编程技术网

C#-在另一个类中调用/写入反序列化方法

C#-在另一个类中调用/写入反序列化方法,c#,xml,serialization,deserialization,C#,Xml,Serialization,Deserialization,我试图反序列化一个类,以便在XML文件中保存特定于用户的值(如dirpath) 以下是我的课程片段: [Serializable] public class CustomSettings { public string initPath; public string InitPath => initPath; } 如果我在我的main窗口中执行此操作,序列化和反序列化工作都会非常好:

我试图反序列化一个类,以便在XML文件中保存特定于用户的值(如dirpath)

以下是我的课程片段:

      [Serializable]
       public class CustomSettings
        {
            public string initPath;
            public string InitPath => initPath;
        }
如果我在我的
main窗口中执行此操作,序列化和反序列化工作都会非常好:

            CustomSettings cs = new CustomSettings();
            XmlSerializer mySerializer = new XmlSerializer(typeof(CustomSettings));
            StreamWriter myWriter = new StreamWriter(@"U:\Alex\prefs.xml");
            mySerializer.Serialize(myWriter, cs);
            myWriter.Close();
对于反序列化:

        CustomSettings cs = new CustomSettings();
        XmlSerializer _mySerializer = new XmlSerializer(typeof(CustomSettings));
        FileStream myFstream = new FileStream(@"U:\Alex\prefs.xml", FileMode.Open);
        cs = (CustomSettings)_mySerializer.Deserialize(myFstream);
        myFstream.Close();
现在,因为我需要做几次反序列化,我想我写了两个方法来完成上面的工作,但是由于可读性,我希望它们在另一个类中

main窗口中
我在另一个类中调用了此方法,该类也按预期工作:

 public void WriteSettingsToFile(CustomSettings cs)
    {
        XmlSerializer mySerializer = new XmlSerializer(typeof(CustomSettings));
        StreamWriter myWriter = new StreamWriter(@"U:\Alex\prefs.xml");
        mySerializer.Serialize(myWriter, cs);
        myWriter.Close();
    }
不过,我编写的反序列化函数不起作用。它成功加载文件,返回正确的路径值,但一旦返回到调用类,字符串总是空的。我多次重写该方法,尝试void、non-void返回字符串、static等,但我被卡住了

任何帮助和想法都将不胜感激

编辑: 下面是使用反序列化方法的一次尝试:

 public void GetInitPath(CustomSettings cs)
    {
        XmlSerializer _mySerializer = new XmlSerializer(typeof(CustomSettings));
        FileStream myFstream = new FileStream(@"U:\Alex\prefs.xml", FileMode.Open);
        cs = (CustomSettings)_mySerializer.Deserialize(myFstream);
        myFstream.Close();

    }
编辑2:

在马修·沃森的帮助下,我解决了我的问题

 public void GetInitPath(ref CustomSettings cs)
    {

        XmlSerializer serializer = new XmlSerializer(typeof(CustomSettings));

        using (var myFstream = new FileStream(@"U:\Alex\prefs.xml", FileMode.Open))
        {
            cs = (CustomSettings)serializer.Deserialize(myFstream);
        }
    }

这是不起作用的方法:

public void GetInitPath(CustomSettings cs)
{
    XmlSerializer _mySerializer = new XmlSerializer(typeof(CustomSettings));
    FileStream    myFstream     = new FileStream(@"U:\Alex\prefs.xml", FileMode.Open);
    cs                          = (CustomSettings)_mySerializer.Deserialize(myFstream);
    myFstream.Close();
}
这个方法的问题是它不返回结果

它使用从
.Deserialise()
返回的对象引用覆盖传递到
GetInitPath()
的对
cs
的引用,但这不会更改传递到
GetInitPath()
的原始对象

解决方案是将参数声明为
ref
参数,或者(更好)更改方法以返回结果,如下所示:

public CustomSettings GetInitPath()
{
    XmlSerializer _mySerializer = new XmlSerializer(typeof(CustomSettings));
    FileStream    myFstream     = new FileStream(@"U:\Alex\prefs.xml", FileMode.Open);
    var cs                      = (CustomSettings)_mySerializer.Deserialize(myFstream);
    myFstream.Close();

    return cs;
}
另一个观察结果是:如果可能的话,最好使用
using
来包装可丢弃的资源。如果这样做,您的方法将如下所示:

public CustomSettings GetInitPath()
{
    var serializer = new XmlSerializer(typeof(CustomSettings));

    using (var myFstream = new FileStream(@"U:\Alex\prefs.xml", FileMode.Open))
    {
        return (CustomSettings) serializer.Deserialize(myFstream);
    }
}

这将关闭
myFstream
,即使在
反序列化()中发生异常

如果您实际包含反序列化的代码,但该代码不起作用,这将非常有用…它看起来与我的帖子中提供的完全相同…您的类实例是cs:CustomSettings cs=new CustomSettings();因此,您需要将父类的“cs”设置为父对象,如List settings=new List();设置。添加(cs);谢谢你的提醒!不幸的是,将结果返回给调用者会再次导致字符串为null。我的两个方法现在都在
TestMethods
类中。在
main窗口中
我像这样调用反序列化方法:
tm.GetInitPath()
。然后,我想将
InitPath
-字符串传递给本地字符串
initialPath
,但它是空的。我做错了什么?很显然,正如你所提到的,
ref
-关键字起了作用。所有值现在都正确地传递给调用函数。我将使用工作解决方案更新OP。谢谢你的帮助!您能解释一下为什么使用返回值而不是
ref
更好吗?老实说,我从来没有在我的方法中使用过
ref
,因为非void方法通常对我有效,除了这种情况。最好使用return,因为该方法正在创建一个全新的对象,所以让代码创建一个虚拟对象只是为了用真实对象替换它是不正确的。