Type.DefaultBinder Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets a reference to the default binder, which implements internal rules for selecting the appropriate members to be called by InvokeMember.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Shared ReadOnly Property DefaultBinder As Binder
public static Binder DefaultBinder { get; }

Property Value

Type: System.Reflection.Binder
A reference to the default binder used by the system.

Remarks

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 can access internal members. 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.

Examples

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.

Imports System.Reflection

Public Class Example

    Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

        Dim obj As Object = New Example()

        ' Invoke the Hello method of the Example class, late bound,
        ' using the default binder to locate the method.
        obj.GetType().InvokeMember("Hello", BindingFlags.InvokeMethod, _
            Type.DefaultBinder, obj, New Object() { outputBlock })

    End Sub 

    Public Sub Hello(ByVal outputBlock As System.Windows.Controls.TextBlock)
        outputBlock.Text &= "Hello, World!" & vbLf
    End Sub 
End Class 

' This example produces the following output:
'
'Hello, World!
using System;
using System.Reflection;

public class Example
{
    public static void Demo(System.Windows.Controls.TextBlock outputBlock)
    {
        object obj = new Example();

        // Invoke the Hello method of the Example class, late bound,
        // using the default binder to locate the method.
        obj.GetType().InvokeMember("Hello", BindingFlags.InvokeMethod,
            Type.DefaultBinder, obj, new object[] { outputBlock });
    }

    public void Hello(System.Windows.Controls.TextBlock outputBlock)
    {
        outputBlock.Text += "Hello, World!\n";
    }
}

/* This example produces the following output:

Hello, World!
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference