这是我的方法:
public BackgroundJobInfo Deserialize(string info)
{
using (var stringReader = new StringReader(info))
{
try
{
var xmlTextReader = XmlReader.Create(stringReader);
return (BackgroundJobInfo)Serializer.ReadObject(xmlTextReader);
}
catch (Exception e)
{
_logger.ErrorException(e, "Error when deserializing a BackgroundJobInfo object from string <{0}>", info);
return null;
}
}
}
我需要在哪里使用using()
声明?也许就在try {...}
里面?
答案 0 :(得分:0)
由于在try块之外不需要你的stringReader,我建议你这样使用它:
public BackgroundJobInfo Deserialize(string info)
{
try
{
using (var stringReader = new StringReader(info))
{
var xmlTextReader = XmlReader.Create(stringReader);
return (BackgroundJobInfo)Serializer.ReadObject(xmlTextReader);
}
}
catch (Exception e)
{
_logger.ErrorException(e, "Error when deserializing a BackgroundJobInfo object from string <{0}>", info);
return null;
}
}
但最后由你决定如何使用它。