Type.DefaultBinder Propiedad
Definición
Obtiene una referencia al enlazador predeterminado, que implementa varias reglas internas para seleccionar los miembros adecuados a los que llamará el método InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]).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; }
member this.DefaultBinder : System.Reflection.Binder
Public Shared ReadOnly Property DefaultBinder As Binder
Valor de propiedad
Referencia al enlazador predeterminado que el sistema usa.A reference to the default binder used by the system.
Ejemplos
En el ejemplo siguiente se obtiene el enlazador predeterminado de la DefaultBinder
propiedad y se invoca a un miembro de MyClass pasando el DefaultBinder
valor como parámetro a InvokeMember .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");
}
}
}
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
Comentarios
El enlazador predeterminado proporcionado con la Common Language Runtime es aplicable en todas las circunstancias, excepto en las más especializadas.The default binder provided with the common language runtime is applicable in all but the most specialized circumstances. Si necesita un enlazador que siga las reglas que difieren de las del enlazador predeterminado proporcionado, defina un tipo derivado de la Binder clase y pase una instancia de ese tipo mediante el binder
parámetro de una de las InvokeMember sobrecargas.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.
La reflexión modela las reglas de accesibilidad del sistema de tipos comunes.Reflection models the accessibility rules of the common type system. Por ejemplo, si el llamador está en el mismo ensamblado, el autor de la llamada no necesita permisos especiales para los miembros internos.For example, if the caller is in the same assembly, the caller does not need special permissions for internal members. De lo contrario, el autor de la llamada necesita ReflectionPermission .Otherwise, the caller needs ReflectionPermission. Esto es coherente con la búsqueda de miembros que están protegidos, privados, etc.This is consistent with lookup of members that are protected, private, and so on.
El principio general es que ChangeType solo debe realizar conversiones de ampliación, que nunca pierden datos.The general principle is that ChangeType should perform only widening conversions, which never lose data. Un ejemplo de una conversión de ampliación es la conversión de un valor que es un entero con signo de 32 bits en un valor que es un entero de 64 bits con signo.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. Esto se distingue de una conversión de restricción, que puede perder datos.This is distinguished from a narrowing conversion, which may lose data. Un ejemplo de una conversión de restricción consiste en convertir un entero con signo de 64 bits en un entero de 32 bits con signo.An example of a narrowing conversion is converting a 64-bit signed integer to a 32-bit signed integer.
En la tabla siguiente se enumeran las conversiones admitidas por el enlazador predeterminado.The following table lists the conversions supported by the default binder.
Tipo de origenSource Type | Tipo de destinoTarget Type |
---|---|
Cualquier tipoAny type | Su tipo base.Its base type. |
Cualquier tipoAny type | La interfaz que implementa.The interface it implements. |
CharChar | Unt16, UInt32, Int32, UInt64, Int64, single, DoubleUnt16, UInt32, Int32, UInt64, Int64, Single, Double |
ByteByte | Char, Unt16, Int16, UInt32, Int32, UInt64, Int64, single, DoubleChar, Unt16, Int16, UInt32, Int32, UInt64, Int64, Single, Double |
SByteSByte | Int16, Int32, Int64, Single, DoubleInt16, Int32, Int64, Single, Double |
UInt16UInt16 | UInt32, Int32, UInt64, Int64, Single, DoubleUInt32, Int32, UInt64, Int64, Single, Double |
Int16Int16 | Int32, Int64, Single, DoubleInt32, Int64, Single, Double |
UInt32UInt32 | UInt64, Int64, Single, DoubleUInt64, Int64, Single, Double |
Int32Int32 | Int64, Single, DoubleInt64, Single, Double |
UInt64UInt64 | Single, DoubleSingle, Double |
Int64Int64 | Single, DoubleSingle, Double |
SingleSingle | DobleDouble |
No referenciaNon-reference | Por referencia.By-reference. |