Export (0) Print
Expand All

ParameterInfo.IsOptional Property

Gets a value indicating whether this parameter is optional.

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

public bool IsOptional { get; }

Property Value

Type: System.Boolean
true if the parameter is optional; otherwise, false.

This method depends on an optional metadata flag. This flag can be inserted by compilers, but compilers are not obligated to do so.

This method uses the Optional member of the ParameterAttributes enumeration.

To get the ParameterInfo array, first get the method, and then call MethodBase.GetParameters.

The following example shows how to test method parameters for the ParameterAttributes.Optional attribute. The example does the following:

  • Creates a dynamic assembly that contains a type named MyType.

  • Adds a static method (Shared method in Visual Basic) named MyMethod to MyType. MyMethod has an optional parameter with a default value.

  • Calls TypeBuilder.CreateType to complete the type.

  • Invokes MyMethod with no parameters to demonstrate the default value of the optional parameter.

  • Gets the parameter list for MyMethod and tests for the ParameterAttributes.In, ParameterAttributes.Out, and ParameterAttributes.Optional attributes.


using System;
using System.Reflection;
using System.Reflection.Emit;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      AssemblyName myAssemblyName = new AssemblyName("MyAssembly");
      AssemblyBuilder myAssemblyBuilder = 
         AppDomain.CurrentDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
      ModuleBuilder myModuleBuilder = 
         myAssemblyBuilder.DefineDynamicModule("MyModule");

      // Create a type in the module.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyType", TypeAttributes.Public);

      // Create a method called MyMethod that takes a string argument and returns a
      // string.
      MethodBuilder myMethodBuilder = 
         myTypeBuilder.DefineMethod("MyMethod", 
                                    MethodAttributes.Public | MethodAttributes.Static, 
                                    typeof(string), 
                                    new Type[] { typeof(string) });

      // Make the parameter optional, and give it a default value.
      ParameterBuilder myParameterBuilder = 
         myMethodBuilder.DefineParameter(1, 
            ParameterAttributes.Optional | ParameterAttributes.HasDefault, "inputString");
      myParameterBuilder.SetConstant("The default value");

      // Get the Microsoft Intermediate Language generator for the method.
      ILGenerator myILGenerator = myMethodBuilder.GetILGenerator();

      // Get the argument and return it. When the method is called without the
      // optional parameter, this returns the default value of the parameter.
      myILGenerator.Emit(OpCodes.Ldarg_0);
      myILGenerator.Emit(OpCodes.Ret);

      // Create the type.
      Type myType = myTypeBuilder.CreateType();

      // Invoke the method with no arguments, by using OptionalParamBinding. This
      // returns the default value.
      object returnValue = myType.InvokeMember("MyMethod", 
         BindingFlags.InvokeMethod | BindingFlags.OptionalParamBinding | 
            BindingFlags.Static | BindingFlags.Public, 
         Type.DefaultBinder, 
         null, 
         null);

      outputBlock.Text += 
         String.Format("Calling MyMethod without the optional parameter returns: '{0}'\n",
         returnValue);

      // Get the method named MyMethod from the type.
      MethodBase myMethodBase = myType.GetMethod("MyMethod");


      // Get the parameters of the method.
      ParameterInfo[] myParameters = myMethodBase.GetParameters();

      outputBlock.Text += String.Format("The method {0} has {1} parameters:\n", 
         myMethodBase, myParameters.Length);

      // Print the IN, OUT and OPTIONAL attributes associated with each of the parameters.
      foreach( ParameterInfo param in myParameters )
      {
         if (param.IsDefined(typeof(System.Runtime.InteropServices.InAttribute), false))
         {
            outputBlock.Text += 
               String.Format("    Parameter '{0}', position {1}, has the In attribute\n", 
                  param.Name, param.Position);
         }
         if (param.IsOptional)
         {
            outputBlock.Text += 
               String.Format("    Parameter '{0}', position {1}, has the Optional attribute\n", 
                  param.Name, param.Position);
         }
         if (param.IsOut)
         {
            outputBlock.Text += 
               String.Format("    Parameter '{0}', position {1}, has the Out attribute\n", 
                  param.Name, param.Position);
         }
      }
   }
}

/* This example produces the following output:

Calling MyMethod without the optional parameter returns: 'The default value'
The method System.String MyMethod(System.String) has 1 parameters:
    Parameter 'inputString', position 0, has the Optional attribute
 */


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

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

Community Additions

ADD
Show:
© 2014 Microsoft