extends System.Object
| Variables: |
|---|
public System.Reflection.Assembly Assembly |
public System.String AssemblyQualifiedName |
public System.Reflection.TypeAttributes Attributes |
public System.Type BaseType |
public System.Type DeclaringType |
public static System.Reflection.Binder DefaultBinder |
public System.String FullName |
public System.Boolean HasElementType |
public System.Boolean IsAbstract |
public System.Boolean IsArray |
public System.Boolean IsAutoLayout |
public System.Boolean IsByRef |
public System.Boolean IsClass |
public System.Boolean IsEnum |
public System.Boolean IsExplicitLayout |
public System.Boolean IsImport |
public System.Boolean IsInterface |
public System.Boolean IsLayoutSequential |
public System.Boolean IsMarshalByRef |
public System.Boolean IsNestedAssembly |
public System.Boolean IsNestedFamANDAssem |
public System.Boolean IsNestedFamily |
public System.Boolean IsNestedFamORAssem |
public System.Boolean IsNestedPrivate |
public System.Boolean IsNestedPublic |
public System.Boolean IsNotPublic |
public System.Boolean IsPointer |
public System.Boolean IsPrimitive |
public System.Boolean IsPublic |
public System.Boolean IsSealed |
public System.Boolean IsSpecialName |
public System.Boolean IsValueType |
public System.Reflection.Module Module |
public System.String Namespace |
public System.Type ReflectedType |
public System.RuntimeTypeHandle TypeHandle |
public System.Reflection.ConstructorInfo TypeInitializer |
public System.Type UnderlyingSystemType |
| Constructors: |
|---|
protected System.Type() Constructs a new instance of the System.Type class. |
| Functions: |
|---|
public System.Boolean Equals(System.Type o) Determines if the underlying system type of the current System.Type is the same as the underlying system type of the specified System.Type. Parameter o: The System.Type whose underlying system type is to be compared with the underlying system type of the current System.Type. Returns: true if the underlying system type of o is the same as the underlying system type of the current System.Type; otherwise, false. |
public virtual System.Int32 GetArrayRank() Returns the number of dimensions in the current System.Type. Returns: A System.Int32 containing the number of dimensions in the current System.Type. Throws: : The current System.Type is not an array. |
protected System.Reflection.TypeAttributes GetAttributeFlagsImpl() When overridden in a derived type implements the System.Type.Attributes property and returns the attributes specified for the type represented by the current instance. Returns: A System.Reflection.TypeAttributes value that signifies the attributes of the type represented by the current instance. This property is read-only. This method returns a System.Reflection.TypeAttributes value that indicates the attributes set in the metadata of the type represented by the current instance. Use this property to determine the visibility, semantics, and layout format of the type represented by the current. Also use this property to determine if the type represented by the current instance has a special name. |
public System.Reflection.ConstructorInfo GetConstructor(System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Type[] types, System.Reflection.ParameterModifier[] modifiers) Returns a constructor defined in the type represented by the current instance. The parameters of the constructor match the specified argument types and modifiers, under the specified binding constraints. Parameter bindingAttr: A bitwise combination of System.Reflection.BindingFlags values that control the binding process. If zero is specified, this method returns null. Parameter binder: A System.Reflection.Binder object that defines a set of properties and enables the binding, coercion of argument types, and invocation of members using reflection. Specify null to use the System.Type.DefaultBinder . Parameter types: An array of System.Type objects. The elements in the array are of the same number, in the same order, and represent the same types as the parameters for the constructor to be returned. Parameter modifiers: The only defined value for this parameter is null . Returns: A System.Reflection.ConstructorInfo object that reflects the constructor that matches the specified criteria. If an exact match does not exist, binder will attempt to coerce the parameter types specified in types to select a match. If binder is unable to select a match, returns null . If the type represented by the current instance is contained in a loaded assembly, the constructor that matches the specified criteria is not public, and the caller does not have sufficient permissions, returns null. Throws: : types is null, or at least one of the elements in types is null. Throws: : types has more than one dimension. The following System.Reflection.BindingFlags are used to define which constructors to include in the search: For more information, see System.Reflection.BindingFlags. |
public System.Reflection.ConstructorInfo GetConstructor(System.Type[] types) Returns a public instance constructor defined in the type represented by the current instance. The parameters of the constructor match the specified argument types. Parameter types: An array of System.Type objects. The elements in the array are of the same number, in the same order, and represent the same types as the parameters for the constructor to be returned. Specify System.Type.EmptyTypes to obtain a constructor that takes no parameters. Returns: A System.Reflection.ConstructorInfo object representing the public instance constructor whose parameters match exactly the types in types , if found; otherwise, null. If the type represented by the current instance is contained in a loaded assembly, the constructor that matches the specified criteria is not public, and the caller does not have sufficient permissions, returns null. Throws: : types is null, or at least one of the elements in types is null. Throws: : types has more than one dimension. This version of System.Type.GetConstructor(System.Reflection.BindingFlags,System.Reflection.Binder,System.Reflection.CallingConventions,System.Type[],System.Reflection.ParameterModifier[]) is equivalent to System.Type.GetConstructor(System.Reflection.BindingFlags,System.Reflection.Binder,System.Reflection.CallingConventions,System.Type[],System.Reflection.ParameterModifier[])(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance, null, types, null). |
public System.Reflection.ConstructorInfo[] GetConstructors() Returns an array of the public constructors defined in the type represented by the current instance. Returns: An array of System.Reflection.ConstructorInfo objects that reflect the public constructors defined in the type represented by the current instance. If no public constructors are defined in the type represented by the current instance, returns an empty array. This version of System.Type.GetConstructors is equivalent to System.Type.GetConstructors(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance). |
public System.Reflection.ConstructorInfo[] GetConstructors(System.Reflection.BindingFlags bindingAttr) Returns an array of constructors defined in the type represented by the current instance, under the specified binding constraints. Parameter bindingAttr: A bitwise combination of System.Reflection.BindingFlags values that control the binding process. If zero is specified, this method returns null . Returns: An array of System.Reflection.ConstructorInfo objects that reflect the constructors that are defined in the type represented by the current instance and match the constraints of bindingAttr. If System.Reflection.BindingFlags.NonPublic and System.Reflection.BindingFlags.Static are specified, this array includes the type initializer if it is defined. If no constructors meeting the constraints of bindingAttr are defined in the type represented by the current instance, returns an empty array. If the type represented by the current instance is contained in a loaded assembly, the constructors that match the specified criteria are not public, and the caller does not have sufficient permission, returns null . The following System.Reflection.BindingFlags are used to define which constructors to include in the search: For more information, see System.Reflection.BindingFlags. As described above. |
public virtual System.Reflection.MemberInfo[] GetDefaultMembers() Returns an array of System.Reflection.MemberInfo objects that reflect the default members defined in the type represented by the current instance. Returns: An array of System.Reflection.MemberInfo objects reflecting the default members of the type represented by the current instance. If the type represented by the current instance does not have any default members, returns an empty array. The members returned by this method have the System.Reflection.DefaultMemberAttribute attribute. |
public System.Type GetElementType() Returns the element type of the current System.Type . Returns: A System.Type that represents the type used to create the current instance if the current instance represents an array, pointer, or an argument passed by reference. Otherwise, returns null . Example: The following example demonstrates the System.Type.GetElementType method. using System;
class TestType {
public static void Main() {
int[] array = {1,2,3};
Type t = array.GetType();
Type t2 = t.GetElementType();
Console.WriteLine("{0} element type is {1}",array, t2.ToString());
TestType newMe = new TestType();
t = newMe.GetType();
t2 = t.GetElementType();
Console.WriteLine("{0} element type is {1}", newMe, t2==null? "null" : t2.ToString());
}
}
The output is System.Int32[] element type is System.Int32 TestType element type is null |
public System.Reflection.EventInfo GetEvent(System.String name) Returns a System.Reflection.EventInfo object reflecting the public event that has the specified name and is defined in the type represented by the current instance. Parameter name: A System.String containing the name of the public event to be returned. Returns: A System.Reflection.EventInfo object reflecting the public event that is named name and is defined in the type represented by the current instance, if found; otherwise, null. Throws: : name is null. This version of System.Type.GetEvent(System.String) is equivalent to System.Type.GetEvent(System.String)( name, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public ). The search for name is case-sensitive. |
public System.Reflection.EventInfo GetEvent(System.String name, System.Reflection.BindingFlags bindingAttr) Returns a System.Reflection.EventInfo object reflecting the event that has the specified name, is defined in the type represented by the current instance, and matches the specified binding constraints. Parameter name: A System.String containing the name of the event to be returned. Parameter bindingAttr: A bitwise combination of System.Reflection.BindingFlags values that control the binding process. If zero is specified, this method returns null. Returns: A System.Reflection.EventInfo object reflecting the event that is named name, is defined in the type represented by the current instance, and matches the constraints of bindingAttr. If an event matching these criteria is not found, returns null. If the event is not public, the current instance represents a type from a loaded assembly, and the caller does not have sufficient permission, returns null. Throws: : name is null. The following System.Reflection.BindingFlags are used to define which events to include in the search: The following System.Reflection.BindingFlags value can be used to change how the search works: For more information, see System.Reflection.BindingFlags. As described above. |
public virtual System.Reflection.EventInfo[] GetEvents() Returns an array of System.Reflection.EventInfo objects that reflect the public events defined in the type represented by the current instance. Returns: An array of System.Reflection.EventInfo objects that reflect the public events defined in the type represented by the current instance. If no public events are defined in the type represented by the current instance, returns an empty array. As described above. This version of System.Type.GetEvents is equivalent to System.Type.GetEvents(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance). |
public System.Reflection.EventInfo[] GetEvents(System.Reflection.BindingFlags bindingAttr) Returns an array of System.Reflection.EventInfo objects that reflect the events that are defined in the type represented by the current instance and match the specified binding constraints. Parameter bindingAttr: A bitwise combination of System.Reflection.BindingFlags values that control the binding process. If zero is specified, this method returns null. Returns: An array of System.Reflection.EventInfo objects that reflect the events that are defined in the type represented by the current instance and match the constraints of bindingAttr . If no events match these constraints, returns an empty array. If the type reflected by the current instance is from a loaded assembly and the caller does not have permission to reflect on non-public objects in loaded assemblies, returns only public events. The following System.Reflection.BindingFlags are used to define which events to include in the search: For more information, see System.Reflection.BindingFlags. As described above. |
public System.Reflection.FieldInfo GetField(System.String name, System.Reflection.BindingFlags bindingAttr) Returns a System.Reflection.FieldInfo object reflecting the field that has the specified name, is defined in the type represented by the current instance, and matches the specified binding constraints. Parameter name: A System.String containing the name of the field to be returned. Parameter bindingAttr: A bitwise combination of System.Reflection.BindingFlags values that control the binding process. If zero is specified, this method returns null . Returns: A System.Reflection.FieldInfo object reflecting the field that is named name, is defined in the type represented by the current instance, and matches the constraints of bindingAttr. If a field matching these criteria cannot be found, returns null. If the field is not public, the current type is from a loaded assembly, and the caller does not have sufficient permission, returns null. Throws: : name is null. The following System.Reflection.BindingFlags are used to define which fields to include in the search: The following System.Reflection.BindingFlags values can be used to change how the search works: For more information, see System.Reflection.BindingFlags. As described above. |
public System.Reflection.FieldInfo GetField(System.String name) Returns a System.Reflection.FieldInfo object reflecting the field that has the specified name and is defined in the type represented by the current instance. Parameter name: A System.String containing the name of the field to be returned. Returns: A System.Reflection.FieldInfo object reflecting the field that is named name and is defined in the type represented by the current instance, if found; otherwise, null. If the selected field is non-public, the type represented by the current instance is from a loaded assembly and the caller does not have sufficient permission to reflect on non-public objects in loaded assemblies, returns null. Throws: : name is null. The search for name is case-sensitive. This version of System.Type.GetField(System.String,System.Reflection.BindingFlags) is equivalent to System.Type.GetField(System.String,System.Reflection.BindingFlags)( name, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance ). |
public System.Reflection.FieldInfo[] GetFields() Returns an array of System.Reflection.FieldInfo objects that reflect the public fields defined in the type represented by the current instance. Returns: An array of System.Reflection.FieldInfo objects that reflect the public fields defined in the type represented by the current instance. If no public fields are defined in the type represented by the current instance, returns an empty array. This version of System.Type.GetFields is equivalent to System.Type.GetFields( System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public ). |
public System.Reflection.FieldInfo[] GetFields(System.Reflection.BindingFlags bindingAttr) Returns an array of System.Reflection.FieldInfo objects that reflect the fields that are defined in the type represented by the current instance and match the specified binding constraints. Parameter bindingAttr: A bitwise combination of System.Reflection.BindingFlags values that control the binding process. If zero is specified, this method returns null . Returns: An array of System.Reflection.FieldInfo objects that reflect the fields that are defined in the type represented by the current instance and match the constraints of bindingAttr . If no fields match these constraints, returns an empty array. If the type represented by the current instance is from a loaded assembly and the caller does not have sufficient permission to reflect on non-public objects in loaded assemblies, returns only public fields. The following System.Reflection.BindingFlags are used to define which fields to include in the search: The following System.Reflection.BindingFlags values can be used to change how the search works: For more information, see System.Reflection.BindingFlags. As described above. |
public System.Int32 GetHashCode() Generates a hash code for the current instance. Returns: A System.Int32 containing the hash code for this instance. The algorithm used to generate the hash code is unspecified. This method overrides System.Object.GetHashCode . |
public System.Type GetInterface(System.String name, System.Boolean ignoreCase) Returns the specified interface, specifying whether to do a case-sensitive search. Parameter name: A System.String containing the name of the interface to return. Parameter ignoreCase: A System.Boolean where true indicates that the name search is to be done case-insensitively, and false performs a case-sensitive search. Returns: A System.Type object representing the interface with the specified name, implemented or inherited by the type represented by the instance, if found; otherwise, null. Throws: : name is null. |
public System.Type GetInterface(System.String name) Searches for the interface with the specified name. Parameter name: A System.String containing the name of the interface to get. Returns: A System.Type object representing the interface with the specified name, implemented or inherited by the current System.Type, if found; otherwise, null. Throws: : name is null. The search for name is case-sensitive. |
public System.Type[] GetInterfaces() Returns all interfaces implemented or inherited by the type represented by the current instance. Returns: An array of System.Type objects representing the interfaces implemented or inherited by the type represented by the current instance. If no interfaces are found, returns an empty System.Type array. |
public virtual System.Reflection.MemberInfo[] GetMember(System.String name, System.Reflection.BindingFlags bindingAttr) Returns an array of System.Reflection.MemberInfo objects that reflect the members defined in the type represented by the current instance that have the specified name and match the specified binding constraints. Parameter name: A System.String containing the name of the member to be returned. Parameter bindingAttr: A bitwise combination of System.Reflection.BindingFlags values that control the binding process. If zero is specified, this method returns null . Returns: An array of System.Reflection.MemberInfo objects that reflect the members named name, are defined in the type represented by the current instance and match the constraints of bindingAttr . If no members match these constraints, returns an empty array. If the selected member is non-public, the type reflected by the current instance is from a loaded assembly and the caller does not have sufficient permission to reflect on non-public objects in loaded assemblies, returns null . Throws: : name is null. The following System.Reflection.BindingFlags are used to define which members to include in the search: The following System.Reflection.BindingFlags values can be used to change how the search works: For more information, see System.Reflection.BindingFlags. As described above. |
public System.Reflection.MemberInfo[] GetMember(System.String name) Returns an array of System.Reflection.MemberInfo objects that reflect the public members that have the specified name and are defined in the type represented by the current instance. Parameter name: A System.String containing the name of the members to be returned. Returns: An array of System.Reflection.MemberInfo objects that reflect the public members that are named name and are defined in the type represented by the current instance. If no public members with the specified name are defined in the type represented by the current instance, returns an empty array. Throws: : name is null. This version of System.Type.GetMember(System.String) is equivalent to System.Type.GetMember(System.String)( name, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public ). The search for name is case-sensitive. |
public System.Reflection.MemberInfo[] GetMembers(System.Reflection.BindingFlags bindingAttr) Returns an array of System.Reflection.MemberInfo objects that reflect the members that are defined in the type represented by the current instance and match the specified binding constraints. Parameter bindingAttr: A bitwise combination of System.Reflection.BindingFlags values that control the binding process. If zero is specified, this method returns null . Returns: An array of System.Reflection.MemberInfo objects that reflect the members defined in the type represented by the current instance that match the constraints of bindingAttr . If no members match these constraints, returns an empty array. If the type represented by the current instance is from a loaded assembly and the caller does not have sufficient permission to reflect on non-public objects in loaded assemblies, returns only public members. The following System.Reflection.BindingFlags are used to define which members to include in the search: The following System.Reflection.BindingFlags values can be used to change how the search works: For more information, see System.Reflection.BindingFlags. As described above. |
public System.Reflection.MemberInfo[] GetMembers() Returns an array of System.Reflection.MemberInfo objects that reflect the public members defined in the type represented by the current instance. Returns: An array of System.Reflection.MemberInfo objects that reflect the public members defined in the type represented by the current instance. If no public members are defined in the type represented by the current instance, returns an empty array. This version of System.Type.GetMembers is equivalent to System.Type.GetMembers(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance). |
public System.Reflection.MethodInfo GetMethod(System.String name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Type[] types, System.Reflection.ParameterModifier[] modifiers) Returns a System.Reflection.MethodInfo object that reflects the method that matches the specified criteria and is defined in the type represented by the current instance. Parameter name: A System.String containing the name of the method to be returned. Parameter bindingAttr: A bitwise combination of System.Reflection.BindingFlags values that control the binding process. If zero is specified, this method returns null . Parameter binder: A System.Reflection.Binder object that defines a set of properties and enables the binding, coercion of argument types, and invocation of members using reflection. Specify null to use the System.Type.DefaultBinder . Parameter types: An array of System.Type objects. The elements in the array are of the same number, in the same order, and represent the same types as the parameters for the method to be returned. Parameter modifiers: The only defined value for this parameter is null . Returns: A System.Reflection.MethodInfo object that reflects the method defined in the type represented by the current instance that matches the specified criteria. If no method matching the specified criteria is found, returns null . If the selected method is non-public, the type reflected by the current instance is from a loaded assembly, and the caller does not have permission to reflect on non-public objects in loaded assemblies, returns null. Throws: : More than one method matching the specified criteria was found. Throws: : name or types is null. -or- At least one of the elements in types is null. Throws: : types has more than one dimension. The following System.Reflection.BindingFlags are used to define which members to include in the search: The following System.Reflection.BindingFlags values can be used to change how the search works: For more information, see System.Reflection.BindingFlags. |
public System.Reflection.MethodInfo GetMethod(System.String name, System.Reflection.BindingFlags bindingAttr) Returns a System.Reflection.MethodInfo object that reflects the method that has the specified name and is defined in the type represented by the current instance. Parameter name: A System.String containing the name of the method to be returned. Parameter bindingAttr: A bitwise combination of System.Reflection.BindingFlags values that control the binding process. If zero is specified, this method returns null . Returns: A System.Reflection.MethodInfo object that reflects the method that is defined in the type represented by the current instance and matches the specified criteria, if found; otherwise, null. Throws: : More than one method matching the specified criteria was found. Throws: : name is null. The following System.Reflection.BindingFlags are used to define which members to include in the search: The following System.Reflection.BindingFlags values can be used to change how the search works: For more information, see System.Reflection.BindingFlags. This version of System.Type.GetMethod(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Reflection.CallingConventions,System.Type[],System.Reflection.ParameterModifier[]) is equivalent to System.Type.GetMethod(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Reflection.CallingConventions,System.Type[],System.Reflection.ParameterModifier[])(name, bindingAttr , null, null, null). |
public System.Reflection.MethodInfo GetMethod(System.String name) Returns a System.Reflection.MethodInfo object that reflects the public method that has the specified name and is defined in the type represented by the current instance. Parameter name: A System.String containing the name of the public method to be returned. Returns: A System.Reflection.MethodInfo object reflecting the public method that is defined in the type represented by the current instance and has the specified name, if found; otherwise, null. Throws: : More than one method matching the specified criteria was found. Throws: : name is null. The search for name is case-sensitive. This version of System.Type.GetMethod(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Reflection.CallingConventions,System.Type[],System.Reflection.ParameterModifier[]) is equivalent to System.Type.GetMethod(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Reflection.CallingConventions,System.Type[],System.Reflection.ParameterModifier[])(name, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance, null, null, null). |
public System.Reflection.MethodInfo GetMethod(System.String name, System.Type[] types) Returns a System.Reflection.MethodInfo object that reflects the public method defined in the type represented by the current instance that has the specified name and parameter information. Parameter name: A System.String containing the name of the public method to be returned. Parameter types: An array of System.Type objects. The elements in the array are of the same number, in the same order, and represent the same types as the parameters for the method to be returned. Returns: A System.Reflection.MethodInfo object reflecting the public method defined in the type represented by the current instance that matches the specified criteria. If no public method matching the specified criteria is found, returns null . Throws: : More than one method matching the specified criteria was found. Throws: : name or types is null. -or- At least one of the elements in types is null. Throws: : types has more than one dimension. The search for name is case-sensitive. This version of System.Type.GetMethod(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Reflection.CallingConventions,System.Type[],System.Reflection.ParameterModifier[]) is equivalent to System.Type.GetMethod(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Reflection.CallingConventions,System.Type[],System.Reflection.ParameterModifier[])(name, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance, null, types, null). |
public System.Reflection.MethodInfo GetMethod(System.String name, System.Type[] types, System.Reflection.ParameterModifier[] modifiers) Returns a System.Reflection.MethodInfo object that reflects the public method that has the specified name and is defined in the type represented by the current instance. Parameter name: A System.String containing the name of the public method to be returned. Parameter types: An array of System.Type objects. The elements in the array are of the same number, in the same order, and represent the same types as the parameters for the method to be returned. Parameter modifiers: The only defined value for this parameter is null . Returns: A System.Reflection.MethodInfo object reflecting the public method that is defined in the type represented by the current instance and matches the specified criteria, if found; otherwise, null. Throws: : More than one method matching the specified criteria was found. Throws: : name or types is null. -or- At least one of the elements in types is null. Throws: : types has more than one dimension. The default binder does not process modifier . The search for name is case-sensitive. This version of System.Type.GetMethod(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Reflection.CallingConventions,System.Type[],System.Reflection.ParameterModifier[]) is equivalent to System.Type.GetMethod(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Reflection.CallingConventions,System.Type[],System.Reflection.ParameterModifier[]) ( name, System.Reflection.BindingFlags.Public |System.Reflection.BindingFlags.Static |System.Reflection.BindingFlags.Instance , null, types , modifiers). |
public System.Reflection.MethodInfo[] GetMethods(System.Reflection.BindingFlags bindingAttr) Returns an array of System.Reflection.MethodInfo objects that reflect the methods defined in the type represented by the current instance that match the specified binding constraints. Parameter bindingAttr: A bitwise combination of System.Reflection.BindingFlags values that control the binding process. If zero is specified, this method returns null . Returns: An array of System.Reflection.MethodInfo objects reflecting the methods defined in the type represented by the current instance that match the constraints of bindingAttr . If no such methods found, returns an empty array. If the type represented by the current instance is from a loaded assembly and the caller does not have permission to reflect on non-public objects in loaded assemblies, returns only public methods. The following System.Reflection.BindingFlags are used to define which members to include in the search: The following System.Reflection.BindingFlags values can be used to change how the search works: For more information, see System.Reflection.BindingFlags. As described above. |
public System.Reflection.MethodInfo[] GetMethods() Returns the public methods defined in the type represented by the current instance. Returns: An array of System.Reflection.MethodInfo objects reflecting the public methods defined in the type represented by the current instance under the constraints of bindingAttr. If no methods matching the constraints are found, returns an empty array. This version of System.Type.GetMethods is equivalent to System.Type.GetMethods( System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public ). |
public System.Type GetNestedType(System.String name) Returns the public nested type defined in the type represented by the current instance Parameter name: A System.String containing the name of the public nested type to return. Specify the unqualified name of the nested type. For example, for a type B nested within A, if typeA represents the type object for A, the correct invocation is typeA.GetNestedType("B"). Returns: A System.Type object representing the public nested type with the specified name, if found; otherwise, null. Throws: : name is null. The search for name is case-sensitive. This version of System.Type.GetNestedTypes is equivalent to System.Type.GetNestedTypes(name, System.Reflection.BindingFlags.Public). |
public System.Type GetNestedType(System.String name, System.Reflection.BindingFlags bindingAttr) Returns a nested types defined in the type represented by the current instance that match the specified binding constraints. Parameter name: A System.String containing the name of the nested type to return. Specify the unqualified name of the nested type. For example, for a type B nested within A, if typeA represents the type object for A, the correct invocation is typeA.GetNestedType("B"). Parameter bindingAttr: A bitwise combination of System.Reflection.BindingFlags values that control the binding process. If zero is specified, this method returns null . Returns: A System.Type object representing the nested type that matches the specified criteria, if found; otherwise, null. If the selected nested type is non-public, the current instance represents a type contained in a loaded assembly and the caller does not have sufficient permissions, returns null . Throws: : name is null. The following System.Reflection.BindingFlags are used to define which members to include in the search: The following System.Reflection.BindingFlags values can be used to change how the search works: For more information, see System.Reflection.BindingFlags. |
public System.Type[] GetNestedTypes() Returns all the public types nested within the current System.Type. Returns: An array of System.Type objects representing all public types nested within the type represented by the current instance, if any. Otherwise, returns an empty System.Type array. This version of System.Type.GetNestedTypes is equivalent to System.Type.GetNestedTypes(System.Reflection.BindingFlags.Public). |
public System.Type[] GetNestedTypes(System.Reflection.BindingFlags bindingAttr) Returns an array containing the nested types defined in the type represented by the current instance that match the specified binding constraints. Parameter bindingAttr: A bitwise combination of System.Reflection.BindingFlags values that control the binding process. If zero is specified, this method returns null . Returns: An array of System.Type objects representing all types nested within the type represented by the current instance that match the specified binding constraints, if any. Otherwise, returns an empty System.Type array. If the type reflected by the current instance is contained in a loaded assembly, the type that matches the specified criteria is not public, and the caller does not have sufficient permission, returns only public types. The following System.Reflection.BindingFlags are used to define which members to include in the search: The following System.Reflection.BindingFlags values can be used to change how the search works: For more information, see System.Reflection.BindingFlags. |
public System.Reflection.PropertyInfo[] GetProperties(System.Reflection.BindingFlags bindingAttr) Returns an array of System.Reflection.PropertyInfo objects that reflect the properties defined for the type represented by the current instance that match the specified binding constraints. Parameter bindingAttr: A bitwise combination of System.Reflection.BindingFlags values that control the binding process. If zero is specified, this method returns null . Returns: An array of System.Reflection.PropertyInfo objects that reflect the properties defined in the type represented by the current instance and match the constraints of bindingAttr . If no matching properties are found, returns an empty array. If the type represented by the current instance is from a loaded assembly and the caller does not have permission to reflect on non-public objects in loaded assemblies, returns only public properties. The following System.Reflection.BindingFlags are used to define which members to include in the search: The following System.Reflection.BindingFlags values can be used to change how the search works: For more information, see System.Reflection.BindingFlags. A property is considered by reflection to be public if it has at least one accessor that is public . Otherwise, the property is not public. |
public System.Reflection.PropertyInfo[] GetProperties() Returns an array of System.Reflection.PropertyInfo objects that reflect the public properties defined in the type represented by the current instance. Returns: An array of System.Reflection.PropertyInfo objects that reflect the public properties defined in the type represented by the current instance. If no public properties are found, returns an empty array. This version of System.Type.GetProperties(System.Reflection.BindingFlags) is equivalent to System.Type.GetProperties(System.Reflection.BindingFlags)( System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public ). A property is considered by reflection to be public if it has at least one accessor that is public. Otherwise, the property is considered to be not public. |
public System.Reflection.PropertyInfo GetProperty(System.String name, System.Type returnType, System.Type[] types) Returns a System.Reflection.PropertyInfo object that reflects the public property defined in the type represented by the current instance that matches the specified search criteria. Parameter name: A System.String containing the name of the public property to be returned. Parameter returnType: A System.Type object that represents the type of the public property to be returned. Parameter types: An array of System.Type objects. The elements in the array are of the same number, in the same order, and represent the same types as the parameters for the indexer to be returned. Specify System.Type.EmptyTypes for a property that is not indexed. Returns: A System.Reflection.PropertyInfo object reflecting the public property defined in the type represented by the current instance that matches the specified criteria. If no matching property is found, returns null . Throws: : More than one property matching the specified criteria was found. Throws: : name or types is null, or at least one of the elements in types is null. Throws: : types has more than one dimension. This version of System.Type.GetProperty(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Type,System.Type[],System.Reflection.ParameterModifier[]) is equivalent to System.Type.GetPropertyImpl(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Type,System.Type[],System.Reflection.ParameterModifier[])(name, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public, null, returnTypes, types, null). The search for name is case-sensitive. Different programming languages use different syntax to specify indexed properties. Internally, this property is referred to by the name "Item" in the metadata. Therefore, any attempt to retrieve an indexed property using reflection is required to specify this internal name in order for the PropertyInfo to be returned correctly. |
public System.Reflection.PropertyInfo GetProperty(System.String name, System.Type[] types) Returns a System.Reflection.PropertyInfo object that reflects the public property defined in the type represented by the current instance that matches the specified search criteria. Parameter name: A System.String containing the name of the public property to be returned. Parameter types: An array of System.Type objects. The elements in the array are of the same number, in the same order, and represent the same types as the parameters for the indexer to be returned. Specify System.Type.EmptyTypes to obtain a property that is not indexed. Returns: A System.Reflection.PropertyInfo object reflecting the public property defined on the type represented by the current instance that matches the specified criteria. If no matching property is found, returns null . Throws: : More than one property matching the specified criteria was found. Throws: : name or types is null, or at least one of the elements in types is null. Throws: : types has more than one dimension. This version of System.Type.GetProperty(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Type,System.Type[],System.Reflection.ParameterModifier[]) is equivalent to System.Type.GetPropertyImpl(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Type,System.Type[],System.Reflection.ParameterModifier[])(name, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public, null, null, types, null). The search for name is case-sensitive. Different programming languages use different syntax to specify indexed properties. Internally, this property is referred to by the name "Item" in the metadata. Therefore, any attempt to retrieve an indexed property using reflection is required to specify this internal name in order for the PropertyInfo to be returned correctly. |
public System.Reflection.PropertyInfo GetProperty(System.String name, System.Type returnType) Returns a System.Reflection.PropertyInfo object that reflects the public property defined in the type represented by the current instance that matches the specified search criteria. Parameter name: A System.String containing the name of the property to be returned. Parameter returnType: A System.Type object that represents the type of the property to be returned. Returns: A System.Reflection.PropertyInfo object reflecting the public property defined on the type represented by the current instance that matches the specified criteria. If no matching property is found, returns null . Throws: : More than one property matching the specified criteria was found. Throws: : name is null. This version of System.Type.GetProperty(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Type,System.Type[],System.Reflection.ParameterModifier[]) is equivalent to System.Type.GetPropertyImpl(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Type,System.Type[],System.Reflection.ParameterModifier[])(name, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public, null, returnType, null, null). The search for name is case-sensitive. |
public System.Reflection.PropertyInfo GetProperty(System.String name) Returns a System.Reflection.PropertyInfo object that reflects the public property defined in the type represented by the current instance that has the specified name. Parameter name: A System.String containing the name of the property to be returned. Returns: A System.Reflection.PropertyInfo object reflecting the public property defined on the type represented by the current instance that has the specified name. If no matching property is found, returns null . Throws: : More than one property matching the specified criteria was found. Throws: : name is null. This version of System.Type.GetProperty(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Type,System.Type[],System.Reflection.ParameterModifier[]) is equivalent to System.Type.GetPropertyImpl(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Type,System.Type[],System.Reflection.ParameterModifier[])(name, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public, null, null, null, null). The search for name is case-sensitive. |
public System.Reflection.PropertyInfo GetProperty(System.String name, System.Reflection.BindingFlags bindingAttr) Returns a System.Reflection.PropertyInfo object that reflects the property defined in the type represented by the current instance that matches the specified search criteria. Parameter name: A System.String containing the name of the property to be returned. Parameter bindingAttr: A bitwise combination of System.Reflection.BindingFlags values that control the binding process. If zero is specified, this method returns null . Returns: A System.Reflection.PropertyInfo object reflecting the property defined in the type represented by the current instance that matches the specified criteria. If no matching property is found, returns null . If the type reflected by the current instance is contained in a loaded assembly, the property that matches the specified criteria is not public, and the caller does not have sufficient permission, returns null. Throws: : More than one property matching the specified criteria was found. Throws: : name is null. The following System.Reflection.BindingFlags are used to define which members to include in the search: The following System.Reflection.BindingFlags values can be used to change how the search works: For more information, see System.Reflection.BindingFlags. This version of System.Type.GetProperty(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Type,System.Type[],System.Reflection.ParameterModifier[]) is equivalent to System.Type.GetPropertyImpl(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Type,System.Type[],System.Reflection.ParameterModifier[])(name, bindingAttr, null, null, null, null). The search for name is case-sensitive. |
public System.Reflection.PropertyInfo GetProperty(System.String name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Type returnType, System.Type[] types, System.Reflection.ParameterModifier[] modifiers) Returns a System.Reflection.PropertyInfo object that reflects the property defined in the type represented by the current instance that matches the specified search criteria . Parameter name: A System.String containing the name of the property to be returned. Parameter bindingAttr: A bitwise combination of System.Reflection.BindingFlags values that control the binding process. If zero is specified, this method returns null . Parameter binder: A System.Reflection.Binder object that defines a set of properties and enables the binding, coercion of argument types, and invocation of members using reflection. Specify null to use the System.Type.DefaultBinder . Parameter returnType: A System.Type object that represents the type of the property to be returned. Parameter types: An array of System.Type objects. The elements in the array are of the same number, in the same order, and represent the same types as the parameters for the indexer to be returned. Specify System.Type.EmptyTypes to obtain a property that is not indexed. Parameter modifiers: The only defined value for this parameter is null . Returns: A System.Reflection.PropertyInfo object reflecting the property that is defined in the type represented by the current instance and matches the specified criteria. If no matching property is found, returns null . If the type reflected by the current instance is contained in a loaded assembly, the property that matches the specified criteria is not public, and the caller does not have sufficient permission, returns null. Throws: : More than one property matching the specified criteria was found. Throws: : name or types is null, or at least one of the elements in types is null. Throws: : types has more than one dimension. The following System.Reflection.BindingFlags are used to define which members to include in the search: The following System.Reflection.BindingFlags values can be used to change how the search works: For more information, see System.Reflection.BindingFlags. This version of System.Type.GetProperty(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Type,System.Type[],System.Reflection.ParameterModifier[]) is equivalent to System.Type.GetPropertyImpl(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Type,System.Type[],System.Reflection.ParameterModifier[])(name, bindingAttr, binder, returnType, types, modifiers). The search for name is case-sensitive. Different programming languages use different syntax to specify indexed properties. Internally, this property is referred to by the name "Item" in the metadata. Therefore, any attempt to retrieve an indexed property using reflection is required to specify this internal name in order for the PropertyInfo to be returned correctly. |
protected System.Reflection.PropertyInfo GetPropertyImpl(System.String name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Type returnType, System.Type[] types, System.Reflection.ParameterModifier[] modifiers) When overridden in a derived class implements the System.Type.GetProperty(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Type,System.Type[],System.Reflection.ParameterModifier[]) method and returns a System.Reflection.PropertyInfo object that reflects the property defined in the type represented by the current instance that matches the specified search criteria. Parameter name: A System.String containing the name of the property to be returned. Parameter bindingAttr: A bitwise combination of System.Reflection.BindingFlags values that control the binding process. If zero is specified, this method returns null . Parameter binder: A System.Reflection.Binder object that defines a set of properties and enables the binding, coercion of argument types, and invocation of members using reflection. Specify null to use the System.Type.DefaultBinder . Parameter returnType: A System.Type object that represents the type of the property to be returned. Parameter types: An array of System.Type objects. The elements in the array are of the same number, in the same order, and represent the same types as the parameters for the indexer to be returned. Specify System.Type.EmptyTypes to obtain a property that is not indexed. Parameter modifiers: The only defined value for this parameter is null . Returns: A System.Reflection.PropertyInfo object representing the property that matches the specified search criteria, if found; otherwise, null. If the type reflected by the current instance is from a loaded assembly, the matching property is not public, and the caller does not have permission to reflect on non-public objects in loaded assemblies, returns null. Throws: : More than one property matching the specified criteria was found. Throws: : name or types is null, or at least one of the elements in types is null. Throws: : types has more than one dimension. The following System.Reflection.BindingFlags are used to define which members to include in the search: The following System.Reflection.BindingFlags values can be used to change how the search works: For more information, see System.Reflection.BindingFlags. Different programming languages use different syntax to specify indexed properties. Internally, this property is referred to by the name "Item" in the metadata. Therefore, any attempt to retrieve an indexed property using reflection is required to specify this internal name in order for the PropertyInfo to be returned correctly. |
public static System.Type GetType(System.String typeName, System.Boolean throwOnError, System.Boolean ignoreCase) Returns the System.Type with the specified name, optionally performing a case-sensitive search and throwing an exception if an error occurs while loading the System.Type. Parameter typeName: A System.String containing the name of the System.Type to return. Parameter throwOnError: A System.Boolean. Specify true to throw a System.TypeLoadException if an error occurs while loading the System.Type. Specify false to ignore errors while loading the System.Type. Parameter ignoreCase: A System.Boolean. Specify true to perform a case-insensitive search for typeName. Specify false to perform a case-sensitive search for typeName . Returns: The System.Type with the specified name, if found; otherwise, null. If the requested type is non-public and the caller does not have permission to reflect non-public objects outside the current assembly, this method returns null. Throws: : typeName is null. Throws: : A type initializer was invoked and threw an exception. Throws: : throwOnError is true and an error was encountered while loading the selected System.Type. typeName can be a simple type name, a fully qualified name, or a complex name that includes an assembly name. System.Type.AssemblyQualifiedName returns a fully qualified type name including nested types and the assembly name. If typeName includes only the name of the System.Type, this method searches in the calling object's assembly, then in the mscorlib.dll assembly. If typeName is fully qualified with the partial or complete assembly name, this method searches in the specified assembly. The following table shows calls to GetType for various types. |
public static System.Type GetType(System.String typeName, System.Boolean throwOnError) Returns the System.Type with the specified name, optionally throwing an exception if an error occurs while loading the System.Type . Parameter typeName: A System.String containing the case-sensitive name of the System.Type to return. Parameter throwOnError: A System.Boolean. Specify true to throw a System.TypeLoadException if an error occurs while loading the System.Type. Specify false to ignore errors while loading the System.Type. Returns: The System.Type with the specified name, if found; otherwise, null . If the requested type is non-public and the caller does not have permission to reflect non-public objects outside the current assembly, this method returns null . Throws: : typeName is null. Throws: : A type initializer was invoked and threw an exception. Throws: : throwOnError is true and an error was encountered while loading the System.Type. This method is equivalent to System.Type.GetType(System.String,System.Boolean,System.Boolean)(name, throwOnError, false). typeName can be a simple type name, a fully qualified name, or a complex name that includes an assembly name specification. If typeName includes only the name of the System.Type, this method searches in the calling object's assembly, then in the mscorlib.dll assembly. If typeName is fully qualified with the partial or complete assembly name, this method searches in the specified assembly. System.Type.AssemblyQualifiedName can return a fully qualified type name including nested types and the assembly name. For complete details, see System.Type.GetType(System.String,System.Boolean,System.Boolean)(System.String, System.Boolean, System.Boolean). |
public static System.Type GetType(System.String typeName) Returns the System.Type with the specified name. Parameter typeName: A System.String containing the case-sensitive name of the System.Type to return. Returns: The System.Type with the specified name, if found; otherwise, null. If the requested type is non-public and the caller does not have permission to reflect non-public objects outside the current assembly, this method returns null. Throws: : typeName is null. Throws: : A type initializer was invoked and threw an exception. This method is equivalent to System.Type.GetType(System.String,System.Boolean,System.Boolean)(name, false, false). typeName can be a simple type name, a type name that includes a namespace, or a complex name that includes an assembly name specification. If typeName includes only the name of the System.Type, this method searches in the calling object's assembly, then in the mscorlib.dll assembly. If typeName is fully qualified with the partial or complete assembly name, this method searches in the specified assembly. System.Type.AssemblyQualifiedName can return a fully qualified type name including nested types and the assembly name. For complete details, see System.Type.GetType(System.String,System.Boolean,System.Boolean)(System.String, System.Boolean, System.Boolean). |
public static System.Type[] GetTypeArray(System.Object[] args) Returns the types of the objects in the specified array. Parameter args: An array of objects whose types are to be returned. Returns: An array of System.Type objects representing the types of the corresponding elements in args. If a requested type is not public and the caller does not have permission to reflect non-public objects outside the current assembly, the corresponding element in the array returned by this method will be null. Throws: : args is null. Throws: : The type initializers were invoked and at least one threw an exception. |
public static System.Type GetTypeFromHandle(System.RuntimeTypeHandle handle) Gets the System.Type referenced by the specified type handle. Parameter handle: The System.RuntimeTypeHandle object that refers to the desired System.Type. Returns: The System.Type referenced by the specified System.RuntimeTypeHandle. Throws: : handle is null. Throws: : The requested type is non-public and outside the current assembly, and the caller does not have the required permission. Throws: : A type initializer was invoked and threw an exception. The handles are valid only in the application domain in which they were obtained. |
public static System.RuntimeTypeHandle GetTypeHandle(System.Object o) Returns the handle for the System.Type of the specified object. Parameter o: The object for which to get the type handle. Returns: The System.RuntimeTypeHandle for the System.Type of the specified System.Object. The handle is valid only in the application domain in which it was obtained. |
protected System.Boolean HasElementTypeImpl() When overridden in a derived class, implements the System.Type.HasElementType property and determines whether the current System.Type encompasses or refers to another type; that is, whether the current System.Type is an array, a pointer, or is passed by reference. Returns: true if the System.Type is an array, a pointer, or is passed by reference; otherwise, false. For example, System.Type.GetType(System.String,System.Boolean,System.Boolean) ("System.Int32[]").HasElementTypeImpl returns true, but System.Type.GetType(System.String,System.Boolean,System.Boolean) ("System.Int32").HasElementTypeImpl returns false. System.Type.HasElementTypeImpl also returns true for "System.Int32*" and "System.Int32&". |
public System.Object InvokeMember(System.String name, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object target, System.Object[] args, System.Globalization.CultureInfo culture) Invokes the specified member, using the specified binding constraints and matching the specified argument list and culture. Parameter name: A System.String containing the name of the constructor or method to invoke, or property or field to access. If the type represented by the current instance has a default member, specify System.String.Empty to invoke that member. For more information on default members, see System.Reflection.DefaultMemberAttribute . Parameter invokeAttr: A bitwise combination of System.Reflection.BindingFlags values that control the binding process. If zero is specified, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance is used by default. Parameter binder: A System.Reflection.Binder object that defines a set of properties and enables the binding, coercion of argument types, and invocation of members using reflection. Specify null to use the System.Type.DefaultBinder . Parameter target: A System.Object on which to invoke the member that matches the other specified criteria. If the matching member is static , this parameter is ignored. Parameter args: An array of objects containing the arguments to pass to the member to be invoked. The elements of this array are of the same number and in the same order by assignment-compatible type as specified by the contract of the member to be bound. Specify an empty array or null for a member that has no parameters. Parameter culture: The only defined value for this parameter is null . Returns: A System.Object containing the return value of the invoked member. If the invoked member does not have a return value, returns a System.Object containing System.Void . Throws: : n ame is null. Throws: : args has more than one dimension. -or- invokeAttr is not a valid System.Reflection.BindingFlags value. -or- The member to be invoked is a constructor and System.Reflection.BindingFlags.CreateInstance is not specified in invokeAttr. -or- The member to be invoked is a method that is not a type initializer or instance constructor, and System.Reflection.BindingFlags.InvokeMethod is not specified in invokeAttr. -or- The member to be accessed is a field, and neither System.Reflection.BindingFlags.GetField nor System.Reflection.BindingFlags.SetField is specified in invokeAttr. -or- The member to be accessed is a property, and neither System.Reflection.BindingFlags.GetProperty nor System.Reflection.BindingFlags.SetProperty is specified in invokeAttr. -or- invokeAttr contains System.Reflection.BindingFlags.CreateInstance and at least one of System.Reflection.BindingFlags.InvokeMethod, System.Reflection.BindingFlags.GetField, System.Reflection.BindingFlags.SetField, System.Reflection.BindingFlags.GetProperty, or System.Reflection.BindingFlags.SetProperty. -or- invokeAttr contains both System.Reflection.BindingFlags.GetField and System.Reflection.BindingFlags.SetField. -or- invokeAttr contains both System.Reflection.BindingFlags.GetProperty and System.Reflection.BindingFlags.SetProperty. -or- invokeAttr contains System.Reflection.BindingFlags.InvokeMethodand at least one of System.Reflection.BindingFlags.SetField or System.Reflection.BindingFlags.SetProperty. -or- invokeAttr contains System.Reflection.BindingFlags.SetField and args has more than one element. Throws: : A field or property matching the specified criteria was not found. Throws: : A method matching the specified criteria was not found. Throws: : The requested member is non-public and the caller does not have the required permission. Throws: : The member matching the specified criteria cannot be invoked on target. Throws: : The member matching the specified criteria threw an exception. Throws: : More than one member matches the specified criteria. This version of System.Type.InvokeMember(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object,System.Object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[]) is equivalent to System.Type.InvokeMember(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object,System.Object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[])( name, invokeAttr, binder, target, args, null, culture, null ). Example: For an example that demonstrates System.Type.InvokeMember(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object,System.Object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[]) , see System.Type.InvokeMember(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object,System.Object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[])( System.String, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object, System.Object[], System.Reflection.ParameterModifier[], System.Globalization.CultureInfo, System.String[]). |
public System.Object InvokeMember(System.String name, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object target, System.Object[] args) Invokes the specified member, using the specified binding constraints and matching the specified argument list. Parameter name: A System.String containing the name of the constructor or method to invoke, or property or field to access. If the type represented by the current instance has a default member, specify System.String.Empty to invoke that member. For more information on default members, see System.Reflection.DefaultMemberAttribute . Parameter invokeAttr: A bitwise combination of System.Reflection.BindingFlags values that control the binding process. If zero is specified, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance is used by default. Parameter binder: A System.Reflection.Binder object that defines a set of properties and enables the binding, coercion of argument types, and invocation of members using reflection. Specify null to use the System.Type.DefaultBinder . Parameter target: A System.Object on which to invoke the member that matches the other specified criteria. If the matching member is static , this parameter is ignored. Parameter args: An array of objects containing the arguments to pass to the member to be invoked. The elements of this array are of the same number and in the same order by assignment-compatible type as specified by the contract of the member to be bound. Specify an empty array or null for a member that has no parameters. Returns: A System.Object containing the return value of the invoked member. If the invoked member does not have a return value, returns a System.Object containing System.Void . Throws: : name is null. Throws: : args has more than one dimension. -or- invokeAttr is not a valid System.Reflection.BindingFlags value. -or- The member to be invoked is a constructor and System.Reflection.BindingFlags.CreateInstance is not specified in invokeAttr. -or- The member to be invoked is a method that is not a type initializer or instance constructor, and System.Reflection.BindingFlags.InvokeMethod is not specified in invokeAttr. -or- The member to be accessed is a field, and neither System.Reflection.BindingFlags.GetField nor System.Reflection.BindingFlags.SetField is specified in invokeAttr. -or- The member to be accessed is a property, and neither System.Reflection.BindingFlags.GetProperty nor System.Reflection.BindingFlags.SetProperty is specified in invokeAttr. -or- invokeAttr contains System.Reflection.BindingFlags.CreateInstance and at least one of System.Reflection.BindingFlags.InvokeMethod, System.Reflection.BindingFlags.GetField, System.Reflection.BindingFlags.SetField, System.Reflection.BindingFlags.GetProperty, or System.Reflection.BindingFlags.SetProperty. -or- invokeAttr contains both System.Reflection.BindingFlags.GetField and System.Reflection.BindingFlags.SetField. -or- invokeAttr contains both System.Reflection.BindingFlags.GetProperty and System.Reflection.BindingFlags.SetProperty. -or- invokeAttr contains System.Reflection.BindingFlags.InvokeMethod and at least one of System.Reflection.BindingFlags.SetField or System.Reflection.BindingFlags.SetProperty. -or- invokeAttr contains System.Reflection.BindingFlags.SetField and args has more than one element. Throws: : A field or property matching the specified criteria was not found. Throws: : A method matching the specified criteria cannot be found. Throws: : The requested member is non-public and the caller does not have the required permission. Throws: : The member matching the specified criteria cannot be invoked on target. Throws: : The member matching the specified criteria threw an exception. Throws: : More than one member matches the specified criteria. This version of System.Type.InvokeMember(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object,System.Object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[]) is equivalent to System.Type.InvokeMember(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object,System.Object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[])(name, invokeAttr, binder, target, args, null, null, null). For a demonstration of the use of System.Type.InvokeMember(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object,System.Object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[]), see the example for System.Type.InvokeMember(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object,System.Object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[])( System.String, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object, System.Object[], System.Reflection.ParameterModifier[], System.Globalization.CultureInfo, System.String[]). |
public System.Object InvokeMember(System.String name, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object target, System.Object[] args, System.Reflection.ParameterModifier[] modifiers, System.Globalization.CultureInfo culture, System.String[] namedParameters) Invokes or accesses a member defined on the type represented by the current instance that matches the specified binding criteria. Parameter name: A System.String containing the name of the constructor or method to invoke, or property or field to access. If the type represented by the current instance has a default member, specify System.String.Empty to invoke that member. For more information on default members, see System.Reflection.DefaultMemberAttribute . Parameter invokeAttr: A bitwise combination of System.Reflection.BindingFlags values that control the binding process. If zero is specified, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance is used by default. Parameter binder: A System.Reflection.Binder that defines a set of properties, and enables the binding, coercion of argument types, and invocation of members using reflection. Specify null to useSystem.Type.DefaultBinder. Parameter target: A System.Object on which to invoke the member that matches the other specified criteria. If the matching member is static , this parameter is ignored. Parameter args: An array of objects containing the arguments to pass to the member to be invoked. The elements of this array are of the same number and in the same order by assignment-compatible type as specified by the contract of the member to be bound if and only if nameParameters is null. If namedParameters is not null, the order of the elements in args corresponds to the order of the parameters specified in namedParameters. Specify an empty array or null for a member that takes no parameters. Parameter modifiers: The only defined value for this parameter is null . Parameter culture: The only defined value for this parameter is null . Parameter namedParameters: An array of System.String objects containing the names of the parameters to which the values in args are passed. These names are processed in a case-sensitive manner and have a one-to-one correspondence with the elements of args. Specify an empty array or null for a member that takes no parameters. Specify null to have this parameter ignored. Returns: A System.Object containing the return value of the invoked or accessed member. If the member does not have a return value, returns a System.Object containing System.Void . Throws: : name is null. Throws: : args has more than one dimension. -or- invokeAttr is not a valid System.Reflection.BindingFlags value. -or- The member to be invoked is a constructor and System.Reflection.BindingFlags.CreateInstance is not specified in invokeAttr. -or- The member to be invoked is a method that is not a type initializer or instance constructor, and System.Reflection.BindingFlags.InvokeMethod is not specified in invokeAttr. -or- The member to be accessed is a field, and neither System.Reflection.BindingFlags.GetField nor System.Reflection.BindingFlags.SetField is specified in invokeAttr. -or- The member to be accessed is a property, and neither System.Reflection.BindingFlags.GetProperty nor System.Reflection.BindingFlags.SetProperty is specified in invokeAttr. -or- invokeAttr contains System.Reflection.BindingFlags.CreateInstance and at least one of System.Reflection.BindingFlags.InvokeMethod, System.Reflection.BindingFlags.GetField, System.Reflection.BindingFlags.SetField, System.Reflection.BindingFlags.GetProperty, or System.Reflection.BindingFlags.SetProperty. -or- invokeAttr contains both System.Reflection.BindingFlags.GetField and System.Reflection.BindingFlags.SetField. -or- invokeAttr contains both System.Reflection.BindingFlags.GetProperty and System.Reflection.BindingFlags.SetProperty. -or- invokeAttr contains System.Reflection.BindingFlags.InvokeMethod and at least one of System.Reflection.BindingFlags.SetField or System.Reflection.BindingFlags.SetProperty. -or- invokeAttr contains System.Reflection.BindingFlags.SetField and args has more than one element. -or- namedParameters.Length > args.Length . -or- At least one element in namedParameters is null. -or- At least one element in args is not assignment-compatible with the corresponding parameter in namedParameters. Throws: : A field or property matching the specified criteria was not found. Throws: : A method matching the specified criteria cannot be found. Throws: : The requested member is non-public and the caller does not have the required permission. Throws: : The member matching the specified criteria cannot be invoked on target. Throws: : The member matching the specified criteria threw an exception. Throws: : More than one member matches the specified criteria. System.Type.InvokeMember(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object,System.Object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[]) calls a constructor or a method , gets or sets a property , gets or sets a field, or gets or sets an element of an array. The binder finds all of the matching members. These members are found based upon the type of binding specified by invokeAttr . The System.Reflection.Binder.BindToMethod(System.Reflection.BindingFlags,System.Reflection.MethodBase[],System.Object[]@,System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[],System.Object@) is responsible for selecting the method to be invoked. The default binder selects the most specific match. The set of members is then filtered by name, number of arguments, and a set of search modifiers defined in the binder. After the member is selected, it is invoked or accessed. Accessibility is checked at that point. Access restrictions are ignored for fully trusted code; that is, private constructors, methods, fields, and properties can be accessed and invoked via reflection whenever the code is fully trusted. The following System.Reflection.BindingFlags are used to define which members to include in the search: The following System.Reflection.BindingFlags values can be used to change how the search works: For more information, see System.Reflection.BindingFlags. Each parameter in the namedParameters array is assigned the value in the corresponding element in the args array. If the length of args is greater than the length of namedParameters, the remaining argument values are passed in order. A member will be found only if the number of parameters in the member declaration equals the number of arguments in the args array (unless default arguments are defined on the member). Also, The type of each argument is required to be convertible by the binder to the type of the parameter. It is required that the caller specify values for invokeAttr as follows: For more information, see System.Reflection.BindingFlags . System.Type.InvokeMember(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object,System.Object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[]) can be used to invoke methods with parameters that have default values. To bind to these methods, System.Reflection.BindingFlags.OptionalParamBinding must be specified. For a parameter that has a default value, the caller can supply a value or supply System.Type.Missing to use the default value. System.Type.InvokeMember(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object,System.Object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[]) can be used to set a field to a particular value by specifying System.Reflection.BindingFlags.SetField . For example, to set a public instance field named F on class C, where F is a string, the value is set using the following statement: typeof(C).InvokeMember("F", BindingFlags.SetField, null, C, new Object{ "strings new value"}, null, null, null); A string array F can be initialized as follows: typeof(C).InvokeMember("F", BindingFlags.SetField, null, C, new Object{new String[]{"a","z","c","d"}, null, null, null); Use System.Type.InvokeMember(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object,System.Object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[]) to set the value of an element in an array by specifying the index of the value and the new value for the element as follows: typeof(C).InvokeMember("F", BindingFlags.SetField, null, C, new Object{1, "b"}, null, null, null); The preceding statement changes "z" in array F to "b". Example: The following example demonstrates the use of System.Type.InvokeMember(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object,System.Object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[]) to construct a System.String, obtain its System.String.Length property, invoke System.String.Insert(System.Int32,System.String) on it, and then set its value using the System.String.Empty field. using System;
using System.Reflection;
class InvokeMemberExample
{
static void Main(string[] args)
{
// Create the parameter arrays that will
// be passed to InvokeMember.
char[] cAry =
new char[] {'A',' ','s','t','r','i','n','g'};
object[] oAry = new object[] {cAry, 0, cAry.Length};
Type t = typeof(string);
// Invoke the constructor of a string.
string str =
(string)t.InvokeMember(null, BindingFlags.Instance |
BindingFlags.Public | BindingFlags.CreateInstance, null,
null, oAry, null, null, null);
Console.WriteLine("The string is \"{0}\".", str);
// Access a property of the string.
int i =
(int) t.InvokeMember("Length", BindingFlags.Instance |
BindingFlags.Public | BindingFlags.GetProperty, null,
str, null, null, null, null);
Console.WriteLine("The length of the string is {0}.", i);
// Invoke a method on the string.
string newStr = "new ";
object[] oAry2 = new Object[] {2, newStr};
str = (string) t.InvokeMember("Insert", BindingFlags.Instance |
BindingFlags.Public | BindingFlags.InvokeMethod, null, str,
oAry2, null, null, null);
Console.WriteLine("The modified string is \"{0}\".", str);
// Access a field of the string.
str = (string) t.InvokeMember("Empty", BindingFlags.Static |
BindingFlags.Public | BindingFlags.GetField, null, str,
null);
Console.WriteLine("The empty string is \"{0}\".", str);
}
}
The output is The string is "A string". The length of the string is 8. The modified string is "A new string" The empty string is "". |
protected System.Boolean IsArrayImpl() When overridden in a derived class implements the System.Type.IsArray property returning a System.Boolean value that indicates whether the type represented by the current instance is an array. Returns: true if the System.Type is an array; otherwise, false. An instance of the System.Array class is required to return false because it is an object, not an array.As described above. |
public virtual System.Boolean IsAssignableFrom(System.Type c) Determines whether an instance of the current System.Type can be assigned from an instance of the specified System.Type . Parameter c: The System.Type to compare with the current System.Type . Returns: false if c is a null reference. true if one or more of the following statements are true; otherwise false. Example: The following example demonstrates the System.Type.IsAssignableFrom(System.Type) method using arrays. using System;
class ArrayTypeTest {
public static void Main() {
int i = 1;
int [] array10 = new int [10];
int [] array2 = new int[2];
int [,]array22 = new int[2,2];
int [,]array24 = new int[2,4];
int [,,]array333 = new int[3,3,3];
Type array10Type = array10.GetType();
Type array2Type = array2.GetType();
Type array22Type = array22.GetType();
Type array24Type = array24.GetType();
Type array333Type = array333.GetType();
// If X and Y are not both arrays, then false
Console.WriteLine("int[2] is assignable from int? {0} ", array2Type.IsAssignableFrom(i.GetType()));
// If X and Y have same type and rank, then true.
Console.WriteLine("int[2] is assignable from int[10]? {0} ", array2Type.IsAssignableFrom(array10Type));
Console.WriteLine("int[2,2] is assignable from int[2,4]? {0}", array22Type.IsAssignableFrom(array24Type));
Console.WriteLine("int[2,4] is assignable from int[2,2]? {0}", array24Type.IsAssignableFrom(array22Type));
Console.WriteLine("");
// If X and Y do not have the same rank, then false.
Console.WriteLine("int[2,2] is assignable from int[10]? {0}", array22Type.IsAssignableFrom(array10Type));
Console.WriteLine("int[2,2] is assignable from int[3,3,3]? {0}", array22Type.IsAssignableFrom(array333Type));
Console.WriteLine("int[3,3,3] is assignable from int[2,2]? {0}", array333Type.IsAssignableFrom(array22Type));
}
}
The output is int[2] is assignable from int? False int[2] is assignable from int[10]? True int[2,2] is assignable from int[2,4]? True int[2,4] is assignable from int[2,2]? True int[2,2] is assignable from int[10]? False int[2,2] is assignable from int[3,3,3]? False int[3,3,3] is assignable from int[2,2]? False |
protected System.Boolean IsByRefImpl() When overridden in a derived class, implements the System.Type.IsByRef property and determines whether the System.Type is passed by reference. Returns: true if the System.Type is passed by reference; otherwise, false. As described above. |
protected System.Boolean IsCOMObjectImpl() Reserved. Returns: false This abstract method is required to be present for legacy implementations. Conforming implementations are permitted to throw the System.NotSupportedException as their implementation. |
public virtual System.Boolean IsInstanceOfType(System.Object o) Determines whether the specified object is an instance of the current System.Type. Parameter o: The object to compare with the current System.Type. Returns: true if either of the following statements is true; otherwise false. If o is a null reference, returns false. As described above. Example: The following example demonstrates the System.Type.IsInstanceOfType(System.Object) method. using System;
public interface IFoo { }
public class MyClass : IFoo {}
public class MyDerivedClass : MyClass {}
class IsInstanceTest {
public static void Main() {
Type ifooType=typeof(IFoo);
MyClass mc = new MyClass();
Type mcType = mc.GetType();
MyClass mdc = new MyDerivedClass();
Type mdcType = mdc.GetType();
int [] array = new int [10];
Type arrayType = typeof(Array);
Console.WriteLine("int[] is instance of Array? {0}", arrayType.IsInstanceOfType(array));
Console.WriteLine("myclass instance is instance of MyClass? {0}", mcType.IsInstanceOfType(mc));
Console.WriteLine("myderivedclass instance is instance of MyClass? {0}", mcType.IsInstanceOfType(mdc));
Console.WriteLine("myclass instance is instance of IFoo? {0}", ifooType.IsInstanceOfType(mc));
Console.WriteLine("myderivedclass instance is instance of IFoo? {0}", ifooType.IsInstanceOfType(mdc));
}
}
The output is int[] is instance of Array? True myclass instance is instance of MyClass? True myderivedclass instance is instance of MyClass? True myclass instance is instance of IFoo? True myderivedclass instance is instance of IFoo? True |
protected System.Boolean IsPointerImpl() When overridden in a derived class, implements the System.Type.IsPointer property and determines whether the System.Type is a pointer. Returns: true if the System.Type is a pointer; otherwise, false. As described above. |
protected System.Boolean IsPrimitiveImpl() When overridden in a derived class, implements the System.Type.IsPrimitive property and determines whether the System.Type is one of the primitive types. Returns: true if the System.Type is one of the primitive types; otherwise, false. This method returns true if the underlying type of the current instance is one of the following: System.Boolean, System.Byte, System.SByte, System.Int16, System.UInt16, System.Int32, System.UInt32, System.Int64, System.UInt64, System.Char, System.Double, and System.Single. |
public virtual System.Boolean IsSubclassOf(System.Type c) Determines whether the current System.Type derives from the specified System.Type . Parameter c: The System.Type to compare with the current System.Type. Returns: true if c and the current System.Type represent classes, and the class represented by the current System.Type derives from the class represented by c; otherwise false. Returns false if c and the current System.Type represent the same class. Example: The following example demonstrates the System.Type.IsSubclassOf(System.Type) method. using System;
public interface IFoo { }
public interface IBar:IFoo{}
public class MyClass : IFoo {}
public class MyDerivedClass : MyClass {}
class IsSubclassTest {
public static void Main() {
Type ifooType = typeof(IFoo);
Type ibarType = typeof(IBar);
MyClass mc = new MyClass();
Type mcType = mc.GetType();
MyClass mdc = new MyDerivedClass();
Type mdcType = mdc.GetType();
int [] array = new int [10];
Type arrayOfIntsType = array.GetType();
Type arrayType = typeof(Array);
Console.WriteLine("Array is subclass of int[]? {0}", arrayType.IsSubclassOf(arrayOfIntsType));
Console.WriteLine("int [] is subclass of Array? {0}", arrayOfIntsType.IsSubclassOf(arrayType));
Console.WriteLine("IFoo is subclass of IBar? {0}", ifooType.IsSubclassOf(ibarType));
Console.WriteLine("myclass is subclass of MyClass? {0}", mcType.IsSubclassOf(mcType));
Console.WriteLine("myderivedclass is subclass of MyClass? {0}", mdcType.IsSubclassOf(mcType));
}
}
The output is Array is subclass of int[]? False int [] is subclass of Array? True IFoo is subclass of IBar? False myclass is subclass of MyClass? False myderivedclass is subclass of MyClass? True |
public System.String ToString() Returns a System.String representation of the current System.Type. Returns: Returns System.Type.FullName . This method overrides System.Object.ToString . |
| Functions inherited from System.Object: |
|---|
public virtual System.Boolean Equals(System.Object obj) Determines whether the specified System.Object is equal to the current instance. Parameter obj: The System.Object to compare with the current instance. Returns: true if obj is equal to the current instance; otherwise, false. The statements listed below are required to be true for all implementations of the System.Object.Equals(System.Object) method. In the list, x, y, and z represent non-null object references. See System.Object.GetHashCode for additional required behaviors pertaining to the System.Object.Equals(System.Object) method. Implementations of System.Object.Equals(System.Object) should not throw exceptions. The System.Object.Equals(System.Object) method tests for referential equality , which means that System.Object.Equals(System.Object) returns true if the specified instance of Object and the current instance are the same instance; otherwise, it returns false . An implementation of the System.Object.Equals(System.Object) method is shown in the following C# code: public virtual bool Equals(Object obj) { return this == obj; } For some kinds of objects, it is desirable to have System.Object.Equals(System.Object) test for value equality instead of referential equality. Such implementations of Equals return true if the two objects have the same "value", even if they are not the same instance. The definition of what constitutes an object's "value" is up to the implementer of the type, but it is typically some or all of the data stored in the instance variables of the object. For example, the value of a System.String is based on the characters of the string; the Equals method of the System.String class returns true for any two string instances that contain exactly the same characters in the same order. When the Equals method of a base class provides value equality, an override of Equals in a class derived from that base class should invoke the inherited implementation of Equals . It is recommended (but not required) that types overriding System.Object.Equals(System.Object) also override System.Object.GetHashCode. Hashtables cannot be relied on to work correctly if this recommendation is not followed. If your programming language supports operator overloading, and if you choose to overload the equality operator for a given type, that type should override the Equals method. Such implementations of the Equals method should return the same results as the equality operator. Following this guideline will help ensure that class library code using Equals (such as System.Collections.ArrayList and System.Collections.Hashtable ) behaves in a manner that is consistent with the way the equality operator is used by application code. If you are implementing a value type, you should follow these guidelines: For reference types, the guidelines are as follows: If you implement System.IComparable on a given type, you should override Equals on that type. The System.Object.Equals(System.Object) method is called by methods in collections classes that perform search operations, including the System.Array.IndexOf(System.Array,System.Object) method and the System.Collections.ArrayList.Contains(System.Object) method. Example: Example 1: The following example contains two calls to the default implementation of System.Object.Equals(System.Object) . using System;
class MyClass {
static void Main() {
Object obj1 = new Object();
Object obj2 = new Object();
Console.WriteLine(obj1.Equals(obj2));
obj1 = obj2;
Console.WriteLine(obj1.Equals(obj2));
}
}
The output is False True Example 2: The following example shows a Point class that overrides the System.Object.Equals(System.Object) method to provide value equality and a class Point3D, which is derived from Point . Because Point's override of System.Object.Equals(System.Object) is the first in the inheritance chain to introduce value equality, the Equals method of the base class (which is inherited from System.Object and checks for referential equality) is not invoked. However, Point3D.Equals invokes Point.Equals because Point implements Equals in a manner that provides value equality. using System;
public class Point: object {
int x, y;
public override bool Equals(Object obj) {
//Check for null and compare run-time types.
if (obj == null || GetType() != obj.GetType()) return false;
Point p = (Point)obj;
return (x == p.x) && (y == p.y);
}
public override int GetHashCode() {
return x ^ y;
}
}
class Point3D: Point {
int z;
public override bool Equals(Object obj) {
return base.Equals(obj) && z == ((Point3D)obj).z;
}
public override int GetHashCode() {
return base.GetHashCode() ^ z;
}
}
The Point.Equals method checks that the obj
argument is non-null and that it references an instance of the same type as this
object. If either of those checks fail, the method returns false. The
System.Object.Equals(System.Object) method uses
System.Object.GetType to determine whether
the run-time types of the two objects are identical. (Note that
typeof is not used here because it returns the static type.) If
instead the method had used a check of the form
In Point3D.Equals , the inherited Equals method is invoked before anything else is done; the inherited Equals method checks to see that obj is non-null, that obj is an instance of the same class as this object, and that the inherited instance variables match. Only when the inherited Equals returns true does the method compare the instance variables introduced in the subclass. Specifically, the cast to Point3D is not executed unless obj has been determined to be of type Point3D or a subclass of Point3D . Example 3: In the previous example, operator == (the equality operator) is used to compare the individual instance variables. In some cases, it is appropriate to use the System.Object.Equals(System.Object) method to compare instance variables in an Equals implementation, as shown in the following example: using System;
class Rectangle {
Point a, b;
public override bool Equals(Object obj) {
if (obj == null || GetType() != obj.GetType()) return false;
Rectangle r = (Rectangle)obj;
//Use Equals to compare instance variables
return a.Equals(r.a) && b.Equals(r.b);
}
public override int GetHashCode() {
return a.GetHashCode() ^ b.GetHashCode();
}
}
Example 4: In some languages, such as C#, operator overloading is supported. When a type overloads operator ==, it should also override the System.Object.Equals(System.Object) method to provide the same functionality. This is typically accomplished by writing the Equals method in terms of the overloaded operator ==. For example: using System;
public struct Complex {
double re, im;
public override bool Equals(Object obj) {
return obj is Complex && this == (Complex)obj;
}
public override int GetHashCode() {
return re.GetHashCode() ^ im.GetHashCode();
}
public static bool operator ==(Complex x, Complex y) {
return x.re == y.re && x.im == y.im;
}
public static bool operator !=(Complex x, Complex y) {
return !(x == y);
}
}
Because Complex is a C# struct (a value type), it is known that there will be no subclasses of Complex . Therefore, the System.Object.Equals(System.Object) method need not compare the GetType() results for each object, but can instead use the is operator to check the type of the obj parameter. |
public static System.Boolean Equals(System.Object objA, System.Object objB) Determines whether two object references are equal. Parameter objA: First object to compare. Parameter objB: Second object to compare. Returns: true if one or more of the following statements is true: otherwise returns false. This static method checks for null references before it calls objA.Equals(objB ) and returns false if either objA or objB is null. If the Equals(object obj) implementation throws an exception, this method throws an exception. Example: The following example demonstrates the System.Object.Equals(System.Object) method. using System;
public class MyClass {
public static void Main() {
string s1 = "Tom";
string s2 = "Carol";
Console.WriteLine("Object.Equals(\"{0}\", \"{1}\") => {2}",
s1, s2, Object.Equals(s1, s2));
s1 = "Tom";
s2 = "Tom";
Console.WriteLine("Object.Equals(\"{0}\", \"{1}\") => {2}",
s1, s2, Object.Equals(s1, s2));
s1 = null;
s2 = "Tom";
Console.WriteLine("Object.Equals(null, \"{1}\") => {2}",
s1, s2, Object.Equals(s1, s2));
s1 = "Carol";
s2 = null;
Console.WriteLine("Object.Equals(\"{0}\", null) => {2}",
s1, s2, Object.Equals(s1, s2));
s1 = null;
s2 = null;
Console.WriteLine("Object.Equals(null, null) => {2}",
s1, s2, Object.Equals(s1, s2));
}
}
The output is Object.Equals("Tom", "Carol") => False Object.Equals("Tom", "Tom") => True Object.Equals(null, "Tom") => False Object.Equals("Carol", null) => False Object.Equals(null, null) => True |
public System.Void Finalize() Allows a System.Object to perform cleanup operations before the memory allocated for the System.Object is automatically reclaimed. During execution, System.Object.Finalize is automatically called after an object becomes inaccessible, unless the object has been exempted from finalization by a call to System.GC.SuppressFinalize(System.Object). During shutdown of an application domain, System.Object.Finalize is automatically called on objects that are not exempt from finalization, even those that are still accessible. System.Object.Finalize is automatically called only once on a given instance, unless the object is re-registered using a mechanism such as System.GC.ReRegisterForFinalize(System.Object) and System.GC.SuppressFinalize(System.Object) has not been subsequently called. Conforming implementations of the CLI are required to make every effort to ensure that for every object that has not been exempted from finalization, the System.Object.Finalize method is called after the object becomes inaccessible. However, there may be some circumstances under which Finalize is not called. Conforming CLI implementations are required to explicitly specify the conditions under which Finalize is not guaranteed to be called. For example, Finalize might not be guaranteed to be called in the event of equipment failure, power failure, or other catastrophic system failures. In addition to System.GC.ReRegisterForFinalize(System.Object) and System.GC.SuppressFinalize(System.Object), conforming implementations of the CLI are allowed to provide other mechanisms that affect the behavior of System.Object.Finalize . Any mechanisms provided are required to be specified by the CLI implementation. The order in which the Finalize methods of two objects are run is unspecified, even if one object refers to the other. The thread on which Finalize is run is unspecified. Every implementation of System.Object.Finalize in a derived type is required to call its base type's implementation of Finalize . This is the only case in which application code calls System.Object.Finalize . The System.Object.Finalize implementation does nothing. A type should implement Finalize when it uses unmanaged resources such as file handles or database connections that must be released when the managed object that uses them is reclaimed. Because Finalize methods may be invoked in any order (including from multiple threads), synchronization may be necessary if the Finalize method may interact with other objects, whether accessible or not. Furthermore, since the order in which Finalize is called is unspecified, implementers of Finalize (or of destructors implemented through overriding Finalize) must take care to correctly handle references to other objects, as their Finalize method may already have been invoked. In general, referenced objects should not be considered valid during finalization. See the System.IDisposable interface for an alternate means of disposing of resources. For C# developers: Destructors are the C# mechanism for performing cleanup operations. Destructors provide appropriate safeguards, such as automatically calling the base type's destructor. In C# code, System.Object.Finalize cannot be called or overridden. |
public virtual System.Int32 GetHashCode() Generates a hash code for the current instance. Returns: A System.Int32 containing the hash code for the current instance. System.Object.GetHashCode serves as a hash function for a specific type. A hash function is used to quickly generate a number (a hash code) corresponding to the value of an object. Hash functions are used with hashtables. A good hash function algorithm rarely generates hash codes that collide. For more information about hash functions, see The Art of Computer Programming , Vol. 3, by Donald E. Knuth. All implementations of System.Object.GetHashCode are required to ensure that for any two object references x and y, if x.Equals(y) == true, then x.GetHashCode() == y.GetHashCode(). Hash codes generated by System.Object.GetHashCode need not be unique. Implementations of System.Object.GetHashCode are not permitted to throw exceptions. The System.Object.GetHashCode implementation attempts to produce a unique hash code for every object, but the hash codes generated by this method are not guaranteed to be unique. Therefore, System.Object.GetHashCode may generate the same hash code for two different instances. It is recommended (but not required) that types overriding System.Object.GetHashCode also override System.Object.Equals(System.Object) . Hashtables cannot be relied on to work correctly if this recommendation is not followed. Use this method to obtain the hash code of an object. Hash codes should not be persisted (i.e. in a database or file) as they are allowed to change from run to run. Example: Example 1 In some cases, System.Object.GetHashCode is implemented to simply return an integer value. The following example illustrates an implementation of System.Int32.GetHashCode , which returns an integer value: using System;
public struct Int32 {
int value;
//other methods...
public override int GetHashCode() {
return value;
}
}
Example 2 Frequently, a type has multiple data members that can participate in generating the hash code. One way to generate a hash code is to combine these fields using an xor (exclusive or) operation, as shown in the following example: using System;
public struct Point {
int x;
int y;
//other methods
public override int GetHashCode() {
return x ^ y;
}
}
Example 3 The following example illustrates another case where the type's fields are combined using xor (exclusive or) to generate the hash code. Notice that in this example, the fields represent user-defined types, each of which implements System.Object.GetHashCode (and should implement System.Object.Equals(System.Object) as well): using System;
public class SomeType {
public override int GetHashCode() {
return 0;
}
}
public class AnotherType {
public override int GetHashCode() {
return 1;
}
}
public class LastType {
public override int GetHashCode() {
return 2;
}
}
public class MyClass {
SomeType a = new SomeType();
AnotherType b = new AnotherType();
LastType c = new LastType();
public override int GetHashCode () {
return a.GetHashCode() ^ b.GetHashCode() ^ c.GetHashCode();
}
}
Avoid implementing System.Object.GetHashCode in a manner that results in circular references. In other words, if AClass.GetHashCode calls BClass.GetHashCode, it should not be the case that BClass.GetHashCode calls AClass.GetHashCode. Example 4 In some cases, the data member of the class in which you are implementing System.Object.GetHashCode is bigger than a System.Int32. In such cases, you could combine the high order bits of the value with the low order bits using an XOR operation, as shown in the following example: using System;
public struct Int64 {
long value;
//other methods...
public override int GetHashCode() {
return ((int)value ^ (int)(value >> 32));
}
}
|
public System.Type GetType() Gets the type of the current instance. Returns: The instance of System.Type that represents the run-time type (the exact type) of the current instance. For two objects x and y that have identical run-time types, System.Object.ReferenceEquals(System.Object,System.Object)(x.GetType(),y.GetType()) returns true . Example: The following example demonstrates the fact that System.Object.GetType returns the run-time type of the current instance: using System;
public class MyBaseClass: Object {
}
public class MyDerivedClass: MyBaseClass {
}
public class Test {
public static void Main() {
MyBaseClass myBase = new MyBaseClass();
MyDerivedClass myDerived = new MyDerivedClass();
object o = myDerived;
MyBaseClass b = myDerived;
Console.WriteLine("mybase: Type is {0}", myBase.GetType());
Console.WriteLine("myDerived: Type is {0}", myDerived.GetType());
Console.WriteLine("object o = myDerived: Type is {0}", o.GetType());
Console.WriteLine("MyBaseClass b = myDerived: Type is {0}", b.GetType());
}
}
The output is mybase: Type is MyBaseClass myDerived: Type is MyDerivedClass object o = myDerived: Type is MyDerivedClass MyBaseClass b = myDerived: Type is MyDerivedClass |
protected System.Object MemberwiseClone() Creates a shallow copy of the current instance. Returns: A shallow copy of the current instance. The run-time type (the exact type) of the returned object is the same as the run-time type of the object that was copied. System.Object.MemberwiseClone creates a new instance of the same type as the current instance and then copies each of the object's non-static fields in a manner that depends on whether the field is a value type or a reference type. If the field is a value type, a bit-by-bit copy of all the field's bits is performed. If the field is a reference type, only the reference is copied. The algorithm for performing a shallow copy is as follows (in pseudo-code): for each instance field f in this instance if (f is a value type) bitwise copy the field if (f is a reference type) copy the reference end for loop This mechanism is referred to as a shallow copy because it copies rather than clones the non-static fields. Because System.Object.MemberwiseClone implements the above algorithm, for any object, a, the following statements are required to be true: System.Object.MemberwiseClone does not call any of the type's constructors. If System.Object.Equals(System.Object) has been overridden, a.MemberwiseClone().Equals(a) might return false . For an alternate copying mechanism, see System.ICloneable . System.Object.MemberwiseClone is protected (rather than public) to ensure that from verifiable code it is only possible to clone objects of the same class as the one performing the operation (or one of its subclasses). Although cloning an object does not directly open security holes, it does allow an object to be created without running any of its constructors. Since these constructors may establish important invariants, objects created by cloning may not have these invariants established, and this may lead to incorrect program behavior. For example, a constructor might add the new object to a linked list of all objects of this class, and cloning the object would not add the new object to that list -- thus operations that relied on the list to locate all instances would fail to notice the cloned object. By making the method protected, only objects of the same class (or a subclass) can produce a clone and implementers of those classes are (presumably) aware of the appropriate invariants and can arrange for them to be true without necessarily calling a constructor. Example: The following example shows a class called MyClass as well as a representation of the instance of MyClass returned by System.Object.MemberwiseClone . using System;
class MyBaseClass {
public static string CompanyName = "My Company";
public int age;
public string name;
}
class MyDerivedClass: MyBaseClass {
static void Main() {
//Create an instance of MyDerivedClass
//and assign values to its fields.
MyDerivedClass m1 = new MyDerivedClass();
m1.age = 42;
m1.name = "Sam";
//Do a shallow copy of m1
//and assign it to m2.
MyDerivedClass m2 = (MyDerivedClass) m1.MemberwiseClone();
}
}
A graphical representation of m1 and m2 might look like this
+---------------+
| 42 | m1
+---------------+
| +---------|-----------------> "Sam"
+---------------+ /|\
|
+---------------+ |
| 42 | | m2
+---------------+ |
| +--------|---------------------|
+---------------+
|
public static System.Boolean ReferenceEquals(System.Object objA, System.Object objB) Determines whether two object references are identical. Parameter objA: First object to compare. Parameter objB: Second object to compare. Returns: True if a and b refer to the same object or are both null references; otherwise, false. This static method provides a way to compare two objects for reference equality. It does not call any user-defined code, including overrides of System.Object.Equals(System.Object) . Example: using System;
class MyClass {
static void Main() {
object o = null;
object p = null;
object q = new Object();
Console.WriteLine(Object.ReferenceEquals(o, p));
p = q;
Console.WriteLine(Object.ReferenceEquals(p, q));
Console.WriteLine(Object.ReferenceEquals(o, p));
}
}
The output is True True False |
public virtual System.String ToString() Creates and returns a System.String representation of the current instance. Returns: A System.String representation of the current instance. System.Object.ToString returns a string whose content is intended to be understood by humans. Where the object contains culture-sensitive data, the string representation returned by System.Object.ToString takes into account the current system culture. For example, for an instance of the System.Double class whose value is zero, the implementation of System.Double.ToString might return "0.00" or "0,00" depending on the current UI culture. Although there are no exact requirements for the format of the returned string, it should as much as possible reflect the value of the object as perceived by the user. System.Object.ToString is equivalent to calling System.Object.GetType to obtain the System.Type object for the current instance and then returning the result of calling the System.Object.ToString implementation for that type. The value returned includes the full name of the type. It is recommended, but not required, that System.Object.ToString be overridden in a derived class to return values that are meaningful for that type. For example, the base data types, such as System.Int32, implement System.Object.ToString so that it returns the string form of the value the object represents. Subclasses that require more control over the formatting of strings than System.Object.ToString provides should implement System.IFormattable, whose System.Object.ToString method uses the culture of the current thread. Example: The following example outputs the textual description of the value of an object of type System.Object to the console. using System;
class MyClass {
static void Main() {
object o = new object();
Console.WriteLine (o.ToString());
}
}
The output is System.Object |