Type.Missing Field
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Represents a missing value in the Type information. This field is read-only.
public: static initonly System::Object ^ Missing;
public static readonly object Missing;
staticval mutable Missing : obj
Public Shared ReadOnly Missing As Object
The following code example shows the use of the Missing
field to invoke a method with its default arguments.
#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;
}
}
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
This code produces the following output:
a = 10 b = 55.3 c = 12
a = 10 b = 1.3 c = 1
a = 10 b = 1.2 c = 1
Use the Missing
field for invocation through reflection to obtain the default value of a parameter. If the Missing
field is passed in for a parameter value and there is no default value for that parameter, an ArgumentException is thrown.
Продукт | Версии |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
Отзыв о .NET
.NET — это проект с открытым исходным кодом. Выберите ссылку, чтобы оставить отзыв: