System.Reflection.Binder Class

Assembly: Mscorlib.dll
Namespace: System.Reflection
Summary
Selects a member from a list of candidates, and performs type conversion from actual argument type to formal argument type.
C# Syntax:
[Serializable]
public abstract class Binder
Remarks


Notes to inheritors: When you inherit from Binder, you must override the following members: Binder.BindToMethod, Binder.BindToField, Binder.SelectMethod, Binder.SelectProperty, and Binder.ChangeType.
Example
using System;
using System.Reflection;
using System.Globalization;



public class MyBinder : Binder 
{

   public MyBinder() : base()
   {

   }

   private class BinderState
   {
      public object[] args;
   }



   public override FieldInfo BindToField(
      BindingFlags bindingAttr,
      FieldInfo[] match,
      object value,
      CultureInfo culture
      )
   {
      if(match == null)
         throw new ArgumentNullException("match");
      
      // Get a field for which 'value' can be converted to that field type.
      for(int i = 0; i < match.Length; i++)
         if(ChangeType(value, match[i].FieldType, culture) != null)
            return match[i];

      return null;
   }




   public override MethodBase BindToMethod(
      BindingFlags bindingAttr,
      MethodBase[] match,
      ref object[] args,
      ParameterModifier[] modifiers,
      CultureInfo culture,
      string[] names,
      out object state
      )
   {
      // Store the arguments to the method in a state object.
      BinderState myBinderState = new BinderState();
      object[] arguments = new Object[args.Length];
      args.CopyTo(arguments, 0);
      myBinderState.args = arguments;
      state = myBinderState;

      if(match == null)
         throw new ArgumentNullException();

      // Find a method that has the same parameters as those of 'args'.
      for(int i = 0; i < match.Length; i++)
      {
         // Count the number of parameters that match.
         int count = 0;
         ParameterInfo[] parameters = match[i].GetParameters();

         // Go on to the next method if the number of parameters don't match.
         if(args.Length != parameters.Length)
            continue;

         // Match each of the parameters that the user expects the method to have.
         for(int j = 0; j < args.Length; j++)
         {
            // If 'names' is not null then reorder 'args'.
            if(names != null)
            {
               if(names.Length != args.Length)
                  throw new ArgumentException("names and args must have the same number of elements");

               for(int k = 0; k < names.Length; k++)
                  if(String.Compare(parameters[j].Name, names[k].ToString()) == 0)
                     args[j] = myBinderState.args[k];
            }

            // Check if the types specified by the user can be converted to parameter type.
            if(ChangeType(args[j], parameters[j].ParameterType, culture) != null)
               count += 1;
            else
               break;
         }

         // Check if the method has been found.
         if(count == args.Length)
            return match[i];
      }
      return null;
   }


   public override object ChangeType(
      object value,
      Type myChangeType,
      CultureInfo culture
      )
   {
      // Check if the 'value' can be converted to a value of type 'myType'.
      if(CanConvertFrom(value.GetType(), myChangeType))
         // Return the converted object.
         return Convert.ChangeType(value, myChangeType);
      else
         // Return null.
         return null;
   }


   public override void ReorderArgumentArray(
      ref object[] args,
      object state
      )
   {
      // Return the args that had been reordered by 'BindToMethod'.
      ((BinderState)state).args.CopyTo(args, 0);
   }


   public override MethodBase SelectMethod(
      BindingFlags bindingAttr,
      MethodBase[] match,
      Type[] types,
      ParameterModifier[] modifiers
      )
   {
      if(match == null)
         throw new ArgumentNullException("match");
      for(int i = 0; i < match.Length; i++)
      {
         // Count the number of parameters that match.
         int count = 0; 
         ParameterInfo[] parameters = match[i].GetParameters();

         // Go on to the next method if the number of parameters don't match.
         if(types.Length != parameters.Length)
            continue;

         // Match each of the parameters that the user expects the method to have.
         for(int j = 0; j < types.Length; j++)
            // Check if the types specified by the user can be converted to parameter type.
            if(CanConvertFrom(types[j], parameters[j].ParameterType))
               count += 1;
            else
               break;

         // Check if the method has been found.
         if(count == types.Length)
            return match[i];
      }
      return null;
   }


   public override PropertyInfo SelectProperty(
      BindingFlags bindingAttr,
      PropertyInfo[] match,
      Type returnType,
      Type[] indexes,
      ParameterModifier[] modifiers
      )
   {
      if(match == null)
         throw new ArgumentNullException("match");

      for(int i = 0; i < match.Length; i++)
      {
         // Count the number of indexes that match.
         int count = 0;
         ParameterInfo[] parameters = match[i].GetIndexParameters();

         // Go on to the next property if the number of indexes don't match.
         if(indexes.Length != parameters.Length)
            continue;

         // Match each of the indexes that the user expects the property to have.
         for(int j = 0; j < indexes.Length; j++)
            // Check if the types specified by the user can be converted to index type.
            if(CanConvertFrom(indexes[j], parameters[j].ParameterType))
               count += 1;
            else
               break;

         // Check if the property has been found.
         if(count == indexes.Length)
            // Check if the return type can be converted to the properties type.
            if(CanConvertFrom(returnType, match[i].GetType()))
               return match[i];
            else
               continue;
      }
      return null;
   }


   // Checks if 'type1' can be converted to 'type2'. (Checks only for primitive types).
   private bool CanConvertFrom(Type type1, Type type2)
   {
      if(type1.IsPrimitive && type2.IsPrimitive)
      {
         TypeCode typeCode1 = Type.GetTypeCode(type1);
         TypeCode typeCode2 = Type.GetTypeCode(type2);
         // If both 'type1' and 'type2' have same type return true.
         if(typeCode1 == typeCode2)
            return true;
         // Possible conversions from 'Char' follow.
         if(typeCode1 == TypeCode.Char)
            switch(typeCode2)
            {
               case TypeCode.UInt16 : return true;
               case TypeCode.UInt32 : return true;
               case TypeCode.Int32  : return true;
               case TypeCode.UInt64 : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Byte' follow.
         if(typeCode1 == TypeCode.Byte)
            switch(typeCode2)
            {
               case TypeCode.Char   : return true;
               case TypeCode.UInt16 : return true;
               case TypeCode.Int16  : return true;
               case TypeCode.UInt32 : return true;
               case TypeCode.Int32  : return true;
               case TypeCode.UInt64 : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'SByte' follow.
         if(typeCode1 == TypeCode.SByte)
            switch(typeCode2)
            {
               case TypeCode.Int16  : return true;
               case TypeCode.Int32  : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'UInt16' follow.
         if(typeCode1 == TypeCode.UInt16)
            switch(typeCode2)
            {
               case TypeCode.UInt32 : return true;
               case TypeCode.Int32  : return true;
               case TypeCode.UInt64 : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Int16' follow.
         if(typeCode1 == TypeCode.Int16)
            switch(typeCode2)
            {
               case TypeCode.Int32  : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'UInt32' follow.
         if(typeCode1 == TypeCode.UInt32)
            switch(typeCode2)
            {
               case TypeCode.UInt64 : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Int32' follow.
         if(typeCode1 == TypeCode.Int32)
            switch(typeCode2)
            {
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'UInt64' follow.
         if(typeCode1 == TypeCode.UInt64)
            switch(typeCode2)
            {
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Int64' follow.
         if(typeCode1 == TypeCode.Int64)
            switch(typeCode2)
            {
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Single' follow.
         if(typeCode1 == TypeCode.Single)
            switch(typeCode2)
            {
               case TypeCode.Double : return true;
               default              : return false;
            }
      }
      return false;
   }
}



public class MyClass1
{
   public short myFieldB;
   public int myFieldA; 
   public void MyMethod(long i, char k)
   {
      Console.WriteLine("\nThis is MyMethod(long i, char k)");
   }
   public void MyMethod(long i, long j)
   {
      Console.WriteLine("\nThis is MyMethod(long i, long j)");
   }
}

public class Binder_Example
{
   public static void Main()
   {
      // Get the type of 'MyClass1'.
      Type myType = typeof(MyClass1);
      // Get the instance of 'MyClass1'.
      MyClass1 myInstance = new MyClass1();
      Console.WriteLine("\nDisplaying results of using 'MyBinder' binder\n");
      // Get the method information for the method 'MyMethod'.
      MethodInfo myMethod = myType.GetMethod("MyMethod", BindingFlags.Public | BindingFlags.Instance,
         new MyBinder(), new Type[] {typeof(short), typeof(short)}, null);
      Console.WriteLine(myMethod);
      // Invoke the method named 'MyMethod'.
      myType.InvokeMember("MyMethod", BindingFlags.InvokeMethod, new MyBinder(),
         myInstance, new Object[] {(int)32, (int)32});
   }
}

    
See also:
System.Reflection Namespace

System.Reflection.Binder Member List:

Public Methods
BindToField Selects a field from the given set of fields, based on the specified criteria.
BindToMethod Selects a method to invoke from the given set of methods, based on the actual arguments.
CanChangeType
ChangeType Changes the type of the given Object to the given Type.
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
ReorderArgumentArray Upon returning from Binder.BindToMethod, restores the args argument to what it was when it came from BindToMethod.
SelectMethod Selects a method from the given set of methods, based on the argument type.
SelectProperty Selects a property from the given set of properties, based on the specified criteria.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Constructors
ctor #1 Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Reflection.Binder Member Details

ctor #1
Summary:
Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
protected Binder();

Return to top


Method: BindToField(
   BindingFlags bindingAttr,
   FieldInfo[] match,
   object value,
   CultureInfo culture
)
Summary
Selects a field from the given set of fields, based on the specified criteria.
C# Syntax:
public abstract FieldInfo BindToField(
   BindingFlags bindingAttr,
   FieldInfo[] match,
   object value,
   CultureInfo culture
);
Parameters:

bindingAttr

One of the BindingFlags enumerators.

match

The set of fields Reflection has determined to be a possible match, typically because they have the correct member name.

value

The field value used to locate a matching field.

culture

An instance of CultureInfo used to control the coercion of data types. If culture is null, the CultureInfo for the current thread is used.

Note For example, this parameter is necessary to convert a String that represents 1000 to a Double value, since 1000 is represented differently by different cultures. An instance of CultureInfo used to control the coercion of data types. If culture is null, the CultureInfo for the current thread is used.

Return Value:
A FieldInfo object containing the matching field.
Remarks
This method controls the binding provided by Type.InvokeMember().
Example
using System;
using System.Reflection;
using System.Globalization;



public class MyBinder : Binder 
{

   public MyBinder() : base()
   {

   }

   private class BinderState
   {
      public object[] args;
   }



   public override FieldInfo BindToField(
      BindingFlags bindingAttr,
      FieldInfo[] match,
      object value,
      CultureInfo culture
      )
   {
      if(match == null)
         throw new ArgumentNullException("match");
      
      // Get a field for which 'value' can be converted to that field type.
      for(int i = 0; i < match.Length; i++)
         if(ChangeType(value, match[i].FieldType, culture) != null)
            return match[i];

      return null;
   }




   public override MethodBase BindToMethod(
      BindingFlags bindingAttr,
      MethodBase[] match,
      ref object[] args,
      ParameterModifier[] modifiers,
      CultureInfo culture,
      string[] names,
      out object state
      )
   {
      // Store the arguments to the method in a state object.
      BinderState myBinderState = new BinderState();
      object[] arguments = new Object[args.Length];
      args.CopyTo(arguments, 0);
      myBinderState.args = arguments;
      state = myBinderState;

      if(match == null)
         throw new ArgumentNullException();

      // Find a method that has the same parameters as those of 'args'.
      for(int i = 0; i < match.Length; i++)
      {
         // Count the number of parameters that match.
         int count = 0;
         ParameterInfo[] parameters = match[i].GetParameters();

         // Go on to the next method if the number of parameters don't match.
         if(args.Length != parameters.Length)
            continue;

         // Match each of the parameters that the user expects the method to have.
         for(int j = 0; j < args.Length; j++)
         {
            // If 'names' is not null then reorder 'args'.
            if(names != null)
            {
               if(names.Length != args.Length)
                  throw new ArgumentException("names and args must have the same number of elements");

               for(int k = 0; k < names.Length; k++)
                  if(String.Compare(parameters[j].Name, names[k].ToString()) == 0)
                     args[j] = myBinderState.args[k];
            }

            // Check if the types specified by the user can be converted to parameter type.
            if(ChangeType(args[j], parameters[j].ParameterType, culture) != null)
               count += 1;
            else
               break;
         }

         // Check if the method has been found.
         if(count == args.Length)
            return match[i];
      }
      return null;
   }


   public override object ChangeType(
      object value,
      Type myChangeType,
      CultureInfo culture
      )
   {
      // Check if the 'value' can be converted to a value of type 'myType'.
      if(CanConvertFrom(value.GetType(), myChangeType))
         // Return the converted object.
         return Convert.ChangeType(value, myChangeType);
      else
         // Return null.
         return null;
   }


   public override void ReorderArgumentArray(
      ref object[] args,
      object state
      )
   {
      // Return the args that had been reordered by 'BindToMethod'.
      ((BinderState)state).args.CopyTo(args, 0);
   }


   public override MethodBase SelectMethod(
      BindingFlags bindingAttr,
      MethodBase[] match,
      Type[] types,
      ParameterModifier[] modifiers
      )
   {
      if(match == null)
         throw new ArgumentNullException("match");
      for(int i = 0; i < match.Length; i++)
      {
         // Count the number of parameters that match.
         int count = 0; 
         ParameterInfo[] parameters = match[i].GetParameters();

         // Go on to the next method if the number of parameters don't match.
         if(types.Length != parameters.Length)
            continue;

         // Match each of the parameters that the user expects the method to have.
         for(int j = 0; j < types.Length; j++)
            // Check if the types specified by the user can be converted to parameter type.
            if(CanConvertFrom(types[j], parameters[j].ParameterType))
               count += 1;
            else
               break;

         // Check if the method has been found.
         if(count == types.Length)
            return match[i];
      }
      return null;
   }


   public override PropertyInfo SelectProperty(
      BindingFlags bindingAttr,
      PropertyInfo[] match,
      Type returnType,
      Type[] indexes,
      ParameterModifier[] modifiers
      )
   {
      if(match == null)
         throw new ArgumentNullException("match");

      for(int i = 0; i < match.Length; i++)
      {
         // Count the number of indexes that match.
         int count = 0;
         ParameterInfo[] parameters = match[i].GetIndexParameters();

         // Go on to the next property if the number of indexes don't match.
         if(indexes.Length != parameters.Length)
            continue;

         // Match each of the indexes that the user expects the property to have.
         for(int j = 0; j < indexes.Length; j++)
            // Check if the types specified by the user can be converted to index type.
            if(CanConvertFrom(indexes[j], parameters[j].ParameterType))
               count += 1;
            else
               break;

         // Check if the property has been found.
         if(count == indexes.Length)
            // Check if the return type can be converted to the properties type.
            if(CanConvertFrom(returnType, match[i].GetType()))
               return match[i];
            else
               continue;
      }
      return null;
   }


   // Checks if 'type1' can be converted to 'type2'. (Checks only for primitive types).
   private bool CanConvertFrom(Type type1, Type type2)
   {
      if(type1.IsPrimitive && type2.IsPrimitive)
      {
         TypeCode typeCode1 = Type.GetTypeCode(type1);
         TypeCode typeCode2 = Type.GetTypeCode(type2);
         // If both 'type1' and 'type2' have same type return true.
         if(typeCode1 == typeCode2)
            return true;
         // Possible conversions from 'Char' follow.
         if(typeCode1 == TypeCode.Char)
            switch(typeCode2)
            {
               case TypeCode.UInt16 : return true;
               case TypeCode.UInt32 : return true;
               case TypeCode.Int32  : return true;
               case TypeCode.UInt64 : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Byte' follow.
         if(typeCode1 == TypeCode.Byte)
            switch(typeCode2)
            {
               case TypeCode.Char   : return true;
               case TypeCode.UInt16 : return true;
               case TypeCode.Int16  : return true;
               case TypeCode.UInt32 : return true;
               case TypeCode.Int32  : return true;
               case TypeCode.UInt64 : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'SByte' follow.
         if(typeCode1 == TypeCode.SByte)
            switch(typeCode2)
            {
               case TypeCode.Int16  : return true;
               case TypeCode.Int32  : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'UInt16' follow.
         if(typeCode1 == TypeCode.UInt16)
            switch(typeCode2)
            {
               case TypeCode.UInt32 : return true;
               case TypeCode.Int32  : return true;
               case TypeCode.UInt64 : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Int16' follow.
         if(typeCode1 == TypeCode.Int16)
            switch(typeCode2)
            {
               case TypeCode.Int32  : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'UInt32' follow.
         if(typeCode1 == TypeCode.UInt32)
            switch(typeCode2)
            {
               case TypeCode.UInt64 : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Int32' follow.
         if(typeCode1 == TypeCode.Int32)
            switch(typeCode2)
            {
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'UInt64' follow.
         if(typeCode1 == TypeCode.UInt64)
            switch(typeCode2)
            {
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Int64' follow.
         if(typeCode1 == TypeCode.Int64)
            switch(typeCode2)
            {
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Single' follow.
         if(typeCode1 == TypeCode.Single)
            switch(typeCode2)
            {
               case TypeCode.Double : return true;
               default              : return false;
            }
      }
      return false;
   }
}



public class MyClass1
{
   public short myFieldB;
   public int myFieldA; 
   public void MyMethod(long i, char k)
   {
      Console.WriteLine("\nThis is MyMethod(long i, char k)");
   }
   public void MyMethod(long i, long j)
   {
      Console.WriteLine("\nThis is MyMethod(long i, long j)");
   }
}

public class Binder_Example
{
   public static void Main()
   {
      // Get the type of 'MyClass1'.
      Type myType = typeof(MyClass1);
      // Get the instance of 'MyClass1'.
      MyClass1 myInstance = new MyClass1();
      Console.WriteLine("\nDisplaying results of using 'MyBinder' binder\n");
      // Get the method information for the method 'MyMethod'.
      MethodInfo myMethod = myType.GetMethod("MyMethod", BindingFlags.Public | BindingFlags.Instance,
         new MyBinder(), new Type[] {typeof(short), typeof(short)}, null);
      Console.WriteLine(myMethod);
      // Invoke the method named 'MyMethod'.
      myType.InvokeMember("MyMethod", BindingFlags.InvokeMethod, new MyBinder(),
         myInstance, new Object[] {(int)32, (int)32});
   }
}

    
See also:
FieldInfo

Return to top


Method: BindToMethod(
   BindingFlags bindingAttr,
   MethodBase[] match,
   ref object[] args,
   ParameterModifier[] modifiers,
   CultureInfo culture,
   string[] names,
   out object state
)
Summary
Selects a method to invoke from the given set of methods, based on the actual arguments.
C# Syntax:
public abstract MethodBase BindToMethod(
   BindingFlags bindingAttr,
   MethodBase[] match,
   ref object[] args,
   ParameterModifier[] modifiers,
   CultureInfo culture,
   string[] names,
   out object state
);
Parameters:

bindingAttr

One of the BindingFlags enumerators.

match

The set of methods Reflection has determined to be a possible match, typically because they have the correct member name.

args

The actual arguments passed in. Both the types and values of the arguments can be changed.

modifiers

An array of parameter modifiers that enable binding to work with parameter signatures in which the types have been modified.

culture

An instance of CultureInfo used to control the coercion of data types. If culture is null, the CultureInfo for the current thread is used.

Note For example, this parameter is necessary to convert a String that represents 1000 to a Double value, since 1000 is represented differently by different cultures.

Not Yet Implemented.

An instance of CultureInfo used to control the coercion of data types. If culture is null, the CultureInfo for the current thread is used.

Not Yet Implemented.

names

The method name or names.

state

A binder-provided object that keeps track of argument reordering. The state parameter is a cookie that was passed to BindToMethod and represents an opaque object. The binder creates this object, and the binder is the sole consumer of this object. If state is not null when BindToMethod returns, the runtime calls Binder.ReorderArgumentArray.

Return Value:
A MethodBase object containing the matching method.
Remarks
The binder allows a client to map the array of arguments back to its original form if the argument array has been manipulated by BindToMethod. Use this remap capability to get back by-reference arguments when such arguments are present. However, to get back by-reference arguments, you must be able to ensure that the argument order you used has not changed. When you pass arguments by name, the binder reorders the argument array, and that is what the calling methods see. The state parameter keeps track of argument reordering, thus enabling the binder to reorder the argument array to its original form.
Example
using System;
using System.Reflection;
using System.Globalization;



public class MyBinder : Binder 
{

   public MyBinder() : base()
   {

   }

   private class BinderState
   {
      public object[] args;
   }



   public override FieldInfo BindToField(
      BindingFlags bindingAttr,
      FieldInfo[] match,
      object value,
      CultureInfo culture
      )
   {
      if(match == null)
         throw new ArgumentNullException("match");
      
      // Get a field for which 'value' can be converted to that field type.
      for(int i = 0; i < match.Length; i++)
         if(ChangeType(value, match[i].FieldType, culture) != null)
            return match[i];

      return null;
   }




   public override MethodBase BindToMethod(
      BindingFlags bindingAttr,
      MethodBase[] match,
      ref object[] args,
      ParameterModifier[] modifiers,
      CultureInfo culture,
      string[] names,
      out object state
      )
   {
      // Store the arguments to the method in a state object.
      BinderState myBinderState = new BinderState();
      object[] arguments = new Object[args.Length];
      args.CopyTo(arguments, 0);
      myBinderState.args = arguments;
      state = myBinderState;

      if(match == null)
         throw new ArgumentNullException();

      // Find a method that has the same parameters as those of 'args'.
      for(int i = 0; i < match.Length; i++)
      {
         // Count the number of parameters that match.
         int count = 0;
         ParameterInfo[] parameters = match[i].GetParameters();

         // Go on to the next method if the number of parameters don't match.
         if(args.Length != parameters.Length)
            continue;

         // Match each of the parameters that the user expects the method to have.
         for(int j = 0; j < args.Length; j++)
         {
            // If 'names' is not null then reorder 'args'.
            if(names != null)
            {
               if(names.Length != args.Length)
                  throw new ArgumentException("names and args must have the same number of elements");

               for(int k = 0; k < names.Length; k++)
                  if(String.Compare(parameters[j].Name, names[k].ToString()) == 0)
                     args[j] = myBinderState.args[k];
            }

            // Check if the types specified by the user can be converted to parameter type.
            if(ChangeType(args[j], parameters[j].ParameterType, culture) != null)
               count += 1;
            else
               break;
         }

         // Check if the method has been found.
         if(count == args.Length)
            return match[i];
      }
      return null;
   }


   public override object ChangeType(
      object value,
      Type myChangeType,
      CultureInfo culture
      )
   {
      // Check if the 'value' can be converted to a value of type 'myType'.
      if(CanConvertFrom(value.GetType(), myChangeType))
         // Return the converted object.
         return Convert.ChangeType(value, myChangeType);
      else
         // Return null.
         return null;
   }


   public override void ReorderArgumentArray(
      ref object[] args,
      object state
      )
   {
      // Return the args that had been reordered by 'BindToMethod'.
      ((BinderState)state).args.CopyTo(args, 0);
   }


   public override MethodBase SelectMethod(
      BindingFlags bindingAttr,
      MethodBase[] match,
      Type[] types,
      ParameterModifier[] modifiers
      )
   {
      if(match == null)
         throw new ArgumentNullException("match");
      for(int i = 0; i < match.Length; i++)
      {
         // Count the number of parameters that match.
         int count = 0; 
         ParameterInfo[] parameters = match[i].GetParameters();

         // Go on to the next method if the number of parameters don't match.
         if(types.Length != parameters.Length)
            continue;

         // Match each of the parameters that the user expects the method to have.
         for(int j = 0; j < types.Length; j++)
            // Check if the types specified by the user can be converted to parameter type.
            if(CanConvertFrom(types[j], parameters[j].ParameterType))
               count += 1;
            else
               break;

         // Check if the method has been found.
         if(count == types.Length)
            return match[i];
      }
      return null;
   }


   public override PropertyInfo SelectProperty(
      BindingFlags bindingAttr,
      PropertyInfo[] match,
      Type returnType,
      Type[] indexes,
      ParameterModifier[] modifiers
      )
   {
      if(match == null)
         throw new ArgumentNullException("match");

      for(int i = 0; i < match.Length; i++)
      {
         // Count the number of indexes that match.
         int count = 0;
         ParameterInfo[] parameters = match[i].GetIndexParameters();

         // Go on to the next property if the number of indexes don't match.
         if(indexes.Length != parameters.Length)
            continue;

         // Match each of the indexes that the user expects the property to have.
         for(int j = 0; j < indexes.Length; j++)
            // Check if the types specified by the user can be converted to index type.
            if(CanConvertFrom(indexes[j], parameters[j].ParameterType))
               count += 1;
            else
               break;

         // Check if the property has been found.
         if(count == indexes.Length)
            // Check if the return type can be converted to the properties type.
            if(CanConvertFrom(returnType, match[i].GetType()))
               return match[i];
            else
               continue;
      }
      return null;
   }


   // Checks if 'type1' can be converted to 'type2'. (Checks only for primitive types).
   private bool CanConvertFrom(Type type1, Type type2)
   {
      if(type1.IsPrimitive && type2.IsPrimitive)
      {
         TypeCode typeCode1 = Type.GetTypeCode(type1);
         TypeCode typeCode2 = Type.GetTypeCode(type2);
         // If both 'type1' and 'type2' have same type return true.
         if(typeCode1 == typeCode2)
            return true;
         // Possible conversions from 'Char' follow.
         if(typeCode1 == TypeCode.Char)
            switch(typeCode2)
            {
               case TypeCode.UInt16 : return true;
               case TypeCode.UInt32 : return true;
               case TypeCode.Int32  : return true;
               case TypeCode.UInt64 : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Byte' follow.
         if(typeCode1 == TypeCode.Byte)
            switch(typeCode2)
            {
               case TypeCode.Char   : return true;
               case TypeCode.UInt16 : return true;
               case TypeCode.Int16  : return true;
               case TypeCode.UInt32 : return true;
               case TypeCode.Int32  : return true;
               case TypeCode.UInt64 : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'SByte' follow.
         if(typeCode1 == TypeCode.SByte)
            switch(typeCode2)
            {
               case TypeCode.Int16  : return true;
               case TypeCode.Int32  : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'UInt16' follow.
         if(typeCode1 == TypeCode.UInt16)
            switch(typeCode2)
            {
               case TypeCode.UInt32 : return true;
               case TypeCode.Int32  : return true;
               case TypeCode.UInt64 : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Int16' follow.
         if(typeCode1 == TypeCode.Int16)
            switch(typeCode2)
            {
               case TypeCode.Int32  : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'UInt32' follow.
         if(typeCode1 == TypeCode.UInt32)
            switch(typeCode2)
            {
               case TypeCode.UInt64 : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Int32' follow.
         if(typeCode1 == TypeCode.Int32)
            switch(typeCode2)
            {
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'UInt64' follow.
         if(typeCode1 == TypeCode.UInt64)
            switch(typeCode2)
            {
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Int64' follow.
         if(typeCode1 == TypeCode.Int64)
            switch(typeCode2)
            {
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Single' follow.
         if(typeCode1 == TypeCode.Single)
            switch(typeCode2)
            {
               case TypeCode.Double : return true;
               default              : return false;
            }
      }
      return false;
   }
}



public class MyClass1
{
   public short myFieldB;
   public int myFieldA; 
   public void MyMethod(long i, char k)
   {
      Console.WriteLine("\nThis is MyMethod(long i, char k)");
   }
   public void MyMethod(long i, long j)
   {
      Console.WriteLine("\nThis is MyMethod(long i, long j)");
   }
}

public class Binder_Example
{
   public static void Main()
   {
      // Get the type of 'MyClass1'.
      Type myType = typeof(MyClass1);
      // Get the instance of 'MyClass1'.
      MyClass1 myInstance = new MyClass1();
      Console.WriteLine("\nDisplaying results of using 'MyBinder' binder\n");
      // Get the method information for the method 'MyMethod'.
      MethodInfo myMethod = myType.GetMethod("MyMethod", BindingFlags.Public | BindingFlags.Instance,
         new MyBinder(), new Type[] {typeof(short), typeof(short)}, null);
      Console.WriteLine(myMethod);
      // Invoke the method named 'MyMethod'.
      myType.InvokeMember("MyMethod", BindingFlags.InvokeMethod, new MyBinder(),
         myInstance, new Object[] {(int)32, (int)32});
   }
}

    
See also:
MethodBase

Return to top


Method: CanChangeType(
   object value,
   Type type,
   CultureInfo culture
)
C# Syntax:
public virtual bool CanChangeType(
   object value,
   Type type,
   CultureInfo culture
);
Parameters:

value

type

culture

Return to top


Method: ChangeType(
   object value,
   Type type,
   CultureInfo culture
)
Summary
Changes the type of the given Object to the given Type.
C# Syntax:
public abstract object ChangeType(
   object value,
   Type type,
   CultureInfo culture
);
Parameters:

value

The value to change into a new Type.

type

The new Type that value will become.

culture

An instance of CultureInfo used to control the coercion of data types. If culture is null, the CultureInfo for the current thread is used.

Note For example, this parameter is necessary to convert a String that represents 1000 to a Double value, since 1000 is represented differently by different cultures. An instance of CultureInfo used to control the coercion of data types. If culture is null, the CultureInfo for the current thread is used.

Return Value:
An Object containing the given value as the new type.
Remarks
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 coercions, which never lose data. An example of a widening coercion is coercing 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 coercion, which may lose data. An example of a narrowing coercion is coercing a 64-bit signed integer to a 32-bit signed integer.

The following table lists the coercions performed by the default ChangeType.



Source Type Target Type
Any type Its base type.
Any type The interface it implements.
Char UInt16, UInt32, Int32, UInt64, Int64, Single, Double
Byte Char, UInt16, 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.
Example
using System;
using System.Reflection;
using System.Globalization;



public class MyBinder : Binder 
{

   public MyBinder() : base()
   {

   }

   private class BinderState
   {
      public object[] args;
   }



   public override FieldInfo BindToField(
      BindingFlags bindingAttr,
      FieldInfo[] match,
      object value,
      CultureInfo culture
      )
   {
      if(match == null)
         throw new ArgumentNullException("match");
      
      // Get a field for which 'value' can be converted to that field type.
      for(int i = 0; i < match.Length; i++)
         if(ChangeType(value, match[i].FieldType, culture) != null)
            return match[i];

      return null;
   }




   public override MethodBase BindToMethod(
      BindingFlags bindingAttr,
      MethodBase[] match,
      ref object[] args,
      ParameterModifier[] modifiers,
      CultureInfo culture,
      string[] names,
      out object state
      )
   {
      // Store the arguments to the method in a state object.
      BinderState myBinderState = new BinderState();
      object[] arguments = new Object[args.Length];
      args.CopyTo(arguments, 0);
      myBinderState.args = arguments;
      state = myBinderState;

      if(match == null)
         throw new ArgumentNullException();

      // Find a method that has the same parameters as those of 'args'.
      for(int i = 0; i < match.Length; i++)
      {
         // Count the number of parameters that match.
         int count = 0;
         ParameterInfo[] parameters = match[i].GetParameters();

         // Go on to the next method if the number of parameters don't match.
         if(args.Length != parameters.Length)
            continue;

         // Match each of the parameters that the user expects the method to have.
         for(int j = 0; j < args.Length; j++)
         {
            // If 'names' is not null then reorder 'args'.
            if(names != null)
            {
               if(names.Length != args.Length)
                  throw new ArgumentException("names and args must have the same number of elements");

               for(int k = 0; k < names.Length; k++)
                  if(String.Compare(parameters[j].Name, names[k].ToString()) == 0)
                     args[j] = myBinderState.args[k];
            }

            // Check if the types specified by the user can be converted to parameter type.
            if(ChangeType(args[j], parameters[j].ParameterType, culture) != null)
               count += 1;
            else
               break;
         }

         // Check if the method has been found.
         if(count == args.Length)
            return match[i];
      }
      return null;
   }


   public override object ChangeType(
      object value,
      Type myChangeType,
      CultureInfo culture
      )
   {
      // Check if the 'value' can be converted to a value of type 'myType'.
      if(CanConvertFrom(value.GetType(), myChangeType))
         // Return the converted object.
         return Convert.ChangeType(value, myChangeType);
      else
         // Return null.
         return null;
   }


   public override void ReorderArgumentArray(
      ref object[] args,
      object state
      )
   {
      // Return the args that had been reordered by 'BindToMethod'.
      ((BinderState)state).args.CopyTo(args, 0);
   }


   public override MethodBase SelectMethod(
      BindingFlags bindingAttr,
      MethodBase[] match,
      Type[] types,
      ParameterModifier[] modifiers
      )
   {
      if(match == null)
         throw new ArgumentNullException("match");
      for(int i = 0; i < match.Length; i++)
      {
         // Count the number of parameters that match.
         int count = 0; 
         ParameterInfo[] parameters = match[i].GetParameters();

         // Go on to the next method if the number of parameters don't match.
         if(types.Length != parameters.Length)
            continue;

         // Match each of the parameters that the user expects the method to have.
         for(int j = 0; j < types.Length; j++)
            // Check if the types specified by the user can be converted to parameter type.
            if(CanConvertFrom(types[j], parameters[j].ParameterType))
               count += 1;
            else
               break;

         // Check if the method has been found.
         if(count == types.Length)
            return match[i];
      }
      return null;
   }


   public override PropertyInfo SelectProperty(
      BindingFlags bindingAttr,
      PropertyInfo[] match,
      Type returnType,
      Type[] indexes,
      ParameterModifier[] modifiers
      )
   {
      if(match == null)
         throw new ArgumentNullException("match");

      for(int i = 0; i < match.Length; i++)
      {
         // Count the number of indexes that match.
         int count = 0;
         ParameterInfo[] parameters = match[i].GetIndexParameters();

         // Go on to the next property if the number of indexes don't match.
         if(indexes.Length != parameters.Length)
            continue;

         // Match each of the indexes that the user expects the property to have.
         for(int j = 0; j < indexes.Length; j++)
            // Check if the types specified by the user can be converted to index type.
            if(CanConvertFrom(indexes[j], parameters[j].ParameterType))
               count += 1;
            else
               break;

         // Check if the property has been found.
         if(count == indexes.Length)
            // Check if the return type can be converted to the properties type.
            if(CanConvertFrom(returnType, match[i].GetType()))
               return match[i];
            else
               continue;
      }
      return null;
   }


   // Checks if 'type1' can be converted to 'type2'. (Checks only for primitive types).
   private bool CanConvertFrom(Type type1, Type type2)
   {
      if(type1.IsPrimitive && type2.IsPrimitive)
      {
         TypeCode typeCode1 = Type.GetTypeCode(type1);
         TypeCode typeCode2 = Type.GetTypeCode(type2);
         // If both 'type1' and 'type2' have same type return true.
         if(typeCode1 == typeCode2)
            return true;
         // Possible conversions from 'Char' follow.
         if(typeCode1 == TypeCode.Char)
            switch(typeCode2)
            {
               case TypeCode.UInt16 : return true;
               case TypeCode.UInt32 : return true;
               case TypeCode.Int32  : return true;
               case TypeCode.UInt64 : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Byte' follow.
         if(typeCode1 == TypeCode.Byte)
            switch(typeCode2)
            {
               case TypeCode.Char   : return true;
               case TypeCode.UInt16 : return true;
               case TypeCode.Int16  : return true;
               case TypeCode.UInt32 : return true;
               case TypeCode.Int32  : return true;
               case TypeCode.UInt64 : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'SByte' follow.
         if(typeCode1 == TypeCode.SByte)
            switch(typeCode2)
            {
               case TypeCode.Int16  : return true;
               case TypeCode.Int32  : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'UInt16' follow.
         if(typeCode1 == TypeCode.UInt16)
            switch(typeCode2)
            {
               case TypeCode.UInt32 : return true;
               case TypeCode.Int32  : return true;
               case TypeCode.UInt64 : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Int16' follow.
         if(typeCode1 == TypeCode.Int16)
            switch(typeCode2)
            {
               case TypeCode.Int32  : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'UInt32' follow.
         if(typeCode1 == TypeCode.UInt32)
            switch(typeCode2)
            {
               case TypeCode.UInt64 : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Int32' follow.
         if(typeCode1 == TypeCode.Int32)
            switch(typeCode2)
            {
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'UInt64' follow.
         if(typeCode1 == TypeCode.UInt64)
            switch(typeCode2)
            {
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Int64' follow.
         if(typeCode1 == TypeCode.Int64)
            switch(typeCode2)
            {
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Single' follow.
         if(typeCode1 == TypeCode.Single)
            switch(typeCode2)
            {
               case TypeCode.Double : return true;
               default              : return false;
            }
      }
      return false;
   }
}



public class MyClass1
{
   public short myFieldB;
   public int myFieldA; 
   public void MyMethod(long i, char k)
   {
      Console.WriteLine("\nThis is MyMethod(long i, char k)");
   }
   public void MyMethod(long i, long j)
   {
      Console.WriteLine("\nThis is MyMethod(long i, long j)");
   }
}

public class Binder_Example
{
   public static void Main()
   {
      // Get the type of 'MyClass1'.
      Type myType = typeof(MyClass1);
      // Get the instance of 'MyClass1'.
      MyClass1 myInstance = new MyClass1();
      Console.WriteLine("\nDisplaying results of using 'MyBinder' binder\n");
      // Get the method information for the method 'MyMethod'.
      MethodInfo myMethod = myType.GetMethod("MyMethod", BindingFlags.Public | BindingFlags.Instance,
         new MyBinder(), new Type[] {typeof(short), typeof(short)}, null);
      Console.WriteLine(myMethod);
      // Invoke the method named 'MyMethod'.
      myType.InvokeMember("MyMethod", BindingFlags.InvokeMethod, new MyBinder(),
         myInstance, new Object[] {(int)32, (int)32});
   }
}

    

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~Binder();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ReorderArgumentArray(
   ref object[] args,
   object state
)
Summary
Upon returning from Binder.BindToMethod, restores the args argument to what it was when it came from BindToMethod.
C# Syntax:
public abstract void ReorderArgumentArray(
   ref object[] args,
   object state
);
Parameters:

args

The actual arguments passed in. Both the types and values of the arguments can be changed.

state

A binder-provided object that keeps track of argument reordering.

Remarks
The common language runtime calls this method if state is not null after a return from BindToMethod.
Example
using System;
using System.Reflection;
using System.Globalization;



public class MyBinder : Binder 
{

   public MyBinder() : base()
   {

   }

   private class BinderState
   {
      public object[] args;
   }



   public override FieldInfo BindToField(
      BindingFlags bindingAttr,
      FieldInfo[] match,
      object value,
      CultureInfo culture
      )
   {
      if(match == null)
         throw new ArgumentNullException("match");
      
      // Get a field for which 'value' can be converted to that field type.
      for(int i = 0; i < match.Length; i++)
         if(ChangeType(value, match[i].FieldType, culture) != null)
            return match[i];

      return null;
   }




   public override MethodBase BindToMethod(
      BindingFlags bindingAttr,
      MethodBase[] match,
      ref object[] args,
      ParameterModifier[] modifiers,
      CultureInfo culture,
      string[] names,
      out object state
      )
   {
      // Store the arguments to the method in a state object.
      BinderState myBinderState = new BinderState();
      object[] arguments = new Object[args.Length];
      args.CopyTo(arguments, 0);
      myBinderState.args = arguments;
      state = myBinderState;

      if(match == null)
         throw new ArgumentNullException();

      // Find a method that has the same parameters as those of 'args'.
      for(int i = 0; i < match.Length; i++)
      {
         // Count the number of parameters that match.
         int count = 0;
         ParameterInfo[] parameters = match[i].GetParameters();

         // Go on to the next method if the number of parameters don't match.
         if(args.Length != parameters.Length)
            continue;

         // Match each of the parameters that the user expects the method to have.
         for(int j = 0; j < args.Length; j++)
         {
            // If 'names' is not null then reorder 'args'.
            if(names != null)
            {
               if(names.Length != args.Length)
                  throw new ArgumentException("names and args must have the same number of elements");

               for(int k = 0; k < names.Length; k++)
                  if(String.Compare(parameters[j].Name, names[k].ToString()) == 0)
                     args[j] = myBinderState.args[k];
            }

            // Check if the types specified by the user can be converted to parameter type.
            if(ChangeType(args[j], parameters[j].ParameterType, culture) != null)
               count += 1;
            else
               break;
         }

         // Check if the method has been found.
         if(count == args.Length)
            return match[i];
      }
      return null;
   }


   public override object ChangeType(
      object value,
      Type myChangeType,
      CultureInfo culture
      )
   {
      // Check if the 'value' can be converted to a value of type 'myType'.
      if(CanConvertFrom(value.GetType(), myChangeType))
         // Return the converted object.
         return Convert.ChangeType(value, myChangeType);
      else
         // Return null.
         return null;
   }


   public override void ReorderArgumentArray(
      ref object[] args,
      object state
      )
   {
      // Return the args that had been reordered by 'BindToMethod'.
      ((BinderState)state).args.CopyTo(args, 0);
   }


   public override MethodBase SelectMethod(
      BindingFlags bindingAttr,
      MethodBase[] match,
      Type[] types,
      ParameterModifier[] modifiers
      )
   {
      if(match == null)
         throw new ArgumentNullException("match");
      for(int i = 0; i < match.Length; i++)
      {
         // Count the number of parameters that match.
         int count = 0; 
         ParameterInfo[] parameters = match[i].GetParameters();

         // Go on to the next method if the number of parameters don't match.
         if(types.Length != parameters.Length)
            continue;

         // Match each of the parameters that the user expects the method to have.
         for(int j = 0; j < types.Length; j++)
            // Check if the types specified by the user can be converted to parameter type.
            if(CanConvertFrom(types[j], parameters[j].ParameterType))
               count += 1;
            else
               break;

         // Check if the method has been found.
         if(count == types.Length)
            return match[i];
      }
      return null;
   }


   public override PropertyInfo SelectProperty(
      BindingFlags bindingAttr,
      PropertyInfo[] match,
      Type returnType,
      Type[] indexes,
      ParameterModifier[] modifiers
      )
   {
      if(match == null)
         throw new ArgumentNullException("match");

      for(int i = 0; i < match.Length; i++)
      {
         // Count the number of indexes that match.
         int count = 0;
         ParameterInfo[] parameters = match[i].GetIndexParameters();

         // Go on to the next property if the number of indexes don't match.
         if(indexes.Length != parameters.Length)
            continue;

         // Match each of the indexes that the user expects the property to have.
         for(int j = 0; j < indexes.Length; j++)
            // Check if the types specified by the user can be converted to index type.
            if(CanConvertFrom(indexes[j], parameters[j].ParameterType))
               count += 1;
            else
               break;

         // Check if the property has been found.
         if(count == indexes.Length)
            // Check if the return type can be converted to the properties type.
            if(CanConvertFrom(returnType, match[i].GetType()))
               return match[i];
            else
               continue;
      }
      return null;
   }


   // Checks if 'type1' can be converted to 'type2'. (Checks only for primitive types).
   private bool CanConvertFrom(Type type1, Type type2)
   {
      if(type1.IsPrimitive && type2.IsPrimitive)
      {
         TypeCode typeCode1 = Type.GetTypeCode(type1);
         TypeCode typeCode2 = Type.GetTypeCode(type2);
         // If both 'type1' and 'type2' have same type return true.
         if(typeCode1 == typeCode2)
            return true;
         // Possible conversions from 'Char' follow.
         if(typeCode1 == TypeCode.Char)
            switch(typeCode2)
            {
               case TypeCode.UInt16 : return true;
               case TypeCode.UInt32 : return true;
               case TypeCode.Int32  : return true;
               case TypeCode.UInt64 : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Byte' follow.
         if(typeCode1 == TypeCode.Byte)
            switch(typeCode2)
            {
               case TypeCode.Char   : return true;
               case TypeCode.UInt16 : return true;
               case TypeCode.Int16  : return true;
               case TypeCode.UInt32 : return true;
               case TypeCode.Int32  : return true;
               case TypeCode.UInt64 : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'SByte' follow.
         if(typeCode1 == TypeCode.SByte)
            switch(typeCode2)
            {
               case TypeCode.Int16  : return true;
               case TypeCode.Int32  : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'UInt16' follow.
         if(typeCode1 == TypeCode.UInt16)
            switch(typeCode2)
            {
               case TypeCode.UInt32 : return true;
               case TypeCode.Int32  : return true;
               case TypeCode.UInt64 : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Int16' follow.
         if(typeCode1 == TypeCode.Int16)
            switch(typeCode2)
            {
               case TypeCode.Int32  : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'UInt32' follow.
         if(typeCode1 == TypeCode.UInt32)
            switch(typeCode2)
            {
               case TypeCode.UInt64 : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Int32' follow.
         if(typeCode1 == TypeCode.Int32)
            switch(typeCode2)
            {
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'UInt64' follow.
         if(typeCode1 == TypeCode.UInt64)
            switch(typeCode2)
            {
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Int64' follow.
         if(typeCode1 == TypeCode.Int64)
            switch(typeCode2)
            {
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Single' follow.
         if(typeCode1 == TypeCode.Single)
            switch(typeCode2)
            {
               case TypeCode.Double : return true;
               default              : return false;
            }
      }
      return false;
   }
}



public class MyClass1
{
   public short myFieldB;
   public int myFieldA; 
   public void MyMethod(long i, char k)
   {
      Console.WriteLine("\nThis is MyMethod(long i, char k)");
   }
   public void MyMethod(long i, long j)
   {
      Console.WriteLine("\nThis is MyMethod(long i, long j)");
   }
}

public class Binder_Example
{
   public static void Main()
   {
      // Get the type of 'MyClass1'.
      Type myType = typeof(MyClass1);
      // Get the instance of 'MyClass1'.
      MyClass1 myInstance = new MyClass1();
      Console.WriteLine("\nDisplaying results of using 'MyBinder' binder\n");
      // Get the method information for the method 'MyMethod'.
      MethodInfo myMethod = myType.GetMethod("MyMethod", BindingFlags.Public | BindingFlags.Instance,
         new MyBinder(), new Type[] {typeof(short), typeof(short)}, null);
      Console.WriteLine(myMethod);
      // Invoke the method named 'MyMethod'.
      myType.InvokeMember("MyMethod", BindingFlags.InvokeMethod, new MyBinder(),
         myInstance, new Object[] {(int)32, (int)32});
   }
}

    

Return to top


Method: SelectMethod(
   BindingFlags bindingAttr,
   MethodBase[] match,
   Type[] types,
   ParameterModifier[] modifiers
)
Summary
Selects a method from the given set of methods, based on the argument type.
C# Syntax:
public abstract MethodBase SelectMethod(
   BindingFlags bindingAttr,
   MethodBase[] match,
   Type[] types,
   ParameterModifier[] modifiers
);
Parameters:

bindingAttr

One of the BindingFlags enumerators.

match

The set of methods Reflection has determined to be a possible match, typically because they have the correct member name.

types

The value used to locate a matching method.

modifiers

An array of parameter modifiers that enable binding to work with parameter signatures in which the types have been modified.

Return Value:
A MethodBase object containing the matching method, if found; otherwise, null.
Remarks
This method should return null if no method matches the criteria. This method controls the selection provided by the GetConstructor and GetMethod methods on Type.
Example
using System;
using System.Reflection;
using System.Globalization;



public class MyBinder : Binder 
{

   public MyBinder() : base()
   {

   }

   private class BinderState
   {
      public object[] args;
   }



   public override FieldInfo BindToField(
      BindingFlags bindingAttr,
      FieldInfo[] match,
      object value,
      CultureInfo culture
      )
   {
      if(match == null)
         throw new ArgumentNullException("match");
      
      // Get a field for which 'value' can be converted to that field type.
      for(int i = 0; i < match.Length; i++)
         if(ChangeType(value, match[i].FieldType, culture) != null)
            return match[i];

      return null;
   }




   public override MethodBase BindToMethod(
      BindingFlags bindingAttr,
      MethodBase[] match,
      ref object[] args,
      ParameterModifier[] modifiers,
      CultureInfo culture,
      string[] names,
      out object state
      )
   {
      // Store the arguments to the method in a state object.
      BinderState myBinderState = new BinderState();
      object[] arguments = new Object[args.Length];
      args.CopyTo(arguments, 0);
      myBinderState.args = arguments;
      state = myBinderState;

      if(match == null)
         throw new ArgumentNullException();

      // Find a method that has the same parameters as those of 'args'.
      for(int i = 0; i < match.Length; i++)
      {
         // Count the number of parameters that match.
         int count = 0;
         ParameterInfo[] parameters = match[i].GetParameters();

         // Go on to the next method if the number of parameters don't match.
         if(args.Length != parameters.Length)
            continue;

         // Match each of the parameters that the user expects the method to have.
         for(int j = 0; j < args.Length; j++)
         {
            // If 'names' is not null then reorder 'args'.
            if(names != null)
            {
               if(names.Length != args.Length)
                  throw new ArgumentException("names and args must have the same number of elements");

               for(int k = 0; k < names.Length; k++)
                  if(String.Compare(parameters[j].Name, names[k].ToString()) == 0)
                     args[j] = myBinderState.args[k];
            }

            // Check if the types specified by the user can be converted to parameter type.
            if(ChangeType(args[j], parameters[j].ParameterType, culture) != null)
               count += 1;
            else
               break;
         }

         // Check if the method has been found.
         if(count == args.Length)
            return match[i];
      }
      return null;
   }


   public override object ChangeType(
      object value,
      Type myChangeType,
      CultureInfo culture
      )
   {
      // Check if the 'value' can be converted to a value of type 'myType'.
      if(CanConvertFrom(value.GetType(), myChangeType))
         // Return the converted object.
         return Convert.ChangeType(value, myChangeType);
      else
         // Return null.
         return null;
   }


   public override void ReorderArgumentArray(
      ref object[] args,
      object state
      )
   {
      // Return the args that had been reordered by 'BindToMethod'.
      ((BinderState)state).args.CopyTo(args, 0);
   }


   public override MethodBase SelectMethod(
      BindingFlags bindingAttr,
      MethodBase[] match,
      Type[] types,
      ParameterModifier[] modifiers
      )
   {
      if(match == null)
         throw new ArgumentNullException("match");
      for(int i = 0; i < match.Length; i++)
      {
         // Count the number of parameters that match.
         int count = 0; 
         ParameterInfo[] parameters = match[i].GetParameters();

         // Go on to the next method if the number of parameters don't match.
         if(types.Length != parameters.Length)
            continue;

         // Match each of the parameters that the user expects the method to have.
         for(int j = 0; j < types.Length; j++)
            // Check if the types specified by the user can be converted to parameter type.
            if(CanConvertFrom(types[j], parameters[j].ParameterType))
               count += 1;
            else
               break;

         // Check if the method has been found.
         if(count == types.Length)
            return match[i];
      }
      return null;
   }


   public override PropertyInfo SelectProperty(
      BindingFlags bindingAttr,
      PropertyInfo[] match,
      Type returnType,
      Type[] indexes,
      ParameterModifier[] modifiers
      )
   {
      if(match == null)
         throw new ArgumentNullException("match");

      for(int i = 0; i < match.Length; i++)
      {
         // Count the number of indexes that match.
         int count = 0;
         ParameterInfo[] parameters = match[i].GetIndexParameters();

         // Go on to the next property if the number of indexes don't match.
         if(indexes.Length != parameters.Length)
            continue;

         // Match each of the indexes that the user expects the property to have.
         for(int j = 0; j < indexes.Length; j++)
            // Check if the types specified by the user can be converted to index type.
            if(CanConvertFrom(indexes[j], parameters[j].ParameterType))
               count += 1;
            else
               break;

         // Check if the property has been found.
         if(count == indexes.Length)
            // Check if the return type can be converted to the properties type.
            if(CanConvertFrom(returnType, match[i].GetType()))
               return match[i];
            else
               continue;
      }
      return null;
   }


   // Checks if 'type1' can be converted to 'type2'. (Checks only for primitive types).
   private bool CanConvertFrom(Type type1, Type type2)
   {
      if(type1.IsPrimitive && type2.IsPrimitive)
      {
         TypeCode typeCode1 = Type.GetTypeCode(type1);
         TypeCode typeCode2 = Type.GetTypeCode(type2);
         // If both 'type1' and 'type2' have same type return true.
         if(typeCode1 == typeCode2)
            return true;
         // Possible conversions from 'Char' follow.
         if(typeCode1 == TypeCode.Char)
            switch(typeCode2)
            {
               case TypeCode.UInt16 : return true;
               case TypeCode.UInt32 : return true;
               case TypeCode.Int32  : return true;
               case TypeCode.UInt64 : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Byte' follow.
         if(typeCode1 == TypeCode.Byte)
            switch(typeCode2)
            {
               case TypeCode.Char   : return true;
               case TypeCode.UInt16 : return true;
               case TypeCode.Int16  : return true;
               case TypeCode.UInt32 : return true;
               case TypeCode.Int32  : return true;
               case TypeCode.UInt64 : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'SByte' follow.
         if(typeCode1 == TypeCode.SByte)
            switch(typeCode2)
            {
               case TypeCode.Int16  : return true;
               case TypeCode.Int32  : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'UInt16' follow.
         if(typeCode1 == TypeCode.UInt16)
            switch(typeCode2)
            {
               case TypeCode.UInt32 : return true;
               case TypeCode.Int32  : return true;
               case TypeCode.UInt64 : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Int16' follow.
         if(typeCode1 == TypeCode.Int16)
            switch(typeCode2)
            {
               case TypeCode.Int32  : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'UInt32' follow.
         if(typeCode1 == TypeCode.UInt32)
            switch(typeCode2)
            {
               case TypeCode.UInt64 : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Int32' follow.
         if(typeCode1 == TypeCode.Int32)
            switch(typeCode2)
            {
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'UInt64' follow.
         if(typeCode1 == TypeCode.UInt64)
            switch(typeCode2)
            {
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Int64' follow.
         if(typeCode1 == TypeCode.Int64)
            switch(typeCode2)
            {
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Single' follow.
         if(typeCode1 == TypeCode.Single)
            switch(typeCode2)
            {
               case TypeCode.Double : return true;
               default              : return false;
            }
      }
      return false;
   }
}



public class MyClass1
{
   public short myFieldB;
   public int myFieldA; 
   public void MyMethod(long i, char k)
   {
      Console.WriteLine("\nThis is MyMethod(long i, char k)");
   }
   public void MyMethod(long i, long j)
   {
      Console.WriteLine("\nThis is MyMethod(long i, long j)");
   }
}

public class Binder_Example
{
   public static void Main()
   {
      // Get the type of 'MyClass1'.
      Type myType = typeof(MyClass1);
      // Get the instance of 'MyClass1'.
      MyClass1 myInstance = new MyClass1();
      Console.WriteLine("\nDisplaying results of using 'MyBinder' binder\n");
      // Get the method information for the method 'MyMethod'.
      MethodInfo myMethod = myType.GetMethod("MyMethod", BindingFlags.Public | BindingFlags.Instance,
         new MyBinder(), new Type[] {typeof(short), typeof(short)}, null);
      Console.WriteLine(myMethod);
      // Invoke the method named 'MyMethod'.
      myType.InvokeMember("MyMethod", BindingFlags.InvokeMethod, new MyBinder(),
         myInstance, new Object[] {(int)32, (int)32});
   }
}

    

Return to top


Method: SelectProperty(
   BindingFlags bindingAttr,
   PropertyInfo[] match,
   Type returnType,
   Type[] indexes,
   ParameterModifier[] modifiers
)
Summary
Selects a property from the given set of properties, based on the specified criteria.
C# Syntax:
public abstract PropertyInfo SelectProperty(
   BindingFlags bindingAttr,
   PropertyInfo[] match,
   Type returnType,
   Type[] indexes,
   ParameterModifier[] modifiers
);
Parameters:

bindingAttr

One of the BindingFlags enumerators.

match

The set of properties Reflection has determined to be a possible match, typically because they have the correct member name.

returnType

The return value the matching property must have.

indexes

The index types of the property being searched for. Used for index properties such as the indexer for a class.

modifiers

An array of parameter modifiers that enable binding to work with parameter signatures in which the types have been modified.

Return Value:
A PropertyInfo object containing the matching property.
Remarks
This method controls the selection provided by the GetProperty method on Type.
Example
using System;
using System.Reflection;
using System.Globalization;



public class MyBinder : Binder 
{

   public MyBinder() : base()
   {

   }

   private class BinderState
   {
      public object[] args;
   }



   public override FieldInfo BindToField(
      BindingFlags bindingAttr,
      FieldInfo[] match,
      object value,
      CultureInfo culture
      )
   {
      if(match == null)
         throw new ArgumentNullException("match");
      
      // Get a field for which 'value' can be converted to that field type.
      for(int i = 0; i < match.Length; i++)
         if(ChangeType(value, match[i].FieldType, culture) != null)
            return match[i];

      return null;
   }




   public override MethodBase BindToMethod(
      BindingFlags bindingAttr,
      MethodBase[] match,
      ref object[] args,
      ParameterModifier[] modifiers,
      CultureInfo culture,
      string[] names,
      out object state
      )
   {
      // Store the arguments to the method in a state object.
      BinderState myBinderState = new BinderState();
      object[] arguments = new Object[args.Length];
      args.CopyTo(arguments, 0);
      myBinderState.args = arguments;
      state = myBinderState;

      if(match == null)
         throw new ArgumentNullException();

      // Find a method that has the same parameters as those of 'args'.
      for(int i = 0; i < match.Length; i++)
      {
         // Count the number of parameters that match.
         int count = 0;
         ParameterInfo[] parameters = match[i].GetParameters();

         // Go on to the next method if the number of parameters don't match.
         if(args.Length != parameters.Length)
            continue;

         // Match each of the parameters that the user expects the method to have.
         for(int j = 0; j < args.Length; j++)
         {
            // If 'names' is not null then reorder 'args'.
            if(names != null)
            {
               if(names.Length != args.Length)
                  throw new ArgumentException("names and args must have the same number of elements");

               for(int k = 0; k < names.Length; k++)
                  if(String.Compare(parameters[j].Name, names[k].ToString()) == 0)
                     args[j] = myBinderState.args[k];
            }

            // Check if the types specified by the user can be converted to parameter type.
            if(ChangeType(args[j], parameters[j].ParameterType, culture) != null)
               count += 1;
            else
               break;
         }

         // Check if the method has been found.
         if(count == args.Length)
            return match[i];
      }
      return null;
   }


   public override object ChangeType(
      object value,
      Type myChangeType,
      CultureInfo culture
      )
   {
      // Check if the 'value' can be converted to a value of type 'myType'.
      if(CanConvertFrom(value.GetType(), myChangeType))
         // Return the converted object.
         return Convert.ChangeType(value, myChangeType);
      else
         // Return null.
         return null;
   }


   public override void ReorderArgumentArray(
      ref object[] args,
      object state
      )
   {
      // Return the args that had been reordered by 'BindToMethod'.
      ((BinderState)state).args.CopyTo(args, 0);
   }


   public override MethodBase SelectMethod(
      BindingFlags bindingAttr,
      MethodBase[] match,
      Type[] types,
      ParameterModifier[] modifiers
      )
   {
      if(match == null)
         throw new ArgumentNullException("match");
      for(int i = 0; i < match.Length; i++)
      {
         // Count the number of parameters that match.
         int count = 0; 
         ParameterInfo[] parameters = match[i].GetParameters();

         // Go on to the next method if the number of parameters don't match.
         if(types.Length != parameters.Length)
            continue;

         // Match each of the parameters that the user expects the method to have.
         for(int j = 0; j < types.Length; j++)
            // Check if the types specified by the user can be converted to parameter type.
            if(CanConvertFrom(types[j], parameters[j].ParameterType))
               count += 1;
            else
               break;

         // Check if the method has been found.
         if(count == types.Length)
            return match[i];
      }
      return null;
   }


   public override PropertyInfo SelectProperty(
      BindingFlags bindingAttr,
      PropertyInfo[] match,
      Type returnType,
      Type[] indexes,
      ParameterModifier[] modifiers
      )
   {
      if(match == null)
         throw new ArgumentNullException("match");

      for(int i = 0; i < match.Length; i++)
      {
         // Count the number of indexes that match.
         int count = 0;
         ParameterInfo[] parameters = match[i].GetIndexParameters();

         // Go on to the next property if the number of indexes don't match.
         if(indexes.Length != parameters.Length)
            continue;

         // Match each of the indexes that the user expects the property to have.
         for(int j = 0; j < indexes.Length; j++)
            // Check if the types specified by the user can be converted to index type.
            if(CanConvertFrom(indexes[j], parameters[j].ParameterType))
               count += 1;
            else
               break;

         // Check if the property has been found.
         if(count == indexes.Length)
            // Check if the return type can be converted to the properties type.
            if(CanConvertFrom(returnType, match[i].GetType()))
               return match[i];
            else
               continue;
      }
      return null;
   }


   // Checks if 'type1' can be converted to 'type2'. (Checks only for primitive types).
   private bool CanConvertFrom(Type type1, Type type2)
   {
      if(type1.IsPrimitive && type2.IsPrimitive)
      {
         TypeCode typeCode1 = Type.GetTypeCode(type1);
         TypeCode typeCode2 = Type.GetTypeCode(type2);
         // If both 'type1' and 'type2' have same type return true.
         if(typeCode1 == typeCode2)
            return true;
         // Possible conversions from 'Char' follow.
         if(typeCode1 == TypeCode.Char)
            switch(typeCode2)
            {
               case TypeCode.UInt16 : return true;
               case TypeCode.UInt32 : return true;
               case TypeCode.Int32  : return true;
               case TypeCode.UInt64 : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Byte' follow.
         if(typeCode1 == TypeCode.Byte)
            switch(typeCode2)
            {
               case TypeCode.Char   : return true;
               case TypeCode.UInt16 : return true;
               case TypeCode.Int16  : return true;
               case TypeCode.UInt32 : return true;
               case TypeCode.Int32  : return true;
               case TypeCode.UInt64 : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'SByte' follow.
         if(typeCode1 == TypeCode.SByte)
            switch(typeCode2)
            {
               case TypeCode.Int16  : return true;
               case TypeCode.Int32  : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'UInt16' follow.
         if(typeCode1 == TypeCode.UInt16)
            switch(typeCode2)
            {
               case TypeCode.UInt32 : return true;
               case TypeCode.Int32  : return true;
               case TypeCode.UInt64 : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Int16' follow.
         if(typeCode1 == TypeCode.Int16)
            switch(typeCode2)
            {
               case TypeCode.Int32  : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'UInt32' follow.
         if(typeCode1 == TypeCode.UInt32)
            switch(typeCode2)
            {
               case TypeCode.UInt64 : return true;
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Int32' follow.
         if(typeCode1 == TypeCode.Int32)
            switch(typeCode2)
            {
               case TypeCode.Int64  : return true;
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'UInt64' follow.
         if(typeCode1 == TypeCode.UInt64)
            switch(typeCode2)
            {
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Int64' follow.
         if(typeCode1 == TypeCode.Int64)
            switch(typeCode2)
            {
               case TypeCode.Single : return true;
               case TypeCode.Double : return true;
               default              : return false;
            }
         // Possible conversions from 'Single' follow.
         if(typeCode1 == TypeCode.Single)
            switch(typeCode2)
            {
               case TypeCode.Double : return true;
               default              : return false;
            }
      }
      return false;
   }
}



public class MyClass1
{
   public short myFieldB;
   public int myFieldA; 
   public void MyMethod(long i, char k)
   {
      Console.WriteLine("\nThis is MyMethod(long i, char k)");
   }
   public void MyMethod(long i, long j)
   {
      Console.WriteLine("\nThis is MyMethod(long i, long j)");
   }
}

public class Binder_Example
{
   public static void Main()
   {
      // Get the type of 'MyClass1'.
      Type myType = typeof(MyClass1);
      // Get the instance of 'MyClass1'.
      MyClass1 myInstance = new MyClass1();
      Console.WriteLine("\nDisplaying results of using 'MyBinder' binder\n");
      // Get the method information for the method 'MyMethod'.
      MethodInfo myMethod = myType.GetMethod("MyMethod", BindingFlags.Public | BindingFlags.Instance,
         new MyBinder(), new Type[] {typeof(short), typeof(short)}, null);
      Console.WriteLine(myMethod);
      // Invoke the method named 'MyMethod'.
      myType.InvokeMember("MyMethod", BindingFlags.InvokeMethod, new MyBinder(),
         myInstance, new Object[] {(int)32, (int)32});
   }
}

    
See also:
PropertyInfo

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.