Skip to content

Constructor Argument Value

"ConstructorArgument" parameters should exist in constructors.

What does this mean ?

When creating a custom Markup Extension that accepts parameters in WPF, the ConstructorArgument markup must be used to identify the discrete properties that match these parameters. However since this is done via a string, the compiler will not notice if there are typos.

What can happen ?

This rule raises an issue when the string argument to ConstructorArgumentAttribute doesn't match any parameter of any constructor.

Recommendation

Need to write the constructor argument string correctly as compiler will not detect if in case it's a typo.

Sample Code

Vulnerable :

using System;

namespace myLibrary
{
  public class MyExtension : MarkupExtension
  {
    public MyExtension() { }

    public MyExtension(object value1)
    {
      Value1 = value1;
    }

    [ConstructorArgument("value2")]   // Noncompliant
    public object Value1 { get; set; }
  }
}

Non Vulnerable :

using System;

namespace myLibrary
{
  public class MyExtension : MarkupExtension
  {
    public MyExtension() { }

    public MyExtension(object value1)
    {
      Value1 = value1;
    }

    [ConstructorArgument("value1")]
    public object Value1 { get; set; }
  }
}

References :