Type.Missing Campo
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Representa um valor em falta na Type informação. Este campo é só de leitura.
public: static initonly System::Object ^ Missing;
public static readonly object Missing;
staticval mutable Missing : obj
Public Shared ReadOnly Missing As Object
Valor de Campo
Exemplos
O seguinte exemplo de código mostra o uso do Missing campo para invocar um método com os seus argumentos padrão.
using System;
using System.Reflection;
using System.CodeDom.Compiler;
class Example
{
public static void Main()
{
// VB source for example. Not all versions of CS and CPP compilers
// support optional arguments.
string codeLines =
"Imports System\n\n" +
"Public Class OptionalArg\n" +
" Public Sub MyMethod(ByVal a As Integer, _\n" +
" Optional ByVal b As Double = 1.2, _\n" +
" Optional ByVal c As Integer = 1)\n\n" +
" Console.WriteLine(\"a = \" & a & \" b = \" & b & \" c = \" & c)\n" +
" End Sub\n" +
"End Class\n";
// Generate a OptionalArg instance from the source above.
object o = GenerateObjectFromSource("OptionalArg", codeLines, "VisualBasic");
Type t;
t = o.GetType();
BindingFlags bf = BindingFlags.Public | BindingFlags.Instance |
BindingFlags.InvokeMethod | BindingFlags.OptionalParamBinding;
t.InvokeMember("MyMethod", bf, null, o, new object[] {10, 55.3, 12});
t.InvokeMember("MyMethod", bf, null, o, new object[] {10, 1.3, Type.Missing});
t.InvokeMember("MyMethod", bf, null, o, new object[] {10, Type.Missing, Type.Missing});
}
private static object GenerateObjectFromSource(string objectName,
string sourceLines, string providerName)
{
object genObject = null;
CodeDomProvider codeProvider = CodeDomProvider.CreateProvider(providerName);
CompilerParameters cp = new CompilerParameters();
cp.GenerateExecutable = false;
cp.GenerateInMemory = true;
CompilerResults results =
codeProvider.CompileAssemblyFromSource(cp, sourceLines);
if (results.Errors.Count == 0)
{
genObject = results.CompiledAssembly.CreateInstance(objectName);
}
return genObject;
}
}
open System
open System.Reflection
open System.CodeDom.Compiler
let generateObjectFromSource objectName (sourceLines: string) providerName =
let codeProvider = CodeDomProvider.CreateProvider providerName
let cp = CompilerParameters()
cp.GenerateExecutable <- false
cp.GenerateInMemory <- true
let results = codeProvider.CompileAssemblyFromSource(cp, sourceLines)
if results.Errors.Count = 0 then
results.CompiledAssembly.CreateInstance objectName
else
null
// VB source for example. Not all versions of CS and CPP compilers
// support optional arguments.
let codeLines =
"""Imports System
Public Class OptionalArg
Public Sub MyMethod(ByVal a As Integer, _
Optional ByVal b As Double = 1.2, _
Optional ByVal c As Integer = 1)
Console.WriteLine(\"a = \" & a & \" b = \" & b & \" c = \" & c)
End Sub
End Class"""
// Generate a OptionalArg instance from the source above.
let o = generateObjectFromSource "OptionalArg" codeLines "VisualBasic"
let t = o.GetType()
let bf = BindingFlags.Public ||| BindingFlags.Instance ||| BindingFlags.InvokeMethod ||| BindingFlags.OptionalParamBinding
t.InvokeMember("MyMethod", bf, null, o, [| 10; 55.3; 12 |]) |> ignore
t.InvokeMember("MyMethod", bf, null, o, [| 10; 1.3; Type.Missing |]) |> ignore
t.InvokeMember("MyMethod", bf, null, o, [| 10; Type.Missing; Type.Missing |]) |> ignore
Imports System.Reflection
Public Class OptionalArg
Public Sub MyMethod(ByVal a As Integer, _
Optional ByVal b As Double = 1.2, _
Optional ByVal c As Integer = 1)
Console.WriteLine("a = " & a & " b = " & b & " c = " & c)
End Sub
End Class
Class Example
Public Shared Sub Main()
Dim o As New OptionalArg()
Dim t As Type
t = GetType(OptionalArg)
Dim bf As BindingFlags = _
BindingFlags.Public Or BindingFlags.Instance Or _
BindingFlags.InvokeMethod Or BindingFlags.OptionalParamBinding
t.InvokeMember("MyMethod", bf, Nothing, o, New Object() {10, 55.3, 12})
t.InvokeMember("MyMethod", bf, Nothing, o, New Object() {10, 1.3, Type.Missing})
t.InvokeMember("MyMethod", bf, Nothing, o, New Object() {10, Type.Missing, Type.Missing})
End Sub
End Class
Este código gera o seguinte resultado:
A = 10 B = 55,3 C = 12
A = 10 B = 1,3 C = 1
A = 10 B = 1,2 C = 1
Observações
Use o Missing campo para invocação através da reflexão para obter o valor padrão de um parâmetro. Se o Missing campo for passado para um valor de parâmetro e não houver valor padrão para esse parâmetro, um ArgumentException é lançado.