public abstract class EventInfo : MemberInfo
|
Delegates are object-oriented function pointers. In C or C++, a function pointer is a reference to a method. In contrast to the C or C++ function pointer, a delegate contains two references: a reference to a method and a reference to an object that supports the method. Delegates can invoke a method without knowing the class type that declares or inherits the method. Delegates need only know the return type and parameter list of the method.
The event model works equally well for single-cast and multicast delegates. When the delegate's invoke method is called, only a single object will have a method called on it. A multicast modifier can be applied to a delegate declaration, which allows multiple methods to be called when the invoke method of the delegate is called.
Calling ICustomAttributeProvider.GetCustomAttributes on EventInfo when the inherit parameter of GetCustomAttributes is true does not walk the type hierarchy. Use Attribute to inherit custom attributes.
Attributes | Read-only Gets the attributes for this event. |
DeclaringType (inherited from System.Reflection.MemberInfo) |
Read-only See base class member description: System.Reflection.MemberInfo.DeclaringType Gets the class that declares this member. |
EventHandlerType | Read-only Gets the Type object of the underlying event-handler delegate associated with this event. |
IsMulticast | Read-only Gets a value indicating whether the event is multicast. |
IsSpecialName | Read-only Gets a value indicating whether the EventInfo has a name with a special meaning. |
MemberType | Read-only Overridden: Gets the member type of this event. |
Name (inherited from System.Reflection.MemberInfo) |
Read-only See base class member description: System.Reflection.MemberInfo.Name Gets the name of this member. |
ReflectedType (inherited from System.Reflection.MemberInfo) |
Read-only See base class member description: System.Reflection.MemberInfo.ReflectedType Gets the class object that was used to obtain this instance of MemberInfo. |
AddEventHandler | Adds an event handler to an event source. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetAddMethod | Overloaded:GetAddMethod() Returns the method used to add an event handler delegate to the event source. |
GetAddMethod | Overloaded:GetAddMethod(bool nonPublic) When overridden in a derived class, retrieves the MethodInfo object for the Add method of the event, specifying whether the method is public or nonpublic. |
GetCustomAttributes (inherited from System.Reflection.MemberInfo) |
Overloaded:GetCustomAttributes(bool inherit) See base class member description: System.Reflection.MemberInfo.GetCustomAttributesWhen overridden in a derived class, returns an array of all of the custom attributes. |
GetCustomAttributes (inherited from System.Reflection.MemberInfo) |
Overloaded:GetCustomAttributes(Type attributeType, bool inherit) See base class member description: System.Reflection.MemberInfo.GetCustomAttributesWhen overridden in a derived class, returns an array of custom attributes identified by Type. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetRaiseMethod | Overloaded:GetRaiseMethod() Returns the method that is called when the event is raised. |
GetRaiseMethod | Overloaded:GetRaiseMethod(bool nonPublic) When overridden in a derived class, returns the method that is called when the event is raised, specifying whether the method is public or nonpublic. |
GetRemoveMethod | Overloaded:GetRemoveMethod() Returns the method used to remove an event handler delegate from the event source. |
GetRemoveMethod | Overloaded:GetRemoveMethod(bool nonPublic) When overridden in a derived class, retrieves the MethodInfo object for removing a method of the event, specifying whether the method is public or nonpublic. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
IsDefined (inherited from System.Reflection.MemberInfo) |
See base class member description: System.Reflection.MemberInfo.IsDefined When overridden in a derived class, indicates whether one or more instance of attributeType is defined on this member. |
RemoveEventHandler | Removes an event handler from an event source. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
ctor #1 | Default constructor. This constructor is called by derived class constructors to initialize state in this type. Initializes a new instance of the EventInfo class. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
protected EventInfo(); |
public abstract EventAttributes Attributes {get;}
|
public abstract Type DeclaringType {get;}
|
interface i { int MyVar() ; }; // DeclaringType for MyVar is i. class A : i { public int MyVar() { return 0; } }; // DeclaringType for MyVar is A. class B : A { new int MyVar() { return 0; } }; // DeclaringType for MyVar is B. class C : A { }; // DeclaringType for MyVar is A.
The following example uses DeclaringType to retrieve the member names of the System.IO.BufferedStream class, along with the class in which those members are declared.
using System; using System.IO; using System.Reflection; class Mymemberinfo { public static void Main(string[] args) { Console.WriteLine ("\nReflection.MemberInfo"); //Get the Type and MemberInfo. Type MyType =Type.GetType("System.IO.BufferedStream"); MemberInfo[] Mymemberinfoarray = MyType.GetMembers(); //Get and display the DeclaringType method. Console.Write("\nThere are {0} members in ", Mymemberinfoarray.Length); Console.Write("{0}.", MyType.FullName); foreach (MemberInfo Mymemberinfo in Mymemberinfoarray) { Console.Write("\n" + Mymemberinfo.Name + " declaring type - " + Mymemberinfo.DeclaringType); } } }
This code produces the following output:
Reflection.MemberInfo
There are 31 members in System.IO.BufferedStream.
WriteByte declaring type - System.IO.BufferedStream
Write declaring type - System.IO.BufferedStream
ReadByte declaring type - System.IO.BufferedStream
Read declaring type - System.IO.BufferedStream
SetLength declaring type - System.IO.BufferedStream
Seek declaring type - System.IO.BufferedStream
EndWrite declaring type - System.IO.Stream
BeginWrite declaring type - System.IO.Stream
EndRead declaring type - System.IO.Stream
BeginRead declaring type - System.IO.Stream
Flush declaring type - System.IO.BufferedStream
Close declaring type - System.IO.BufferedStream
set_Position declaring type - System.IO.BufferedStream
get_Position declaring type - System.IO.BufferedStream
get_Length declaring type - System.IO.BufferedStream
get_CanWrite declaring type - System.IO.BufferedStream
get_CanSeek declaring type - System.IO.BufferedStream
get_CanRead declaring type - System.IO.BufferedStream
InitializeLifetimeService declaring type - System.MarshalByRefObject
GetHashCode declaring type - System.Object
Equals declaring type - System.Object
ToString declaring type - System.Object
GetLifetimeService declaring type - System.MarshalByRefObject
GetType declaring type - System.Object
.ctor declaring type - System.IO.BufferedStream
.ctor declaring type - System.IO.BufferedStream
CanRead declaring type - System.IO.BufferedStream
CanWrite declaring type - System.IO.BufferedStream
CanSeek declaring type - System.IO.BufferedStream
Length declaring type - System.IO.BufferedStream
Position declaring type - System.IO.BufferedStream
In the following code example, when B overrides virtual method M from A, it essentially redefines (or redeclares) this method. Therefore, B.M's MethodInfo reports the declaring type as B rather than A, even though A is where this method was originally declared.
class A { virtual public void M () {} } class B: A { override public void M () {} }
public Type EventHandlerType {get;}
|
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
public bool IsMulticast {get;}
|
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
public bool IsSpecialName {get;}
|
public override MemberTypes MemberType {get;}
|
public abstract string Name {get;}
|
To get the Name property, get the class Type. From the Type, get the MemberInfo array. From a MemberInfo element of the array, obtain the Name property.
using System; using System.Reflection; class Mymemberinfo { public static int Main() { Console.WriteLine ("\nReflection.MemberInfo"); //Get the Type and MemberInfo. Type MyType = Type.GetType("System.Empty"); MemberInfo[] Mymemberinfoarray = MyType.GetMembers(); //Get and display the DeclaringType method. Console.Write("\nThere are {0} members in ", Mymemberinfoarray.GetLength(0)); Console.Write("{0}.", MyType.FullName); foreach (MemberInfo Mymemberinfo in Mymemberinfoarray) { Console.Write("\n" + Mymemberinfo.Name + " declaring type - " + Mymemberinfo.DeclaringType); } return 0; } } /* This code produces the following output: Reflection.MemberInfo There are 6 members in System.Empty. Value declaring type - System.Empty GetObjectData declaring type - System.Empty GetHashCode declaring type - System.Object Equals declaring type - System.Object ToString declaring type - System.Empty GetType declaring type - System.Object */
public abstract Type ReflectedType {get;}
|
In order to obtain a MethodInfo object:
using System; using System.IO; using System.Reflection; class Mymemberinfo { public static void Main(string[] args) { Console.WriteLine ("\nReflection.MemberInfo"); //Get the Type and MemberInfo Type MyType =Type.GetType("System.IO.BufferedStream"); MemberInfo[] Mymemberinfoarray = MyType.GetMembers(); //Get and display the DeclaringType method Console.Write("\nThere are {0} members in ", Mymemberinfoarray.Length); Console.Write("{0}.", MyType.FullName); foreach (MemberInfo Mymemberinfo in Mymemberinfoarray) { Console.Write("\n" + Mymemberinfo.Name + " reflected type - " + Mymemberinfo.ReflectedType); } } }This code produces the following output:
Reflection.MemberInfo
There are 31 members in System.IO.BufferedStream.
WriteByte reflected type - System.IO.BufferedStream
target
handler
Exception Type | Condition |
---|---|
TargetInvocationException | The Add method throws an exception. |
ArgumentException | The handler that was passed in cannot be used. |
TargetException | The target parameter is null and the event is not static. -or- The EventInfo is not declared on the target. |
~EventInfo(); |
public MethodInfo GetAddMethod(); |
add_<EventName>(<EventHandlerType> handler)
public abstract MethodInfo GetAddMethod( |
nonPublic
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
add_<EventName>(<EventHandlerType> handler)
inherit
using System; using System.Reflection; // Define a custom attribute with one named parameter. [AttributeUsage(AttributeTargets.All)] public class MyAttribute : Attribute { private string myName; public MyAttribute(string name) { myName = name; } public string Name { get { return myName; } } } // Define a class which has the custom attribute associated with one of its members. public class MyClass1 { [MyAttribute("This is an example attribute")] public void MyMethod(int i) { return; } } public class MemberInfo_GetCustomAttributes { public static void Main() { try { // Get the type of the class 'MyClass1'. Type myType = typeof(MyClass1); // Get the members associated with the class 'MyClass1'. MemberInfo[] myMembers = myType.GetMembers(); // Display the attributes for each of the members of the class 'MyClass1'. for(int i = 0; i < myMembers.Length; i++) { Object[] myAttributes = myMembers[i].GetCustomAttributes(false); if(myAttributes.Length > 0) { Console.WriteLine("\nThe attributes for the member {0} are : \n", myMembers[i]); for(int j = 0; j < myAttributes.Length; j++) Console.WriteLine("The type of the attribute is : {0}", myAttributes[j]); } } } catch(Exception e) { Console.WriteLine("Exception Caught! "+e.Message); } } }
attributeType
inherit
Exception Type | Condition |
---|---|
TypeLoadException | If the custom attribute type can not be loaded. |
public virtual int GetHashCode(); |
public MethodInfo GetRaiseMethod(); |
public abstract MethodInfo GetRaiseMethod( |
nonPublic
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
public MethodInfo GetRemoveMethod(); |
remove_<EventName>(<EventHandlerType> handler)
public abstract MethodInfo GetRemoveMethod( |
nonPublic
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
remove_<EventName>(<EventHandlerType> handler)
public Type GetType(); |
attributeType
inherit
using System; using System.Reflection; // Define a custom attribute with one named parameter. [AttributeUsage(AttributeTargets.All)] public class MyAttribute : Attribute { private string myName; public MyAttribute(string name) { myName = name; } public string Name { get { return myName; } } } // Define a class which has the custom attribute associated with one of its members. public class MyClass1 { [MyAttribute("This is an example attribute")] public void MyMethod(int i) { return; } } public class MemberInfo_GetCustomAttributes_IsDefined { public static void Main() { try { // Get the type of the class 'MyClass1'. Type myType = typeof(MyClass1); // Get the members associated with the class 'MyClass1'. MemberInfo[] myMembers = myType.GetMembers(); // Display the attributes for each of the members of the class 'MyClass1'. for(int i = 0; i < myMembers.Length; i++) { // Display the attribute if it is of type 'MyAttribute'. if(myMembers[i].IsDefined(typeof(MyAttribute), false)) { Object[] myAttributes = myMembers[i].GetCustomAttributes(typeof(MyAttribute), false); Console.WriteLine("\nThe attributes of type 'MyAttribute' for the member {0} are : \n", myMembers[i]); for(int j = 0; j < myAttributes.Length; j++) // Display the value associated with the attribute. Console.WriteLine("The value of the attribute is : \"{0}\"", ((MyAttribute)myAttributes[j]).Name); } } } catch(Exception e) { Console.WriteLine("Exception Caught! "+e.Message); } } }
protected object MemberwiseClone(); |
target
handler
Exception Type | Condition |
---|---|
TargetInvocationException | The Remove method throws an exception. |
ArgumentException | The handler that was passed in cannot be used. |
TargetException | The target parameter is null and the event is not static. -or- The EventInfo is not declared on the target. |
When an event is raised by target, the method or methods encapsulated by handler will no longer be invoked.
public virtual string ToString(); |