Type.Missing Campo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Rappresenta un valore mancante nelle informazioni Type. Questo campo è di sola lettura.
public: static initonly System::Object ^ Missing;
public static readonly object Missing;
staticval mutable Missing : obj
Public Shared ReadOnly Missing As Object
Valore del campo
Esempio
Nell'esempio di codice seguente viene illustrato l'uso Missing
del campo per richiamare un metodo con gli argomenti predefiniti.
#using <System.dll>
using namespace System;
using namespace System::Reflection;
using namespace System::CodeDom::Compiler;
ref 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, nullptr, o, gcnew array<Object^> {10, 55.3, 12});
t->InvokeMember("MyMethod", bf, nullptr, o, gcnew array<Object^> {10, 1.3, Type::Missing});
t->InvokeMember("MyMethod", bf, nullptr, o, gcnew array<Object^> {10, Type::Missing, Type::Missing});
}
private:
static Object^ GenerateObjectFromSource(String^ objectName,
String^ sourceLines, String^ providerName)
{
Object^ genObject = nullptr;
CodeDomProvider^ codeProvider = CodeDomProvider::CreateProvider(providerName);
CompilerParameters^ cp = gcnew 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;
}
};
int main()
{
Example::Main();
}
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;
}
}
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
Questo codice genera l'output seguente:
a = 10 b = 55,3 c = 12
a = 10 b = 1,3 c = 1
a = 10 b = 1,2 c = 1
Commenti
Usare il Missing
campo per la chiamata tramite reflection per ottenere il valore predefinito di un parametro. Se il campo viene passato per un valore di parametro e non esiste alcun valore predefinito per tale parametro, viene Missing
generata ArgumentException un'eccezione .