• 0

Suggestions to fix a couple of FxCop errors!


Question

Hi,

I'm having a couple of FxCop errors that I don't really know how to fix. Well, I do know how to fix them, but I don't really like the solutions I can think of...

The errors I'm having are:

[suppressMessage("Microsoft.Design", "CA1019:DefineAccessorsForAttributeArguments")]

Code causing the error:

[CLSCompliantAttribute(true)]
[AttributeUsage(AttributeTargets.Property)]
public sealed class LightSettingsAttribute : Attribute {

	private string sectionName;
	private object defaultValue;

	public string SectionName {
		get {
			return this.sectionName;
		}
	}

	public object DefaultValue {
		get {
			return this.defaultValue;
		}
	}

	public LightSettingsAttribute(String sectionName, Type valueType, string defaultValue) {
		this.sectionName = sectionName;
		this.defaultValue = TypeDescriptor.GetConverter(valueType).ConvertFromInvariantString(defaultValue);
	}
}

I would fix it by creating a public readonly property and then set the private value (that the property returns) to the variable valueType passed as parameter of the constructor and then use the property value in the GetConverter() method. But this is stupid... I only need to use valueType on the GetConverter() inside the constructor, I don't need to save the value of valueType.

I checked similar code in the framework (using Reflector) and Microsoft doesn't do that, it simply uses the parameter value as I did. Anyway to go around this issue?

[suppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]

Basically this is how I'm handling some exception:

try {

} catch (Exception ex) {

}

I'm doing this because no matter what the CLS compilant exception thrown I need the piece of code inside the catch block to be executed. I can't go and catch each individual exception and duplicate the code (yes, I could put it in a method but I was looking to avoid that if possible). Maybe only 2 out of a few exceptions will actually be thrown (if they are) but either way, is there some way to catch multiple exceptions and use the same catch block?

4 answers to this question

Recommended Posts

  • 0
Hi,

I'm having a couple of FxCop errors that I don't really know how to fix. Well, I do know how to fix them, but I don't really like the solutions I can think of...

The errors I'm having are:

[suppressMessage("Microsoft.Design", "CA1019:DefineAccessorsForAttributeArguments")]

Code causing the error:

[CLSCompliantAttribute(true)]
[AttributeUsage(AttributeTargets.Property)]
public sealed class LightSettingsAttribute : Attribute {

	private string sectionName;
	private object defaultValue;

	public string SectionName {
		get {
			return this.sectionName;
		}
	}

	public object DefaultValue {
		get {
			return this.defaultValue;
		}
	}

	public LightSettingsAttribute(String sectionName, Type valueType, string defaultValue) {
		this.sectionName = sectionName;
		this.defaultValue = TypeDescriptor.GetConverter(valueType).ConvertFromInvariantString(defaultValue);
	}
}

I would fix it by creating a public readonly property and then set the private value (that the property returns) to the variable valueType passed as parameter of the constructor and then use the property value in the GetConverter() method. But this is stupid... I only need to use valueType on the GetConverter() inside the constructor, I don't need to save the value of valueType.

I checked similar code in the framework (using Reflector) and Microsoft doesn't do that, it simply uses the parameter value as I did. Anyway to go around this issue?

The beauty of FxCop is you can choose to ignore issues that you know are caused by a required design of yours. If it is throwing that, but you are happy with how it is, just suppress that issue.

[suppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]

Basically this is how I'm handling some exception:

try {

} catch (Exception ex) {

}

I'm doing this because no matter what the CLS compilant exception thrown I need the piece of code inside the catch block to be executed. I can't go and catch each individual exception and duplicate the code (yes, I could put it in a method but I was looking to avoid that if possible). Maybe only 2 out of a few exceptions will actually be thrown (if they are) but either way, is there some way to catch multiple exceptions and use the same catch block?

Do you know what types of Exceptions will be thrown? If you don't know and you are happy to catch a generic exception then again, just suppress the message.

  • 0

I know I can suppress the message but I've only found a way to do it in a global way, I mean, suppress the error type not the specific error regarding the error on line XYZ.

I wanted to suppress the error message specific to some line of codes, not the type of error (it might be good to know of that error in future code), but I don't know how can I do that. That's why I was asking for suggestions...

  • 0

I may be missing the point of the exception portion, but normally in the situation you describe I would write something like:

try
{
	 // iffy code
}
catch
{
	 throw;
}
finally
{
	 // vital code
}

Would that still trip up FxCop since it immediately throwing the exception again?

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.