Code Anlysis Rule CA2000 / CA2202

前端 未结 3 602
日久生厌
日久生厌 2020-12-18 05:39

I am trying to ensure my coding follows correct disposal of objects so I am enforcing these rules as errors. But I am having trouble with this section of code



        
3条回答
  •  长情又很酷
    2020-12-18 06:03

    This is not the answer, but you might find this code more readable:

    public String ToXml()
    {
        var objSerializer =
            new DataContractSerializer(GetType());
    
        using (var objStream = new MemoryStream())
        {
            //  Serialize the object
            objSerializer.WriteObject(objStream, this);
    
            // Move to start of stream to read 
            // out contents
            objStream.Seek(0, SeekOrigin.Begin);
    
            using (var objReader =
                new StreamReader(objStream))
            {
                // Read Contents into a string
                retirm objReader.ReadToEnd();
            }
        }
    }
    

提交回复
热议问题