I'm looking at how ParameterInfo.IsOptional
is defined (I'm adding default parameter support to an internal IOC framework), and it seems to me that, when true, there is no guarantee that ParameterInfo.DefaultValue
(or indeed ParameterInfo.RawDefaultValue
) are actually the default values that are to be applied.
If you look at the MSDN example given for IsOptional
, it seems possible in IL to define a parameter that is optional but for which no default is supplied (given that the ParameterAttributes.HasDefault
must be explicitly supplied). I.e. potentially leading to a situation that a parameter type is, say, Int32
, ParameterInfo.IsOptional
is true, but ParameterInfo.DefaultValue
is null.
My language is C#, therefore I can work on what that compiler will do. Based on that I can have a simple test as follows (parameter
here is a ParameterInfo
instance, and the method is meant to return an instance to be used as the runtime argument for the parameter):
if(no_config_value)
{
if(!parameter.IsOptional) throw new InvalidOperationException();
//it's optional, so read the Default
return parameter.DefaultValue;
}
else
return current_method_for_getting_value();
But I'm thinking that some languages (and I want to get this right at the IL-level, rather than just based on what one particular compiler does) can place the onus on the caller to determine the default value to be used, if so, a default(parameter.ParameterType)
would need to be in order.
This is where it gets a little more interesting, because DefaultValue
is, apparently DBNull.Value
(according to the documentation for RawValue
) if there is no default. Which is no good if the parameter is of type object
and IsOptional==true
!
Having done a bit more digging, I'm hopeful that the reliable way to solve this is to physically read the ParameterInfo.Attributes
member, reading the bitflags individually first to check for ParameterAttributes.Optional
and then check for ParameterAttributes.Default
. Only if both are present, then reading ParameterInfo.DefaultValue
will be correct.
I'm going to start coding and writing tests around this, but I'm asking in the hope that there's someone with more IL knowledge that can confirm my suspicions and hopefully confirm that this'll be correct for any IL-based language (thus avoiding the need to mock up loads of libraries in different languages!).