다음을 통해 공유


Accessing Default Argument Values

Some languages (such as the Managed Extensions for C++ and Microsoft Visual Basic .NET) support the assignment of default values to arguments. For example, the following example is a legitimate Visual Basic .NET 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.

You can declare default values of arguments by specifying exactly which arguments are defaults or by leaving off the trailing default arguments. For example, all the following examples are valid calls for MyMethod.

MyMethod(10, 55.3, 12)
MyMethod(10, 1.3) ' c == 1
MyMethod(11) ' b == 1.2, c == 1
[C#]
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
[C#]
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);
}

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 .NET example shows how 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.

See Also

Viewing Type Information | Type.Missing | Reflection.Missing |MethodBase.Invoke | InvokeMember