Accessing Default Argument Values
Some languages (such as Visual C++ and Microsoft Visual Basic 2005) support the assignment of default values to arguments. For example, the following is a legitimate Visual Basic 2005 declaration that has default values for two of the arguments.
Public Sub MyMethod (a as Integer, _
Optional b as Double = 1.2, _
Optional c as Integer = 1)
You can use a parameter attribute to assign a default parameter value.
In Visual Basic and C++, optional parameters can be omitted when the method is called. In C# values must be specified for optional arguments.
For example, all the following Visual Basic and C++ examples are valid calls for MyMethod.
MyMethod(10, 55.3, 12)
MyMethod(10, 1.3) ' c == 1
MyMethod(11) ' b == 1.2, c == 1
MyMethod(10, 55.3, 12);
MyMethod(10, 1.3); // c == 1
MyMethod(11); // b == 1.2, c == 1
To retrieve the default value of an argument using reflection, get a ParameterInfo object for the parameter, and then retrieve the default value using the ParameterInfo.DefaultValue property. If there is no default value, the property returns Value.DBNull.
The following example displays the default values for MyMethod to the console.
Dim m As MethodInfo = t.GetMethod("MyMethod")
Dim ps As ParameterInfo() = m.GetParameters()
Dim i As Integer
For i = 0 To ps.Length - 1
Console.WriteLine("Default Value == {0}", ps(i).DefaultValue)
Next i
MethodInfo m = t.GetMethod("MyMethod");
ParameterInfo[] ps = m.GetParameters();
for (int i = 0; i < ps.Length; i+)
{
Console.WriteLine("Default Value == {0}", ps[i].DefaultValue);
}
MethodInfo m = t->GetMethod("MyMethod");
ParameterInfo[] ps = m->GetParameters();
for (int i = 0; i < ps.Length; i+)
{
Console::WriteLine(S"Default Value == {0}", ps[i]->DefaultValue);
}
To invoke methods that have arguments with default values, use Type.Missing as a parameter value to the InvokeMember method. This enables the late-binding service to use the default value for the indicated parameter value. If Type.Missing is passed for a parameter that has no default value, an ArgumentException is thrown. It is important to note that not all compilers' binding mechanisms might respect these rules for Type.Missing. Some binders might not support this functionality, or might treat Type.Missing differently. When using Type.Missing, the default values do not have to be trailing.
The C# language does not support default arguments.
The following Visual Basic 2005 example shows how to use Reflection to invoke methods that have default arguments.
Option Strict Off
Imports System
Imports System.Reflection
Public Class OptionalArg
Public Sub MyMethod (a As Integer, Optional b As Double = 1.2, Optional c As Integer=1)
Console.WriteLine("a = " & a & " b = " & b & " c = " & c)
End Sub
End Class
Module Module1
Sub Main()
Dim o As New OptionalArg
Dim t As Type
t = GetType(OptionalArg)
Dim Param As Object()= {10, 20, 30}
t.InvokeMember("MyMethod", _
BindingFlags.Public Or _
BindingFlags.Instance Or _
BindingFlags.InvokeMethod Or _
BindingFlags.OptionalParamBinding, _
Nothing, _
o, _
New Object() {10, 55.3, 12})
t.InvokeMember("MyMethod", _
BindingFlags.Public Or _
BindingFlags.Instance Or _
BindingFlags.InvokeMethod Or _
BindingFlags.OptionalParamBinding, _
Nothing, _
o, _
New Object() {10, 1.3, Type.Missing})
t.InvokeMember("MyMethod", _
BindingFlags.Public Or _
BindingFlags.Instance Or _
BindingFlags.InvokeMethod Or _
BindingFlags.OptionalParamBinding, _
Nothing, _
o, _
New Object() {10, Type.Missing, Type.Missing})
End Sub
End Module
When using the preceding technique, trailing default arguments are considered even when the caller specifies no value. This is the most common way to invoke methods with default arguments.
If you are using MethodBase.Invoke to invoke the method, you need to explicitly specify which arguments are defaults by passing an object array that contains Type.Missing for all the parameters that do not have values.