Merge remote branch 'upstream/master'
[mono.git] / mcs / class / IKVM.Reflection / Reader / EventInfoImpl.cs
1 /*
2   Copyright (C) 2009 Jeroen Frijters
3
4   This software is provided 'as-is', without any express or implied
5   warranty.  In no event will the authors be held liable for any damages
6   arising from the use of this software.
7
8   Permission is granted to anyone to use this software for any purpose,
9   including commercial applications, and to alter it and redistribute it
10   freely, subject to the following restrictions:
11
12   1. The origin of this software must not be misrepresented; you must not
13      claim that you wrote the original software. If you use this software
14      in a product, an acknowledgment in the product documentation would be
15      appreciated but is not required.
16   2. Altered source versions must be plainly marked as such, and must not be
17      misrepresented as being the original software.
18   3. This notice may not be removed or altered from any source distribution.
19
20   Jeroen Frijters
21   jeroen@frijters.net
22   
23 */
24 using System;
25 using System.Collections.Generic;
26 using System.Text;
27 using IKVM.Reflection.Metadata;
28
29 namespace IKVM.Reflection.Reader
30 {
31         sealed class EventInfoImpl : EventInfo
32         {
33                 private readonly ModuleReader module;
34                 private readonly Type declaringType;
35                 private readonly int index;
36                 private bool isPublic;
37                 private bool isStatic;
38                 private bool flagsCached;
39
40                 internal EventInfoImpl(ModuleReader module, Type declaringType, int index)
41                 {
42                         this.module = module;
43                         this.declaringType = declaringType;
44                         this.index = index;
45                 }
46
47                 public override bool Equals(object obj)
48                 {
49                         EventInfoImpl other = obj as EventInfoImpl;
50                         return other != null && other.declaringType == declaringType && other.index == index;
51                 }
52
53                 public override int GetHashCode()
54                 {
55                         return declaringType.GetHashCode() * 123 + index;
56                 }
57
58                 public override EventAttributes Attributes
59                 {
60                         get { return (EventAttributes)module.Event.records[index].EventFlags; }
61                 }
62
63                 public override MethodInfo GetAddMethod(bool nonPublic)
64                 {
65                         return module.MethodSemantics.GetMethod(module, this.MetadataToken, nonPublic, MethodSemanticsTable.AddOn);
66                 }
67
68                 public override MethodInfo GetRaiseMethod(bool nonPublic)
69                 {
70                         return module.MethodSemantics.GetMethod(module, this.MetadataToken, nonPublic, MethodSemanticsTable.Fire);
71                 }
72
73                 public override MethodInfo GetRemoveMethod(bool nonPublic)
74                 {
75                         return module.MethodSemantics.GetMethod(module, this.MetadataToken, nonPublic, MethodSemanticsTable.RemoveOn);
76                 }
77
78                 public override MethodInfo[] GetOtherMethods(bool nonPublic)
79                 {
80                         return module.MethodSemantics.GetMethods(module, this.MetadataToken, nonPublic, MethodSemanticsTable.Other);
81                 }
82
83                 public override Type EventHandlerType
84                 {
85                         get { return module.ResolveType(module.Event.records[index].EventType, declaringType); }
86                 }
87
88                 public override string Name
89                 {
90                         get { return module.GetString(module.Event.records[index].Name); }
91                 }
92
93                 public override Type DeclaringType
94                 {
95                         get { return declaringType; }
96                 }
97
98                 public override Module Module
99                 {
100                         get { return module; }
101                 }
102
103                 public override int MetadataToken
104                 {
105                         get { return (EventTable.Index << 24) + index + 1; }
106                 }
107
108                 internal override bool IsPublic
109                 {
110                         get
111                         {
112                                 if (!flagsCached)
113                                 {
114                                         ComputeFlags();
115                                 }
116                                 return isPublic;
117                         }
118                 }
119
120                 internal override bool IsStatic
121                 {
122                         get
123                         {
124                                 if (!flagsCached)
125                                 {
126                                         ComputeFlags();
127                                 }
128                                 return isStatic;
129                         }
130                 }
131
132                 private void ComputeFlags()
133                 {
134                         module.MethodSemantics.ComputeFlags(module, this.MetadataToken, out isPublic, out isStatic);
135                         flagsCached = true;
136                 }
137         }
138 }