When checking a method's parameter, I throw an ArgumentNullException
if it's null. See the first line in the method below. But what about properties on the parameter that shouldn't be null? If I try to handle them the same way, I get a code analysis error:
CA2208 Instantiate argument exceptions correctly Method 'PriorityDeratingComponentLogic.CreateItem(IvSimulation)' passes 'ivSimulation.SolarPanel' as the 'paramName' argument to a 'ArgumentNullException' constructor. Replace this argument with one of the method's parameter names. Note that the provided parameter name should have the exact casing as declared on the method.
public DeratingComponentBase CreateItem(IvSimulation ivSimulation)
{
if (ivSimulation == null) { throw new ArgumentNullException("ivSimulation"); }
if (ivSimulation.SolarPanel == null) { throw new ArgumentNullException("ivSimulation.SolarPanel"); }
if (ivSimulation.GlobalEquipment == null) { throw new ArgumentNullException("ivSimulation.GlobalEquipment"); }
// ... method body here
}
Is the CA error something I should suppress, or is there a generally-accepted way to better handle this? Perhaps the issue is upstream and we shouldn't even have to check for those properties being null at this point?