Missing Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Represents a missing Object. This class cannot be inherited.

Inheritance Hierarchy

System.Object
  System.Reflection.Missing

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public NotInheritable Class Missing
[ComVisibleAttribute(true)]
public sealed class Missing

The Missing type exposes the following members.

Methods

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetType Gets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Fields

  Name Description
Public fieldStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Value Represents the sole instance of the Missing class.

Top

Remarks

Missing is used to invoke a method with a default argument.

Only one instance of Missing ever exists.

Examples

The following example shows how to use Missing to invoke a method with a default argument with late binding.

To run this example, first see Building Examples That Use a Demo Method and a TextBlock Control. Once you have created a Silverlight application project, you must include the following Visual Basic code. For a Visual Basic Silverlight-based application, add the code to your project as a new class. For a C# Silverlight-based application, add a Visual Basic Silverlight class library project, and create a reference to this project from your C# project.

Public Class MissingSample
    
    Public Shared Function MyMethod(Optional k As Integer = 33) As String
        Return "k = " & k.ToString()
    End Function

End Class
NoteNote:

The C# code in this example assumes that the Visual Basic class library project is named SilverlightLibrary; if you name it something else you must change the namespace that is shown in the argument of the GetMethod method.

Visual Basic code is used for the MissingSample class because C# does not support optional parameters in managed code. Optional parameters are not part of the Common Language Specification (CLS). Therefore, code that uses optional parameters is not CLS-compliant. For more information, see the Common Language Specification and Writing CLS-Compliant Code in the .NET Framework documentation.

Imports System.Reflection

Public Class MissingSample

    Public Shared Function MyMethod(Optional k As Integer = 33) As String
        Return "k = " & k.ToString()
    End Function

End Class


Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' To invoke MyMethod with the default argument value, pass 
      ' Missing.Value for the optional parameter. First, get the 
      ' method.
      Dim mi As MethodInfo = GetType(MissingSample).GetMethod("MyMethod")

      ' Second, create an array of parameters to pass to the method.
      ' In this case, the array contains just one element.
      Dim arguments() As Object = { Missing.Value }

      ' Finally, invoke the method. Specify Nothing for the target
      ' object, because the method is Shared.
      Dim result As Object = mi.Invoke(Nothing, arguments)

      outputBlock.Text &= String.Format("MyMethod returned '{0}'" & vbLf, result)

   End Sub

End Class

' This code example produces the following output:
'
'MyMethod returned 'k = 33'
using System;
using System.Reflection;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // To invoke MyMethod with the default argument value, pass 
      // Missing.Value for the optional parameter. First, get the 
      // method.
      MethodInfo mi = 
         typeof(SilverlightLibrary.MissingSample).GetMethod("MyMethod");

      // Second, create an array of parameters and invoke the method.
      // In this case, the array contains just one element.
      object result = mi.Invoke(null, new Object[] { Missing.Value });

      outputBlock.Text += String.Format("MyMethod returned '{0}'\n", result);
   }
}

/* This code example produces the following output:

MyMethod returned 'k = 33'
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.