Can anyone explain why this code works:
public class AdministratorSettingValidationAttribute : Attribute
{
public AdministratorSettingValidationAttribute(AdministratorSettingDataType administratorSettingDataType)
{
DataType = administratorSettingDataType;
}
public AdministratorSettingValidationAttribute(AdministratorSettingDataType administratorSettingDataType, Type enumerationType)
{
DataType = administratorSettingDataType;
EnumerationType = enumerationType;
}
}
...but refactoring it to use an optional parameter instead:
public AdministratorSettingValidationAttribute(AdministratorSettingDataType administratorSettingDataType, Type enumerationType = null)
{
DataType = administratorSettingDataType;
EnumerationType = enumerationType;
}
...causes a compile time error:
"An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type".
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…