C# ParameterInfo IsOptional
Description
ParameterInfo IsOptional
Gets a value indicating whether
this parameter is optional.
Syntax
ParameterInfo.IsOptional
has the following syntax.
public bool IsOptional { get; }
Example
using System;/* w w w .j av a2s .c o m*/
using System.Reflection;
using System.Threading;
using System.Reflection.Emit;
public class ParameterInfo_IsIn_IsOut_IsOptional
{
public static void Main()
{
Assembly[] myAssemblies = Thread.GetDomain().GetAssemblies();
Assembly myAssembly = null;
for(int i = 0; i < myAssemblies.Length; i++)
if(String.Compare(myAssemblies[i].GetName(false).Name, "MyAssembly") == 0)
myAssembly = myAssemblies[i];
if(myAssembly != null)
{
Type myType = myAssembly.GetType("MyType");
MethodBase myMethodBase = myType.GetMethod("MyMethod");
ParameterInfo[] myParameters = myMethodBase.GetParameters();
for(int i = 0; i < myParameters.Length; i++)
{
if(myParameters[i].IsIn)
Console.WriteLine("\tThe {0} parameter has the In attribute",
i + 1);
if(myParameters[i].IsOptional)
Console.WriteLine("\tThe {0} parameter has the Optional attribute",
i + 1);
if(myParameters[i].IsOut)
Console.WriteLine("\tThe {0} parameter has the Out attribute",
i + 1);
}
}
}
}