System.Reflection.IReflect Interface

Assembly: Mscorlib.dll
Namespace: System.Reflection
Summary
Allows objects to return MemberInfo objects that represent an object.
C# Syntax:
public interface IReflect
Remarks
IReflect defines a subset of the Type reflection methods. Use IReflect to access and invoke members of a type.
See also:
System.Reflection Namespace

System.Reflection.IReflect Member List:

Public Properties
UnderlyingSystemType Read-only

Gets the underlying type that represents the IReflect object.
Public Methods
GetField Returns the FieldInfo object corresponding to the specified field and BindingFlag.
GetFields Returns an array of FieldInfo objects corresponding to all fields of the current class.
GetMember Retrieves an array of MemberInfo objects corresponding to all public members or to all members that match a specified name.
GetMembers Retrieves an array of MemberInfo objects corresponding either to all public members or to all members of the current class.
GetMethod Overloaded:
GetMethod(string name, BindingFlags bindingAttr)

Retrieves a MethodInfo object corresponding to a specified method under specified search constraints.
GetMethod Overloaded:
GetMethod(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)

Retrieves a MethodInfo object corresponding to a specified method, using a Type array to choose from among overloaded methods.
GetMethods Retrieves an array of MethodInfo objects with all public methods or all methods of the current class.
GetProperties Retrieves an array of PropertyInfo objects corresponding to all public properties or to all properties of the current class.
GetProperty Overloaded:
GetProperty(string name, BindingFlags bindingAttr)

Retrieves a PropertyInfo object corresponding to a specified property under specified search constraints.
GetProperty Overloaded:
GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)

Retrieves a PropertyInfo object corresponding to a specified property with specified search constraints.
InvokeMember Invokes a specified member.

System.Reflection.IReflect Member Details

Property: UnderlyingSystemType (read-only)
Summary
Gets the underlying type that represents the IReflect object.
C# Syntax:
Type UnderlyingSystemType {get;}
See also:
Object

Return to top


Method: GetField(
   string name,
   BindingFlags bindingAttr
)
Summary
Returns the FieldInfo object corresponding to the specified field and BindingFlag.
C# Syntax:
FieldInfo GetField(
   string name,
   BindingFlags bindingAttr
);
Parameters:

name

The name of the field to find.

bindingAttr

The binding attributes used to control the search.

Return Value:
A FieldInfo object containing the field information for the named object that meets the search constraints specified in bindingAttr.
Exceptions
Exception Type Condition
AmbiguousMatchException The object implements multiple fields with the same name.
See also:
BindingFlags

Return to top


Method: GetFields(
   BindingFlags bindingAttr
)
Summary
Returns an array of FieldInfo objects corresponding to all fields of the current class.
C# Syntax:
FieldInfo[] GetFields(
   BindingFlags bindingAttr
);
Parameters:

bindingAttr

The binding attributes used to control the search.

Return Value:
An array of FieldInfo objects containing all the field information for this reflection object that meets the search constraints specified in bindingAttr.
Remarks
The match is based upon a name. Each field must have a unique name. The BindingFlags.NonPublic flag specifies that non-public methods are included in the search. The BindingFlags.Public flag specifies that public methods are included in the search.
See also:
BindingFlags | BindingFlags | BindingFlags

Return to top


Method: GetMember(
   string name,
   BindingFlags bindingAttr
)
Summary
Retrieves an array of MemberInfo objects corresponding to all public members or to all members that match a specified name.
C# Syntax:
MemberInfo[] GetMember(
   string name,
   BindingFlags bindingAttr
);
Parameters:

name

The name of the member to find.

bindingAttr

The binding attributes used to control the search.

Return Value:
An array of MemberInfo objects matching name.
Remarks
The GetMember method retrieves an array of MemberInfo objects by using the name and binding attribute that correspond to all public members or to all members that match a specified name. The case of the specified name is observed or ignored, as specified by BindingFlags.IgnoreCase.
See also:
BindingFlags

Return to top


Method: GetMembers(
   BindingFlags bindingAttr
)
Summary
Retrieves an array of MemberInfo objects corresponding either to all public members or to all members of the current class.
C# Syntax:
MemberInfo[] GetMembers(
   BindingFlags bindingAttr
);
Parameters:

bindingAttr

The binding attributes used to control the search.

Return Value:
An array of MemberInfo objects containing all the member information for this reflection object.
Remarks
This method retrieves an array of MemberInfo objects by using the binding attribute that corresponds either to all public members or to all members of the current class. It returns an array of all of the members defined for this object.
See also:
BindingFlags

Return to top


Overloaded Method: GetMethod(
   string name,
   BindingFlags bindingAttr
)
Summary
Retrieves a MethodInfo object corresponding to a specified method under specified search constraints.
C# Syntax:
MethodInfo GetMethod(
   string name,
   BindingFlags bindingAttr
);
Parameters:

name

The name of the member to find.

bindingAttr

The binding attributes used to control the search.

Return Value:
A MethodInfo object containing the method information, with the match being based on the method name and search constraints specified in bindingAttr.
Exceptions
Exception Type Condition
AmbiguousMatchException The object implements multiple methods with the same name.
See also:
BindingFlags

Return to top


Overloaded Method: GetMethod(
   string name,
   BindingFlags bindingAttr,
   Binder binder,
   Type[] types,
   ParameterModifier[] modifiers
)
Summary
Retrieves a MethodInfo object corresponding to a specified method, using a Type array to choose from among overloaded methods.
C# Syntax:
MethodInfo GetMethod(
   string name,
   BindingFlags bindingAttr,
   Binder binder,
   Type[] types,
   ParameterModifier[] modifiers
);
Parameters:

name

The name of the member to find.

bindingAttr

The binding attributes used to control the search.

binder

An object that implements Binder, containing properties related to this method.

types

An array used to choose among overloaded methods.

modifiers

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

Return Value:
The requested method that matches all the specified parameters.
Exceptions
Exception Type Condition
AmbiguousMatchException The object implements multiple methods with the same name.
Remarks
The return value is a match based on the method name, BindingFlags enum member, the kind of type conversion specified by the binder parameter, the overload, and the ParameterInfo that describes the signature of the method.
See also:
BindingFlags | Binder | Type | ParameterModifier

Return to top


Method: GetMethods(
   BindingFlags bindingAttr
)
Summary
Retrieves an array of MethodInfo objects with all public methods or all methods of the current class.
C# Syntax:
MethodInfo[] GetMethods(
   BindingFlags bindingAttr
);
Parameters:

bindingAttr

The binding attributes used to control the search.

Return Value:
An array of MethodInfo objects containing all the methods defined for this reflection object that meet the search constraints specified in bindingAttr.
Remarks
The non-public attribute of bindingAttr is indicated only if public methods are returned.
See also:
BindingFlags | BindingFlags

Return to top


Method: GetProperties(
   BindingFlags bindingAttr
)
Summary
Retrieves an array of PropertyInfo objects corresponding to all public properties or to all properties of the current class.
C# Syntax:
PropertyInfo[] GetProperties(
   BindingFlags bindingAttr
);
Parameters:

bindingAttr

The binding attribute used to control the search.

Return Value:
An array of PropertyInfo objects for all the properties defined on the reflection object.
See also:
BindingFlags

Return to top


Overloaded Method: GetProperty(
   string name,
   BindingFlags bindingAttr
)
Summary
Retrieves a PropertyInfo object corresponding to a specified property under specified search constraints.
C# Syntax:
PropertyInfo GetProperty(
   string name,
   BindingFlags bindingAttr
);
Parameters:

name

The name of the property to find.

bindingAttr

The binding attributes used to control the search.

Return Value:
A PropertyInfo object for the located property that meets the search constraints specified in bindingAttr, or null if the property was not located.
Exceptions
Exception Type Condition
AmbiguousMatchException The object implements multiple fields with the same name.
See also:
BindingFlags

Return to top


Overloaded Method: GetProperty(
   string name,
   BindingFlags bindingAttr,
   Binder binder,
   Type returnType,
   Type[] types,
   ParameterModifier[] modifiers
)
Summary
Retrieves a PropertyInfo object corresponding to a specified property with specified search constraints.
C# Syntax:
PropertyInfo GetProperty(
   string name,
   BindingFlags bindingAttr,
   Binder binder,
   Type returnType,
   Type[] types,
   ParameterModifier[] modifiers
);
Parameters:

name

The name of the member to find.

bindingAttr

The binding attribute used to control the search.

binder

An object that implements Binder, containing properties related to this method.

returnType

The type of the property.

types

An array used to choose among overloaded methods with the same name.

modifiers

An array used to choose the parameter modifiers.

Return Value:
A PropertyInfo object for the located property, if a property with the specified name was located in this reflection object, or null if the property was not located.
Remarks
This method retrieves a PropertyInfo object corresponding to a specified property under specified search constraints. A type array is used to choose from among overloaded methods.
See also:
ParameterModifier | Type | Binder | BindingFlags

Return to top


Method: InvokeMember(
   string name,
   BindingFlags invokeAttr,
   Binder binder,
   object target,
   object[] args,
   ParameterModifier[] modifiers,
   CultureInfo culture,
   string[] namedParameters
)
Summary
Invokes a specified member.
C# Syntax:
object InvokeMember(
   string name,
   BindingFlags invokeAttr,
   Binder binder,
   object target,
   object[] args,
   ParameterModifier[] modifiers,
   CultureInfo culture,
   string[] namedParameters
);
Parameters:

name

The name of the member to find.

invokeAttr

One of the BindingFlags invocation attributes. The invokeAttr parameter may be a constructor, method, property, or field. A suitable invocation attribute must be specified. Invoke the default member of a class by passing the empty string ("") as the name of the member.

binder

One of the BindingFlags bit flags. Implements Binder, containing properties related to this method.

target

The object on which to invoke the specified member. This parameter is ignored for static members.

args

An array of objects that contains the number, order, and type of the parameters of the member to be invoked. This is an empty array if there are no parameters.

modifiers

An array of ParameterModifier objects. This array has the same length as args, representing the invoked member's argument attributes in the metadata. A parameter can have the following attributes: pdIn, pdOut, pdRetval, pdOptional, and pdHasDefault. These represent [In], [Out], [retval], [optional], and a default parameter, respectively. These attributes are used by various interoperability services.

culture

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

namedParameters

A String array of parameters.

Return Value:
The specified member.
Exceptions
Exception Type Condition
ArgumentException invokeAttr is BindingFlags.CreateInstance and another bit flag is also set.
ArgumentException invokeAttr is not BindingFlags.CreateInstance and name is null.
ArgumentException invokeAttr is not an invocation attribute from BindingFlags.
ArgumentException invokeAttr specifies both get and set for a property or field.
ArgumentException invokeAttr specifies both a field set and an Invoke method.args are provided for a field get.
ArgumentException More than one argument is specified for a field set.
MissingFieldException The field or property cannot be found.
MissingMethodException The method cannot be found.
SecurityException A private member is invoked without the necessary ReflectionPermission.
Remarks
The method that is to be invoked must be accessible and provide the most specific match with the specified argument list, under the constraints of the specified binder and invocation attributes.

A method is invoked if the number of parameters in the method declaration equals the number of arguments in the specified argument list, and the type of each argument can be converted by the binder to the type of the parameter.



Note The array of parameter modifiers passed in to InvokeMember must contain a single parameter modifier. Only the first parameter modifier is considered when determining which argument needs to be passed by reference when exposed to COM.

The binder finds all matching methods, in accordance with the type of binding requested ( BindingFlags.InvokeMethod, IReflect.GetProperties, and so on). The set of methods is filtered by the name, number of arguments, and a set of search modifiers defined in the binder. After the method is selected, it is invoked, and accessibility is checked at that point. The search may control which set of methods is searched based upon the accessibility attribute associated with the method. Binder.BindToMethod selects the method to be invoked. The default binder selects the most specific match.

Access restrictions are ignored for fully trusted code. That is, private constructors, methods, fields, and properties can be accessed and invoked through reflection whenever the code is fully trusted.

Example
This Managed Extensions for C++ sample demonstrates using InvokeMember to obtain the value of the DateTime.Now property.
public class MainClass { 
    public static void Main(string[] args)  {
        Type tDate = Type.GetType("System.DateTime");
        Object result = tDate.InvokeMember("Now", BindingFlags.GetProperty, null, null, new Object[0]);
        Console.WriteLine(result.ToString());
    }
}
/*
 
OUTPUT:

D:\CCOM>csc invokemember.cs

Microsoft (R) Visual C# Compiler Version 7.00.9150 [CLR version v1.0.2702]

Copyright (C) Microsoft Corp 2000. All rights reserved.

D:\CCOM>invokemember.exe

3/26/2001 2:39:22 PM

*/

    
See also:
Object | Binder | BindingFlags

Return to top


Top of page

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