2 // decl.cs: Declaration base class for structs, classes, enums and interfaces.
4 // Author: Miguel de Icaza (miguel@gnu.org)
6 // Licensed under the terms of the GNU GPL
8 // (C) 2001 Ximian, Inc (http://www.ximian.com)
10 // TODO: Move the method verification stuff from the class.cs and interface.cs here
14 using System.Collections;
15 using System.Reflection.Emit;
16 using System.Reflection;
18 namespace Mono.CSharp {
21 /// Base representation for members. This is only used to keep track
22 /// of Name, Location and Modifier flags.
24 public abstract class MemberCore {
31 /// Modifier flags that the user specified in the source code
36 /// Location where this declaration happens
38 public readonly Location Location;
40 public MemberCore (string name, Location loc)
46 protected void WarningNotHiding (TypeContainer parent)
50 "The member " + parent.MakeName (Name) + " does not hide an " +
51 "inherited member. The keyword new is not required");
55 void Error_CannotChangeAccessModifiers (TypeContainer parent, MethodInfo parent_method,
59 // FIXME: report the old/new permissions?
62 507, Location, parent.MakeName (Name) +
63 ": can't change the access modifiers when overriding inherited " +
64 "member `" + name + "'");
68 // Performs various checks on the MethodInfo `mb' regarding the modifier flags
69 // that have been defined.
71 // `name' is the user visible name for reporting errors (this is used to
72 // provide the right name regarding method names and properties)
74 protected bool CheckMethodAgainstBase (TypeContainer parent, MethodAttributes my_attrs,
75 MethodInfo mb, string name)
79 if ((ModFlags & Modifiers.OVERRIDE) != 0){
80 if (!(mb.IsAbstract || mb.IsVirtual)){
82 506, Location, parent.MakeName (Name) +
83 ": cannot override inherited member `" +
84 name + "' because it is not " +
85 "virtual, abstract or override");
89 // Now we check that the overriden method is not final
92 Report.Error (239, Location, parent.MakeName (Name) + " : cannot " +
93 "override inherited member `" + name +
94 "' because it is sealed.");
99 // Check that the permissions are not being changed
101 MethodAttributes thisp = my_attrs & MethodAttributes.MemberAccessMask;
102 MethodAttributes parentp = mb.Attributes & MethodAttributes.MemberAccessMask;
104 if (thisp != parentp){
105 Error_CannotChangeAccessModifiers (parent, mb, name);
110 if (mb.IsVirtual || mb.IsAbstract){
111 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0){
112 if (Name != "Finalize"){
114 114, 2, Location, parent.MakeName (Name) +
115 " hides inherited member `" + name +
116 "'. To make the current member override that " +
117 "implementation, add the override keyword, " +
118 "otherwise use the new keyword");
119 ModFlags |= Modifiers.NEW;
123 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0){
124 if (Name != "Finalize"){
126 108, 1, Location, "The keyword new is required on " +
127 parent.MakeName (Name) + " because it hides " +
128 "inherited member `" + name + "'");
129 ModFlags |= Modifiers.NEW;
137 public abstract bool Define (TypeContainer parent);
140 // Whehter is it ok to use an unsafe pointer in this type container
142 public bool UnsafeOK (DeclSpace parent)
145 // First check if this MemberCore modifier flags has unsafe set
147 if ((ModFlags & Modifiers.UNSAFE) != 0)
150 if (parent.UnsafeContext)
153 Expression.UnsafeError (Location);
159 // FIXME: This is temporary outside DeclSpace, because I have to fix a bug
160 // in MCS that makes it fail the lookup for the enum
164 /// The result value from adding an declaration into
165 /// a struct or a class
167 public enum AdditionResult {
169 /// The declaration has been successfully
170 /// added to the declation space.
175 /// The symbol has already been defined.
180 /// Returned if the declation being added to the
181 /// name space clashes with its container name.
183 /// The only exceptions for this are constructors
184 /// and static constructors
189 /// Returned if a constructor was created (because syntactically
190 /// it looked like a constructor) but was not (because the name
191 /// of the method is not the same as the container class
196 /// This is only used by static constructors to emit the
197 /// error 111, but this error for other things really
198 /// happens at another level for other functions.
204 /// Base class for structs, classes, enumerations and interfaces.
207 /// They all create new declaration spaces. This
208 /// provides the common foundation for managing those name
211 public abstract class DeclSpace : MemberCore {
213 /// this points to the actual definition that is being
214 /// created with System.Reflection.Emit
216 public TypeBuilder TypeBuilder;
219 /// This variable tracks whether we have Closed the type
221 public bool Created = false;
224 // This is the namespace in which this typecontainer
225 // was declared. We use this to resolve names.
227 public Namespace Namespace;
229 public Hashtable Cache = new Hashtable ();
231 public string Basename;
234 /// defined_names is used for toplevel objects
236 protected Hashtable defined_names;
238 TypeContainer parent;
240 public DeclSpace (TypeContainer parent, string name, Location l)
243 Basename = name.Substring (1 + name.LastIndexOf ('.'));
244 defined_names = new Hashtable ();
245 this.parent = parent;
249 /// Returns a status code based purely on the name
250 /// of the member being added
252 protected AdditionResult IsValid (string name)
254 if (name == Basename)
255 return AdditionResult.EnclosingClash;
257 if (defined_names.Contains (name))
258 return AdditionResult.NameExists;
260 return AdditionResult.Success;
264 /// Introduce @name into this declaration space and
265 /// associates it with the object @o. Note that for
266 /// methods this will just point to the first method. o
268 protected void DefineName (string name, object o)
270 defined_names.Add (name, o);
274 /// Returns the object associated with a given name in the declaration
275 /// space. This is the inverse operation of `DefineName'
277 public object GetDefinition (string name)
279 return defined_names [name];
282 bool in_transit = false;
285 /// This function is used to catch recursive definitions
288 public bool InTransit {
298 public TypeContainer Parent {
305 /// Looks up the alias for the name
307 public string LookupAlias (string name)
309 if (Namespace != null)
310 return Namespace.LookupAlias (name);
316 // root_types contains all the types. All TopLevel types
317 // hence have a parent that points to `root_types', that is
318 // why there is a non-obvious test down here.
320 public bool IsTopLevel {
323 if (parent.parent == null)
330 public virtual void CloseType ()
334 TypeBuilder.CreateType ();
337 // The try/catch is needed because
338 // nested enumerations fail to load when they
341 // Even if this is the right order (enumerations
342 // declared after types).
344 // Note that this still creates the type and
345 // it is possible to save it
352 /// Should be overriten by the appropriate declaration space
354 public abstract TypeBuilder DefineType ();
357 /// Define all members, but don't apply any attributes or do anything which may
358 /// access not-yet-defined classes. This method also creates the MemberCache.
360 public abstract bool DefineMembers (TypeContainer parent);
363 // Whether this is an `unsafe context'
365 public bool UnsafeContext {
367 if ((ModFlags & Modifiers.UNSAFE) != 0)
370 return parent.UnsafeContext;
375 public static string MakeFQN (string nsn, string name)
377 string prefix = (nsn == "" ? "" : nsn + ".");
379 return prefix + name;
382 EmitContext type_resolve_ec;
383 EmitContext GetTypeResolveEmitContext (TypeContainer parent, Location loc)
385 type_resolve_ec = new EmitContext (parent, this, loc, null, null, ModFlags, false);
386 type_resolve_ec.ResolvingTypeTree = true;
388 return type_resolve_ec;
392 // Looks up the type, as parsed into the expression `e'
394 public Type ResolveType (Expression e, bool silent, Location loc)
396 if (type_resolve_ec == null)
397 type_resolve_ec = GetTypeResolveEmitContext (parent, loc);
398 type_resolve_ec.loc = loc;
400 int errors = Report.Errors;
401 Expression d = e.Resolve (type_resolve_ec, ResolveFlags.Type);
402 if (d == null || d.eclass != ExprClass.Type){
403 if (!silent && errors == Report.Errors){
404 Report.Error (246, loc, "Cannot find type `"+ e.ToString () +"'");
413 // Resolves the expression `e' for a type, and will recursively define
416 public Expression ResolveTypeExpr (Expression e, bool silent, Location loc)
418 if (type_resolve_ec == null)
419 type_resolve_ec = GetTypeResolveEmitContext (parent, loc);
421 Expression d = e.Resolve (type_resolve_ec, ResolveFlags.Type);
422 if (d == null || d.eclass != ExprClass.Type){
424 Report.Error (246, loc, "Cannot find type `"+ e +"'");
432 Type LookupInterfaceOrClass (string ns, string name, out bool error)
438 name = MakeFQN (ns, name);
440 t = TypeManager.LookupType (name);
444 parent = (DeclSpace) RootContext.Tree.Decls [name];
448 t = parent.DefineType ();
450 Report.Error (146, "Class definition is circular: `"+name+"'");
458 /// GetType is used to resolve type names at the DeclSpace level.
459 /// Use this to lookup class/struct bases, interface bases or
460 /// delegate type references
464 /// Contrast this to LookupType which is used inside method bodies to
465 /// lookup types that have already been defined. GetType is used
466 /// during the tree resolution process and potentially define
467 /// recursively the type
469 public Type FindType (string name)
475 // For the case the type we are looking for is nested within this one
476 // or is in any base class
478 DeclSpace containing_ds = this;
480 while (containing_ds != null){
481 Type current_type = containing_ds.TypeBuilder;
483 while (current_type != null) {
484 string pre = current_type.FullName;
486 t = LookupInterfaceOrClass (pre, name, out error);
493 current_type = current_type.BaseType;
495 containing_ds = containing_ds.Parent;
499 // Attempt to lookup the class on our namespace and all it's implicit parents
501 for (string ns = Namespace.Name; ns != null; ns = RootContext.ImplicitParent (ns)) {
503 t = LookupInterfaceOrClass (ns, name, out error);
512 // Attempt to do a direct unqualified lookup
514 t = LookupInterfaceOrClass ("", name, out error);
522 // Attempt to lookup the class on any of the `using'
526 for (Namespace ns = Namespace; ns != null; ns = ns.Parent){
528 t = LookupInterfaceOrClass (ns.Name, name, out error);
536 // Now check the using clause list
538 ArrayList using_list = ns.UsingTable;
540 if (using_list == null)
544 foreach (Namespace.UsingEntry ue in using_list){
545 match = LookupInterfaceOrClass (ue.Name, name, out error);
551 Report.Error (104, "`" + name + "' is an ambiguous reference");
563 //Report.Error (246, Location, "Can not find type `"+name+"'");
568 /// This function is broken and not what you're looking for. It should only
569 /// be used while the type is still being created since it doesn't use the cache
570 /// and relies on the filter doing the member name check.
572 public abstract MemberList FindMembers (MemberTypes mt, BindingFlags bf,
573 MemberFilter filter, object criteria);
576 /// If we have a MemberCache, return it. This property may return null if the
577 /// class doesn't have a member cache or while it's still being created.
579 public abstract MemberCache MemberCache {
585 /// This is a readonly list of MemberInfo's.
587 public class MemberList : IList {
588 public readonly IList List;
592 /// Create a new MemberList from the given IList.
594 public MemberList (IList list)
599 this.List = new ArrayList ();
604 /// Concatenate the ILists `first' and `second' to a new MemberList.
606 public MemberList (IList first, IList second)
608 ArrayList list = new ArrayList ();
609 list.AddRange (first);
610 list.AddRange (second);
615 public static readonly MemberList Empty = new MemberList (new ArrayList ());
618 /// Cast the MemberList into a MemberInfo[] array.
621 /// This is an expensive operation, only use it if it's really necessary.
623 public static explicit operator MemberInfo [] (MemberList list)
625 Timer.StartTimer (TimerType.MiscTimer);
626 MemberInfo [] result = new MemberInfo [list.Count];
627 list.CopyTo (result, 0);
628 Timer.StopTimer (TimerType.MiscTimer);
640 public bool IsSynchronized {
642 return List.IsSynchronized;
646 public object SyncRoot {
648 return List.SyncRoot;
652 public void CopyTo (Array array, int index)
654 List.CopyTo (array, index);
659 public IEnumerator GetEnumerator ()
661 return List.GetEnumerator ();
666 public bool IsFixedSize {
672 public bool IsReadOnly {
678 object IList.this [int index] {
684 throw new NotSupportedException ();
688 // FIXME: try to find out whether we can avoid the cast in this indexer.
689 public MemberInfo this [int index] {
691 return (MemberInfo) List [index];
695 public int Add (object value)
697 throw new NotSupportedException ();
702 throw new NotSupportedException ();
705 public bool Contains (object value)
707 return List.Contains (value);
710 public int IndexOf (object value)
712 return List.IndexOf (value);
715 public void Insert (int index, object value)
717 throw new NotSupportedException ();
720 public void Remove (object value)
722 throw new NotSupportedException ();
725 public void RemoveAt (int index)
727 throw new NotSupportedException ();
732 /// This interface is used to get all members of a class when creating the
733 /// member cache. It must be implemented by all DeclSpace derivatives which
734 /// want to support the member cache and by TypeHandle to get caching of
735 /// non-dynamic types.
737 public interface IMemberContainer {
739 /// The name of the IMemberContainer. This is only used for
740 /// debugging purposes.
747 /// The type of this IMemberContainer.
754 /// Returns the IMemberContainer of the parent class or null if this
755 /// is an interface or TypeManger.object_type.
756 /// This is used when creating the member cache for a class to get all
757 /// members from the parent class.
759 IMemberContainer Parent {
764 /// Whether this is an interface.
771 /// Returns all members of this class with the corresponding MemberTypes
772 /// and BindingFlags.
775 /// When implementing this method, make sure not to return any inherited
776 /// members and check the MemberTypes and BindingFlags properly.
777 /// Unfortunately, System.Reflection is lame and doesn't provide a way to
778 /// get the BindingFlags (static/non-static,public/non-public) in the
779 /// MemberInfo class, but the cache needs this information. That's why
780 /// this method is called multiple times with different BindingFlags.
782 MemberList GetMembers (MemberTypes mt, BindingFlags bf);
785 /// Return the container's member cache.
787 MemberCache MemberCache {
793 /// The MemberCache is used by dynamic and non-dynamic types to speed up
794 /// member lookups. It has a member name based hash table; it maps each member
795 /// name to a list of CacheEntry objects. Each CacheEntry contains a MemberInfo
796 /// and the BindingFlags that were initially used to get it. The cache contains
797 /// all members of the current class and all inherited members. If this cache is
798 /// for an interface types, it also contains all inherited members.
800 /// There are two ways to get a MemberCache:
801 /// * if this is a dynamic type, lookup the corresponding DeclSpace and then
802 /// use the DeclSpace.MemberCache property.
803 /// * if this not a dynamic type, call TypeHandle.GetTypeHandle() to get a
804 /// TypeHandle instance for the type and then use TypeHandle.MemberCache.
806 public class MemberCache {
807 public readonly IMemberContainer Container;
808 protected Hashtable member_hash;
809 protected Hashtable method_hash;
810 protected Hashtable interface_hash;
813 /// Create a new MemberCache for the given IMemberContainer `container'.
815 public MemberCache (IMemberContainer container)
817 this.Container = container;
819 Timer.IncrementCounter (CounterType.MemberCache);
820 Timer.StartTimer (TimerType.CacheInit);
822 interface_hash = new Hashtable ();
824 // If we have a parent class (we have a parent class unless we're
825 // TypeManager.object_type), we deep-copy its MemberCache here.
826 if (Container.Parent != null)
827 member_hash = SetupCache (Container.Parent.MemberCache);
828 else if (Container.IsInterface)
829 member_hash = SetupCacheForInterface ();
831 member_hash = new Hashtable ();
833 // If this is neither a dynamic type nor an interface, create a special
834 // method cache with all declared and inherited methods.
835 Type type = container.Type;
836 if (!(type is TypeBuilder) && !type.IsInterface) {
837 method_hash = new Hashtable ();
841 // Add all members from the current class.
842 AddMembers (Container);
844 Timer.StopTimer (TimerType.CacheInit);
848 /// Bootstrap this member cache by doing a deep-copy of our parent.
850 Hashtable SetupCache (MemberCache parent)
852 Hashtable hash = new Hashtable ();
854 IDictionaryEnumerator it = parent.member_hash.GetEnumerator ();
855 while (it.MoveNext ()) {
856 hash [it.Key] = ((ArrayList) it.Value).Clone ();
862 void AddInterfaces (MemberCache parent)
864 foreach (Type iface in parent.interface_hash.Keys) {
865 if (!interface_hash.Contains (iface))
866 interface_hash.Add (iface, true);
871 /// Add the contents of `new_hash' to `hash'.
873 void AddHashtable (Hashtable hash, Hashtable new_hash)
875 IDictionaryEnumerator it = new_hash.GetEnumerator ();
876 while (it.MoveNext ()) {
877 ArrayList list = (ArrayList) hash [it.Key];
879 list.AddRange ((ArrayList) it.Value);
881 hash [it.Key] = ((ArrayList) it.Value).Clone ();
886 /// Bootstrap the member cache for an interface type.
887 /// Type.GetMembers() won't return any inherited members for interface types,
888 /// so we need to do this manually. Interfaces also inherit from System.Object.
890 Hashtable SetupCacheForInterface ()
892 Hashtable hash = SetupCache (TypeHandle.ObjectType.MemberCache);
893 Type [] ifaces = TypeManager.GetInterfaces (Container.Type);
895 foreach (Type iface in ifaces) {
896 if (interface_hash.Contains (iface))
898 interface_hash.Add (iface, true);
900 IMemberContainer iface_container =
901 TypeManager.LookupMemberContainer (iface);
903 MemberCache iface_cache = iface_container.MemberCache;
904 AddHashtable (hash, iface_cache.member_hash);
905 AddInterfaces (iface_cache);
912 /// Add all members from class `container' to the cache.
914 void AddMembers (IMemberContainer container)
916 // We need to call AddMembers() with a single member type at a time
917 // to get the member type part of CacheEntry.EntryType right.
918 AddMembers (MemberTypes.Constructor, container);
919 AddMembers (MemberTypes.Field, container);
920 AddMembers (MemberTypes.Method, container);
921 AddMembers (MemberTypes.Property, container);
922 AddMembers (MemberTypes.Event, container);
923 // Nested types are returned by both Static and Instance searches.
924 AddMembers (MemberTypes.NestedType,
925 BindingFlags.Static | BindingFlags.Public, container);
926 AddMembers (MemberTypes.NestedType,
927 BindingFlags.Static | BindingFlags.NonPublic, container);
930 void AddMembers (MemberTypes mt, IMemberContainer container)
932 AddMembers (mt, BindingFlags.Static | BindingFlags.Public, container);
933 AddMembers (mt, BindingFlags.Static | BindingFlags.NonPublic, container);
934 AddMembers (mt, BindingFlags.Instance | BindingFlags.Public, container);
935 AddMembers (mt, BindingFlags.Instance | BindingFlags.NonPublic, container);
939 /// Add all members from class `container' with the requested MemberTypes and
940 /// BindingFlags to the cache. This method is called multiple times with different
941 /// MemberTypes and BindingFlags.
943 void AddMembers (MemberTypes mt, BindingFlags bf, IMemberContainer container)
945 MemberList members = container.GetMembers (mt, bf);
946 BindingFlags new_bf = (container == Container) ?
947 bf | BindingFlags.DeclaredOnly : bf;
949 foreach (MemberInfo member in members) {
950 string name = member.Name;
952 // We use a name-based hash table of ArrayList's.
953 ArrayList list = (ArrayList) member_hash [name];
955 list = new ArrayList ();
956 member_hash.Add (name, list);
959 // When this method is called for the current class, the list will
960 // already contain all inherited members from our parent classes.
961 // We cannot add new members in front of the list since this'd be an
962 // expensive operation, that's why the list is sorted in reverse order
963 // (ie. members from the current class are coming last).
964 list.Add (new CacheEntry (container, member, mt, bf));
969 /// Add all declared and inherited methods from class `type' to the method cache.
971 void AddMethods (Type type)
973 AddMethods (BindingFlags.Static | BindingFlags.Public, type);
974 AddMethods (BindingFlags.Static | BindingFlags.NonPublic, type);
975 AddMethods (BindingFlags.Instance | BindingFlags.Public, type);
976 AddMethods (BindingFlags.Instance | BindingFlags.NonPublic, type);
979 void AddMethods (BindingFlags bf, Type type)
981 MemberInfo [] members = type.GetMethods (bf);
983 foreach (MethodBase member in members) {
984 string name = member.Name;
986 // Varargs methods aren't allowed in C# code.
987 if ((member.CallingConvention & CallingConventions.VarArgs) != 0)
990 // We use a name-based hash table of ArrayList's.
991 ArrayList list = (ArrayList) method_hash [name];
993 list = new ArrayList ();
994 method_hash.Add (name, list);
997 // Unfortunately, the elements returned by Type.GetMethods() aren't
998 // sorted so we need to do this check for every member.
999 BindingFlags new_bf = bf;
1000 if (member.DeclaringType == type)
1001 new_bf |= BindingFlags.DeclaredOnly;
1003 list.Add (new CacheEntry (Container, member, MemberTypes.Method, new_bf));
1008 /// Compute and return a appropriate `EntryType' magic number for the given
1009 /// MemberTypes and BindingFlags.
1011 protected static EntryType GetEntryType (MemberTypes mt, BindingFlags bf)
1013 EntryType type = EntryType.None;
1015 if ((mt & MemberTypes.Constructor) != 0)
1016 type |= EntryType.Constructor;
1017 if ((mt & MemberTypes.Event) != 0)
1018 type |= EntryType.Event;
1019 if ((mt & MemberTypes.Field) != 0)
1020 type |= EntryType.Field;
1021 if ((mt & MemberTypes.Method) != 0)
1022 type |= EntryType.Method;
1023 if ((mt & MemberTypes.Property) != 0)
1024 type |= EntryType.Property;
1025 // Nested types are returned by static and instance searches.
1026 if ((mt & MemberTypes.NestedType) != 0)
1027 type |= EntryType.NestedType | EntryType.Static | EntryType.Instance;
1029 if ((bf & BindingFlags.Instance) != 0)
1030 type |= EntryType.Instance;
1031 if ((bf & BindingFlags.Static) != 0)
1032 type |= EntryType.Static;
1033 if ((bf & BindingFlags.Public) != 0)
1034 type |= EntryType.Public;
1035 if ((bf & BindingFlags.NonPublic) != 0)
1036 type |= EntryType.NonPublic;
1037 if ((bf & BindingFlags.DeclaredOnly) != 0)
1038 type |= EntryType.Declared;
1044 /// The `MemberTypes' enumeration type is a [Flags] type which means that it may
1045 /// denote multiple member types. Returns true if the given flags value denotes a
1046 /// single member types.
1048 public static bool IsSingleMemberType (MemberTypes mt)
1051 case MemberTypes.Constructor:
1052 case MemberTypes.Event:
1053 case MemberTypes.Field:
1054 case MemberTypes.Method:
1055 case MemberTypes.Property:
1056 case MemberTypes.NestedType:
1065 /// We encode the MemberTypes and BindingFlags of each members in a "magic"
1066 /// number to speed up the searching process.
1069 protected enum EntryType {
1074 MaskStatic = Instance|Static,
1078 MaskProtection = Public|NonPublic,
1082 Constructor = 0x020,
1089 MaskType = Constructor|Event|Field|Method|Property|NestedType
1092 protected struct CacheEntry {
1093 public readonly IMemberContainer Container;
1094 public readonly EntryType EntryType;
1095 public readonly MemberInfo Member;
1097 public CacheEntry (IMemberContainer container, MemberInfo member,
1098 MemberTypes mt, BindingFlags bf)
1100 this.Container = container;
1101 this.Member = member;
1102 this.EntryType = GetEntryType (mt, bf);
1107 /// This is called each time we're walking up one level in the class hierarchy
1108 /// and checks whether we can abort the search since we've already found what
1109 /// we were looking for.
1111 protected bool DoneSearching (ArrayList list)
1114 // We've found exactly one member in the current class and it's not
1115 // a method or constructor.
1117 if (list.Count == 1 && !(list [0] is MethodBase))
1121 // Multiple properties: we query those just to find out the indexer
1124 if ((list.Count > 0) && (list [0] is PropertyInfo))
1131 /// Looks up members with name `name'. If you provide an optional
1132 /// filter function, it'll only be called with members matching the
1133 /// requested member name.
1135 /// This method will try to use the cache to do the lookup if possible.
1137 /// Unlike other FindMembers implementations, this method will always
1138 /// check all inherited members - even when called on an interface type.
1140 /// If you know that you're only looking for methods, you should use
1141 /// MemberTypes.Method alone since this speeds up the lookup a bit.
1142 /// When doing a method-only search, it'll try to use a special method
1143 /// cache (unless it's a dynamic type or an interface) and the returned
1144 /// MemberInfo's will have the correct ReflectedType for inherited methods.
1145 /// The lookup process will automatically restart itself in method-only
1146 /// search mode if it discovers that it's about to return methods.
1148 public MemberList FindMembers (MemberTypes mt, BindingFlags bf, string name,
1149 MemberFilter filter, object criteria)
1151 bool declared_only = (bf & BindingFlags.DeclaredOnly) != 0;
1152 bool method_search = mt == MemberTypes.Method;
1153 // If we have a method cache and we aren't already doing a method-only search,
1154 // then we restart a method search if the first match is a method.
1155 bool do_method_search = !method_search && (method_hash != null);
1157 ArrayList applicable;
1159 // If this is a method-only search, we try to use the method cache if
1160 // possible; a lookup in the method cache will return a MemberInfo with
1161 // the correct ReflectedType for inherited methods.
1162 if (method_search && (method_hash != null))
1163 applicable = (ArrayList) method_hash [name];
1165 applicable = (ArrayList) member_hash [name];
1167 if (applicable == null)
1168 return MemberList.Empty;
1170 ArrayList list = new ArrayList ();
1172 Timer.StartTimer (TimerType.CachedLookup);
1174 EntryType type = GetEntryType (mt, bf);
1176 IMemberContainer current = Container;
1178 // `applicable' is a list of all members with the given member name `name'
1179 // in the current class and all its parent classes. The list is sorted in
1180 // reverse order due to the way how the cache is initialy created (to speed
1181 // things up, we're doing a deep-copy of our parent).
1183 for (int i = applicable.Count-1; i >= 0; i--) {
1184 CacheEntry entry = (CacheEntry) applicable [i];
1186 // This happens each time we're walking one level up in the class
1187 // hierarchy. If we're doing a DeclaredOnly search, we must abort
1188 // the first time this happens (this may already happen in the first
1189 // iteration of this loop if there are no members with the name we're
1190 // looking for in the current class).
1191 if (entry.Container != current) {
1192 if (declared_only || DoneSearching (list))
1195 current = entry.Container;
1198 // Is the member of the correct type ?
1199 if ((entry.EntryType & type & EntryType.MaskType) == 0)
1202 // Is the member static/non-static ?
1203 if ((entry.EntryType & type & EntryType.MaskStatic) == 0)
1206 // Apply the filter to it.
1207 if (filter (entry.Member, criteria)) {
1208 if ((entry.EntryType & EntryType.MaskType) != EntryType.Method)
1209 do_method_search = false;
1210 list.Add (entry.Member);
1214 Timer.StopTimer (TimerType.CachedLookup);
1216 // If we have a method cache and we aren't already doing a method-only
1217 // search, we restart in method-only search mode if the first match is
1218 // a method. This ensures that we return a MemberInfo with the correct
1219 // ReflectedType for inherited methods.
1220 if (do_method_search && (list.Count > 0))
1221 return FindMembers (MemberTypes.Method, bf, name, filter, criteria);
1223 return new MemberList (list);