Merged pull request #58 from XTZGZoReX/master.
[mono.git] / mcs / class / IKVM.Reflection / Emit / EventBuilder.cs
index be29644456e44efbcbe5651c58510b0e290ca974..c0e28424fe4b402e21d87de059b00254a2e9d610 100644 (file)
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2009 Jeroen Frijters
+  Copyright (C) 2009-2011 Jeroen Frijters
 
   This software is provided 'as-is', without any express or implied
   warranty.  In no event will the authors be held liable for any damages
@@ -38,9 +38,15 @@ namespace IKVM.Reflection.Emit
                private MethodBuilder addOnMethod;
                private MethodBuilder removeOnMethod;
                private MethodBuilder fireMethod;
-               private List<MethodBuilder> otherMethods;
+               private readonly List<Accessor> accessors = new List<Accessor>();
                private int lazyPseudoToken;
 
+               private struct Accessor
+               {
+                       internal short Semantics;
+                       internal MethodBuilder Method;
+               }
+
                internal EventBuilder(TypeBuilder typeBuilder, string name, EventAttributes attributes, Type eventtype)
                {
                        this.typeBuilder = typeBuilder;
@@ -52,25 +58,36 @@ namespace IKVM.Reflection.Emit
                public void SetAddOnMethod(MethodBuilder mdBuilder)
                {
                        addOnMethod = mdBuilder;
+                       Accessor acc;
+                       acc.Semantics = MethodSemanticsTable.AddOn;
+                       acc.Method = mdBuilder;
+                       accessors.Add(acc);
                }
 
                public void SetRemoveOnMethod(MethodBuilder mdBuilder)
                {
                        removeOnMethod = mdBuilder;
+                       Accessor acc;
+                       acc.Semantics = MethodSemanticsTable.RemoveOn;
+                       acc.Method = mdBuilder;
+                       accessors.Add(acc);
                }
 
                public void SetRaiseMethod(MethodBuilder mdBuilder)
                {
                        fireMethod = mdBuilder;
+                       Accessor acc;
+                       acc.Semantics = MethodSemanticsTable.Fire;
+                       acc.Method = mdBuilder;
+                       accessors.Add(acc);
                }
 
                public void AddOtherMethod(MethodBuilder mdBuilder)
                {
-                       if (otherMethods == null)
-                       {
-                               otherMethods = new List<MethodBuilder>();
-                       }
-                       otherMethods.Add(mdBuilder);
+                       Accessor acc;
+                       acc.Semantics = MethodSemanticsTable.Other;
+                       acc.Method = mdBuilder;
+                       accessors.Add(acc);
                }
 
                public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
@@ -118,19 +135,26 @@ namespace IKVM.Reflection.Emit
                public override MethodInfo[] GetOtherMethods(bool nonPublic)
                {
                        List<MethodInfo> list = new List<MethodInfo>();
-                       if (otherMethods != null)
+                       foreach (Accessor acc in accessors)
                        {
-                               foreach (MethodInfo method in otherMethods)
+                               if (acc.Semantics == MethodSemanticsTable.Other && (nonPublic || acc.Method.IsPublic))
                                {
-                                       if (nonPublic || method.IsPublic)
-                                       {
-                                               list.Add(method);
-                                       }
+                                       list.Add(acc.Method);
                                }
                        }
                        return list.ToArray();
                }
 
+               public override MethodInfo[] __GetMethods()
+               {
+                       List<MethodInfo> list = new List<MethodInfo>();
+                       foreach (Accessor acc in accessors)
+                       {
+                               list.Add(acc.Method);
+                       }
+                       return list.ToArray();
+               }
+
                public override Type DeclaringType
                {
                        get { return typeBuilder; }
@@ -173,24 +197,9 @@ namespace IKVM.Reflection.Emit
                                typeBuilder.ModuleBuilder.RegisterTokenFixup(lazyPseudoToken, token);
                        }
 
-                       if (addOnMethod != null)
-                       {
-                               AddMethodSemantics(MethodSemanticsTable.AddOn, addOnMethod.MetadataToken, token);
-                       }
-                       if (removeOnMethod != null)
+                       foreach (Accessor acc in accessors)
                        {
-                               AddMethodSemantics(MethodSemanticsTable.RemoveOn, removeOnMethod.MetadataToken, token);
-                       }
-                       if (fireMethod != null)
-                       {
-                               AddMethodSemantics(MethodSemanticsTable.Fire, fireMethod.MetadataToken, token);
-                       }
-                       if (otherMethods != null)
-                       {
-                               foreach (MethodBuilder method in otherMethods)
-                               {
-                                       AddMethodSemantics(MethodSemanticsTable.Other, method.MetadataToken, token);
-                               }
+                               AddMethodSemantics(acc.Semantics, acc.Method.MetadataToken, token);
                        }
                }
 
@@ -207,18 +216,11 @@ namespace IKVM.Reflection.Emit
                {
                        get
                        {
-                               if ((addOnMethod != null && addOnMethod.IsPublic) || (removeOnMethod != null && removeOnMethod.IsPublic) || (fireMethod != null && fireMethod.IsPublic))
-                               {
-                                       return true;
-                               }
-                               if (otherMethods != null)
+                               foreach (Accessor acc in accessors)
                                {
-                                       foreach (MethodBuilder method in otherMethods)
+                                       if (acc.Method.IsPublic)
                                        {
-                                               if (method.IsPublic)
-                                               {
-                                                       return true;
-                                               }
+                                               return true;
                                        }
                                }
                                return false;
@@ -229,18 +231,11 @@ namespace IKVM.Reflection.Emit
                {
                        get
                        {
-                               if ((addOnMethod != null && addOnMethod.IsStatic) || (removeOnMethod != null && removeOnMethod.IsStatic) || (fireMethod != null && fireMethod.IsStatic))
-                               {
-                                       return true;
-                               }
-                               if (otherMethods != null)
+                               foreach (Accessor acc in accessors)
                                {
-                                       foreach (MethodBuilder method in otherMethods)
+                                       if (acc.Method.IsStatic)
                                        {
-                                               if (method.IsStatic)
-                                               {
-                                                       return true;
-                                               }
+                                               return true;
                                        }
                                }
                                return false;