cshaigh1

Hello.

When an instantiated class needs to be serialised into XML, its public properties must define both a get and set method. When the get method is a calculated value, it is counter-intuitive to write a corresponding set method, but serialisation constraints demand that a set method be left in, even if it is blank; however, doing so violates FxCop rule CA1801: Review Unused Parameters because the implicit value parameter in the set method remains unused.

For example:

Code Snippet
[XmlAttribute]
public string FullPathName
{
get
{
return Tools.GetFullPathName (localPath);
}

set { } // incurs the wrath of FxCop!
}



I was wondering - is there a workaround for this In every other case that I have seen where XML serialisation is not required, I would like to have this rule enforced, but it seems in this case that XmlSerialization and FxCop have conflicting interests.

Thanks in advance for your help

Chris Haigh - Developer/Researcher
Zolv.com Ltd (www.zolv.com)



Re: Visual Studio Code Analysis and Code Metrics CA1801: Review Unused Parameters and XML serialisation

David M. Kean - MSFT

In situations where a property is a calculated value, it sometimes doesn't make sense to persist that value to XML because it's value can simply be recalculated via other properties on the type. However, it does make sense to persist it if you have other consumers of the XML outside of .NET (other than deserializing back into the same type).

There are a couple of options that I can see:

1) Suppress the warning. This is exactly the type of situation that suppression was designed for. My concern with this though, is that the empty set could be confusing to the consumer of the class.

2) Implement IXmlSerializable and handle the serializing yourself. This may or may not be overkill depending on your situation.

Regards

David






Re: Visual Studio Code Analysis and Code Metrics CA1801: Review Unused Parameters and XML serialisation

cshaigh1

Thanks David

I've considered what you've said. Option 1 would definitely be the easiest, although I would prefer to keep this warning very much alive. Option 2 would be overkill I think, just for the sake of keeping FxCop happy. I did have an idea to modify the code to use a private variable as follows:

Code Snippet

private string fullPathName = string.Empty;

[XmlAttribute]
public string FullPathName
{
get
{
if (fullPathName == string.Empty)
{
fullPathName = Tools.GetFullPathName (localPath);
}

return fullPathName;
}

set
{
fullPathName = value;
}
}



I think this solution would keep both FxCop and the XmlSerializer happy. Would you agree

Thanks again for your help

Chris Haigh - Developer/Researcher
Zolv.com Ltd (www.zolv.com)




Re: Visual Studio Code Analysis and Code Metrics CA1801: Review Unused Parameters and XML serialisation

David M. Kean - MSFT

That looks fine, however, you need to be aware that this may unexpected consequences. Now, when the object is deserialized, fullPathName will be set and GetFullPathName will never be called. If you are deserializing this on a different computer, then this will be a change in behavior.