A guy buys a Ferrari, but some fool set the governor (the contraption that limits the speed of the car) to 30 mph. The guy cannot change the governor to something more reasonable, so he rips it out. Now the Ferrari can go as fast as the engine can muscle.
Microsoft doesn't let you use nullables as custom attribute properties. However, Microsoft does let you use strings, which can be null, as custom attribute properties. So rip out the limitation altogether. Replace your nullable int with a string. Sure, a string is even less restrictive than a nullable int as it can have non-integer values like "bob", but that's the price you pay for Microsoft screwing up custom attributes, a language feature, for an implementation detail in the CLR that should be irrelevant.
Here's my example.
public abstract class Contract : Attribute, IContract
{
public abstract void Check (Object root, String path, Object valueAtPath);
}
public sealed class DecimalPlacesContract : Contract
{
public String MinimumMantissaCount
{
get
{
return minimumMantissaCount?.ToString();
}
set
{
minimumMantissaCount = value == null ? (int?) null : Int32.Parse(value);
}
}
public String MaximumMantissaCount
{
get
{
return maximumMantissaCount?.ToString();
}
set
{
maximumMantissaCount = value == null ? (int?) null : Int32.Parse(value);
}
}
public String MinimumSignificantDigitCount
{
get
{
return minimumSignificantDigitCount?.ToString();
}
set
{
minimumSignificantDigitCount = value == null ? (int?) null : Int32.Parse(value);
}
}
public String MaximumSignificantDigitCount
{
get
{
return maximumSignificantDigitCount?.ToString();
}
set
{
maximumSignificantDigitCount = value == null ? (int?) null : Int32.Parse(value);
}
}
private int? minimumMantissaCount;
private int? maximumMantissaCount;
private int? minimumSignificantDigitCount;
private int? maximumSignificantDigitCount;
public override void Check (Object root, String path, Object valueAtPath)
{
decimal? value = valueAtPath as decimal?;
int mantissaCount = DecimalMisc.GetMantissaDigitCount(value ?? 0);
int significantDigitCount = DecimalMisc.GetSignificantDigitCount(value ?? 0);
if (value == null ||
mantissaCount < minimumMantissaCount ||
mantissaCount > maximumMantissaCount ||
significantDigitCount < minimumSignificantDigitCount ||
significantDigitCount > maximumSignificantDigitCount)
{
throw new ContractException(this, root, path, valueAtPath);
}
}
}
Here's how to use the custom attribute properties.
private class Dollar
{
[DecimalPlacesContract (MinimumMantissaCount = "0", MaximumMantissaCount = "2")]
public decimal Amount { get; set; }
}
private class DollarProper
{
[DecimalPlacesContract (MinimumSignificantDigitCount = "2", MaximumSignificantDigitCount = "2")]
public decimal Amount { get; set; }
}
Just add quotes around the values. Unspecified properties default to null.
Any improper value like "bob" will cause a FormatException to be thrown when you call GetCustomAttributes or GetCustomAttribute off the Type or PropertyInfo instance. Sure, it would be nice to have the compile-time check of the nullable int, but this is good enough. So rip that governor right out.
int?
property on an attribute with two constructors -- is that what you tried when you said properties don't work? I'm trying to figure out if this restriction was removed since the version of .NET you were using (.NET 4.0 I'm guessing?).