Type.DefaultBinder Property
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.
Gets a reference to the default binder, which implements internal rules for selecting the appropriate members to be called by InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]).
public:
static property System::Reflection::Binder ^ DefaultBinder { System::Reflection::Binder ^ get(); };
public static System.Reflection.Binder DefaultBinder { get; }
static member DefaultBinder : System.Reflection.Binder
Public Shared ReadOnly Property DefaultBinder As Binder
A reference to the default binder used by the system.
The following example gets the default binder from the DefaultBinder
property, and invokes a member of MyClass by passing the DefaultBinder
value as a parameter to InvokeMember.
using namespace System;
using namespace System::Reflection;
ref class MyClass
{
public:
void HelloWorld()
{
Console::WriteLine( "Hello World" );
}
};
int main()
{
try
{
Binder^ defaultBinder = Type::DefaultBinder;
MyClass^ myClass = gcnew MyClass;
// Invoke the HelloWorld method of MyClass.
myClass->GetType()->InvokeMember( "HelloWorld", BindingFlags::InvokeMethod, defaultBinder, myClass, nullptr );
}
catch ( Exception^ e )
{
Console::WriteLine( "Exception : {0}", e->Message );
}
}
using System;
using System.Reflection;
public class MyDefaultBinderSample
{
public static void Main()
{
try
{
Binder defaultBinder = Type.DefaultBinder;
MyClass myClass = new MyClass();
// Invoke the HelloWorld method of MyClass.
myClass.GetType().InvokeMember("HelloWorld", BindingFlags.InvokeMethod,
defaultBinder, myClass, new object [] {});
}
catch(Exception e)
{
Console.WriteLine("Exception :" + e.Message);
}
}
class MyClass
{
public void HelloWorld()
{
Console.WriteLine("Hello World");
}
}
}
open System
open System.Reflection
type MyClass() =
member _.HelloWorld() =
printfn "Hello World"
try
let defaultBinder = Type.DefaultBinder
let myClass = MyClass()
// Invoke the HelloWorld method of MyClass.
myClass.GetType().InvokeMember("HelloWorld", BindingFlags.InvokeMethod, defaultBinder, myClass, [||])
|> ignore
with e ->
printfn $"Exception: {e.Message}"
Imports System.Reflection
Public Class MyDefaultBinderSample
Public Shared Sub Main()
Try
Dim defaultBinder As Binder = Type.DefaultBinder
Dim [myClass] As New [MyClass]()
' Invoke the HelloWorld method of MyClass.
[myClass].GetType().InvokeMember("HelloWorld", BindingFlags.InvokeMethod, defaultBinder, [myClass], New Object() {})
Catch e As Exception
Console.WriteLine("Exception :" + e.Message.ToString())
End Try
End Sub
Class [MyClass]
Public Sub HelloWorld()
Console.WriteLine("Hello World")
End Sub
End Class
End Class
The default binder provided with the common language runtime is applicable in all but the most specialized circumstances. If you need a binder that follows rules that differ from those of the supplied default binder, define a type derived from the Binder class and pass an instance of that type using the binder
parameter of one of the InvokeMember overloads.
Reflection models the accessibility rules of the common type system. For example, if the caller is in the same assembly, the caller does not need special permissions for internal members. Otherwise, the caller needs ReflectionPermission. This is consistent with lookup of members that are protected, private, and so on.
The general principle is that ChangeType should perform only widening conversions, which never lose data. An example of a widening conversion is converting a value that is a 32-bit signed integer to a value that is a 64-bit signed integer. This is distinguished from a narrowing conversion, which may lose data. An example of a narrowing conversion is converting a 64-bit signed integer to a 32-bit signed integer.
The following table lists the conversions supported by the default binder.
Source Type | Target Type |
---|---|
Any type | Its base type. |
Any type | The interface it implements. |
Char | Unt16, UInt32, Int32, UInt64, Int64, Single, Double |
Byte | Char, Unt16, Int16, UInt32, Int32, UInt64, Int64, Single, Double |
SByte | Int16, Int32, Int64, Single, Double |
UInt16 | UInt32, Int32, UInt64, Int64, Single, Double |
Int16 | Int32, Int64, Single, Double |
UInt32 | UInt64, Int64, Single, Double |
Int32 | Int64, Single, Double |
UInt64 | Single, Double |
Int64 | Single, Double |
Single | Double |
Non-reference | By-reference. |
Product | Versions |
---|---|
.NET | 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 | 2.0, 2.1 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: