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
.MonoBASIC
{
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 // Now we check that the overriden method is not final
83 Report
.Error (30267, Location
, parent
.MakeName (Name
) + " : cannot " +
84 "override inherited member `" + name
+
85 "' because it is NotOverridable.");
88 else if (!(mb
.IsAbstract
|| mb
.IsVirtual
)){
90 31086, Location
, parent
.MakeName (Name
) +
91 ": Cannot override inherited member `" +
92 name
+ "' because it is not " +
93 "declared as Overridable");
98 // Check that the permissions are not being changed
100 MethodAttributes thisp
= my_attrs
& MethodAttributes
.MemberAccessMask
;
101 MethodAttributes parentp
= mb
.Attributes
& MethodAttributes
.MemberAccessMask
;
103 if (thisp
!= parentp
){
104 Error_CannotChangeAccessModifiers (parent
, mb
, name
);
109 if ((ModFlags
& ( Modifiers
.NEW
| Modifiers
.SHADOWS
| Modifiers
.OVERRIDE
)) == 0) {
110 if ((ModFlags
& Modifiers
.NONVIRTUAL
) != 0) {
111 Report
.Error (31088, Location
,
112 parent
.MakeName (Name
) + " cannot " +
113 "be declared NotOverridable since this method is " +
114 "not marked as Overrides");
119 if ((ModFlags
& (Modifiers
.OVERRIDE
)) == 0) {
120 if (Name
!= "Finalize") {
123 name
+ " cannot Shadows the method " +
124 parent
.MakeName (Name
) + " since it is declared " +
125 "'MustOverride' in base class");
129 else if (mb
.IsVirtual
){
130 if ((ModFlags
& (Modifiers
.NEW
| Modifiers
.OVERRIDE
| Modifiers
.SHADOWS
)) == 0){
131 if (Name
!= "Finalize"){
133 40005, 2, Location
, parent
.MakeName (Name
) +
134 " shadows overridable member `" + name
+
135 "'. To make the current member override that " +
136 "implementation, add the overrides keyword." );
137 ModFlags
|= Modifiers
.SHADOWS
;
142 if ((ModFlags
& (Modifiers
.NEW
| Modifiers
.OVERRIDE
|
143 Modifiers
.SHADOWS
)) == 0){
144 if (Name
!= "Finalize"){
146 40004, 1, Location
, "The keyword Shadows is required on " +
147 parent
.MakeName (Name
) + " because it hides " +
148 "inherited member `" + name
+ "'");
149 ModFlags
|= Modifiers
.SHADOWS
;
157 public abstract bool Define (TypeContainer parent
);
160 // Whehter is it ok to use an unsafe pointer in this type container
162 public bool UnsafeOK (DeclSpace parent
)
165 // First check if this MemberCore modifier flags has unsafe set
167 if ((ModFlags
& Modifiers
.UNSAFE
) != 0)
170 if (parent
.UnsafeContext
)
173 Expression
.UnsafeError (Location
);
179 // FIXME: This is temporary outside DeclSpace, because I have to fix a bug
180 // in MCS that makes it fail the lookup for the enum
184 /// The result value from adding an declaration into
185 /// a struct or a class
187 public enum AdditionResult
{
189 /// The declaration has been successfully
190 /// added to the declation space.
195 /// The symbol has already been defined.
200 /// Returned if the declation being added to the
201 /// name space clashes with its container name.
203 /// The only exceptions for this are constructors
204 /// and static constructors
209 /// Returned if a constructor was created (because syntactically
210 /// it looked like a constructor) but was not (because the name
211 /// of the method is not the same as the container class
216 /// This is only used by static constructors to emit the
217 /// error 111, but this error for other things really
218 /// happens at another level for other functions.
224 /// Base class for structs, classes, enumerations and interfaces.
227 /// They all create new declaration spaces. This
228 /// provides the common foundation for managing those name
231 public abstract class DeclSpace
: MemberCore
{
233 /// this points to the actual definition that is being
234 /// created with System.Reflection.Emit
236 public TypeBuilder TypeBuilder
;
239 /// This variable tracks whether we have Closed the type
241 public bool Created
= false;
244 // This is the namespace in which this typecontainer
245 // was declared. We use this to resolve names.
247 public Namespace Namespace
;
249 public Hashtable Cache
= new Hashtable ();
251 public string Basename
;
254 /// defined_names is used for toplevel objects
256 protected Hashtable defined_names
;
258 TypeContainer parent
;
260 public DeclSpace (TypeContainer parent
, string name
, Location l
)
263 Basename
= name
.Substring (1 + name
.LastIndexOf ('.'));
264 defined_names
= new Hashtable ();
265 this.parent
= parent
;
269 /// Returns a status code based purely on the name
270 /// of the member being added
272 protected AdditionResult
IsValid (string name
)
274 if (name
== Basename
)
275 return AdditionResult
.EnclosingClash
;
277 if (defined_names
.Contains (name
))
278 return AdditionResult
.NameExists
;
280 return AdditionResult
.Success
;
284 /// Introduce @name into this declaration space and
285 /// associates it with the object @o. Note that for
286 /// methods this will just point to the first method. o
288 protected void DefineName (string name
, object o
)
291 defined_names
.Add (name
, o
);
293 catch (ArgumentException exp
) {
294 Report
.Error (30260, /*Location,*/
295 "More than one defination with same name '" + name
+
296 "' is found in the container '" + Name
+ "'");
302 /// Returns the object associated with a given name in the declaration
303 /// space. This is the inverse operation of `DefineName'
305 public object GetDefinition (string name
)
307 return defined_names
[name
];
310 bool in_transit
= false;
313 /// This function is used to catch recursive definitions
316 public bool InTransit
{
326 public TypeContainer Parent
{
333 /// Looks up the alias for the name
335 public string LookupAlias (string name
)
337 return RootContext
.SourceBeingCompiled
.LookupAlias (name
);
341 // root_types contains all the types. All TopLevel types
342 // hence have a parent that points to `root_types', that is
343 // why there is a non-obvious test down here.
345 public bool IsTopLevel
{
348 if (parent
.parent
== null)
355 public virtual void CloseType ()
359 TypeBuilder
.CreateType ();
362 // The try/catch is needed because
363 // nested enumerations fail to load when they
366 // Even if this is the right order (enumerations
367 // declared after types).
369 // Note that this still creates the type and
370 // it is possible to save it
377 /// Should be overriten by the appropriate declaration space
379 public abstract TypeBuilder
DefineType ();
382 /// Define all members, but don't apply any attributes or do anything which may
383 /// access not-yet-defined classes. This method also creates the MemberCache.
385 public abstract bool DefineMembers (TypeContainer parent
);
388 // Whether this is an `unsafe context'
390 public bool UnsafeContext
{
392 if ((ModFlags
& Modifiers
.UNSAFE
) != 0)
395 return parent
.UnsafeContext
;
400 public static string MakeFQN (string nsn
, string name
)
402 string prefix
= (nsn
== "" ? "" : nsn
+ ".");
404 return prefix
+ name
;
407 EmitContext type_resolve_ec
;
408 EmitContext
GetTypeResolveEmitContext (TypeContainer parent
, Location loc
)
410 type_resolve_ec
= new EmitContext (parent
, this, loc
, null, null, ModFlags
, false);
411 type_resolve_ec
.ResolvingTypeTree
= true;
413 return type_resolve_ec
;
417 // Looks up the type, as parsed into the expression `e'
419 public Type
ResolveType (Expression e
, bool silent
, Location loc
)
421 if (type_resolve_ec
== null)
422 type_resolve_ec
= GetTypeResolveEmitContext (parent
, loc
);
423 type_resolve_ec
.loc
= loc
;
425 int errors
= Report
.Errors
;
426 Expression d
= e
.Resolve (type_resolve_ec
, ResolveFlags
.Type
);
427 if (d
== null || d
.eclass
!= ExprClass
.Type
){
428 if (!silent
&& errors
== Report
.Errors
){
429 Report
.Error (30002, loc
, "Cannot find type `"+ e
.ToString () +"'");
438 // Resolves the expression `e' for a type, and will recursively define
441 public Expression
ResolveTypeExpr (Expression e
, bool silent
, Location loc
)
443 if (type_resolve_ec
== null)
444 type_resolve_ec
= GetTypeResolveEmitContext (parent
, loc
);
446 Expression d
= e
.Resolve (type_resolve_ec
, ResolveFlags
.Type
);
447 if (d
== null || d
.eclass
!= ExprClass
.Type
){
449 Report
.Error (30002, loc
, "Cannot find type `"+ e
+"'");
457 Type
LookupInterfaceOrClass (string ns
, string name
, out bool error
)
463 name
= MakeFQN (ns
, name
);
465 t
= TypeManager
.LookupType (name
);
469 parent
= (DeclSpace
) RootContext
.Tree
.Decls
[name
];
473 t
= parent
.DefineType ();
475 Report
.Error (30256, Location
, "Class definition is circular: `"+name
+"'");
482 public static void Error_AmbiguousTypeReference (Location loc
, string name
, Type t1
, Type t2
)
484 Report
.Error (104, loc
,
485 String
.Format ("`{0}' is an ambiguous reference ({1} or {2}) ", name
,
486 t1
.FullName
, t2
.FullName
));
490 /// GetType is used to resolve type names at the DeclSpace level.
491 /// Use this to lookup class/struct bases, interface bases or
492 /// delegate type references
496 /// Contrast this to LookupType which is used inside method bodies to
497 /// lookup types that have already been defined. GetType is used
498 /// during the tree resolution process and potentially define
499 /// recursively the type
501 public Type
FindType (Location loc
, string name
)
507 // For the case the type we are looking for is nested within this one
508 // or is in any base class
510 DeclSpace containing_ds
= this;
512 while (containing_ds
!= null){
513 Type current_type
= containing_ds
.TypeBuilder
;
515 while (current_type
!= null) {
516 string pre
= current_type
.FullName
;
518 t
= LookupInterfaceOrClass (pre
, name
, out error
);
525 current_type
= current_type
.BaseType
;
527 containing_ds
= containing_ds
.Parent
;
531 // Attempt to lookup the class on our namespace and all it's implicit parents
533 for (string ns
= Namespace
.Name
; ns
!= null; ns
= RootContext
.ImplicitParent (ns
)) {
535 t
= LookupInterfaceOrClass (ns
, name
, out error
);
544 // Attempt to do a direct unqualified lookup
546 t
= LookupInterfaceOrClass ("", name
, out error
);
554 // Attempt to lookup the class on any of the `using'
558 for (Namespace ns
= Namespace
; ns
!= null; ns
= ns
.Parent
){
560 t
= LookupInterfaceOrClass (ns
.Name
, name
, out error
);
568 // Now check the using clause list
570 ICollection imports_list
= RootContext
.SourceBeingCompiled
.ImportsTable
;
572 if (imports_list
== null)
576 foreach (SourceBeingCompiled
.ImportsEntry ue
in imports_list
){
577 match
= LookupInterfaceOrClass (ue
.Name
, name
, out error
);
583 Error_AmbiguousTypeReference (loc
, name
, t
, match
);
595 //Report.Error (246, Location, "Can not find type `"+name+"'");
600 /// This function is broken and not what you're looking for. It should only
601 /// be used while the type is still being created since it doesn't use the cache
602 /// and relies on the filter doing the member name check.
604 public abstract MemberList
FindMembers (MemberTypes mt
, BindingFlags bf
,
605 MemberFilter filter
, object criteria
);
608 /// If we have a MemberCache, return it. This property may return null if the
609 /// class doesn't have a member cache or while it's still being created.
611 public abstract MemberCache MemberCache
{
617 /// This is a readonly list of MemberInfo's.
619 public class MemberList
: IList
{
620 public readonly IList List
;
624 /// Create a new MemberList from the given IList.
626 public MemberList (IList list
)
631 this.List
= new ArrayList ();
636 /// Concatenate the ILists `first' and `second' to a new MemberList.
638 public MemberList (IList first
, IList second
)
640 ArrayList list
= new ArrayList ();
641 list
.AddRange (first
);
642 list
.AddRange (second
);
647 public static readonly MemberList Empty
= new MemberList (new ArrayList ());
650 /// Cast the MemberList into a MemberInfo[] array.
653 /// This is an expensive operation, only use it if it's really necessary.
655 public static explicit operator MemberInfo
[] (MemberList list
)
657 Timer
.StartTimer (TimerType
.MiscTimer
);
658 MemberInfo
[] result
= new MemberInfo
[list
.Count
];
659 list
.CopyTo (result
, 0);
660 Timer
.StopTimer (TimerType
.MiscTimer
);
672 public bool IsSynchronized
{
674 return List
.IsSynchronized
;
678 public object SyncRoot
{
680 return List
.SyncRoot
;
684 public void CopyTo (Array array
, int index
)
686 List
.CopyTo (array
, index
);
691 public IEnumerator
GetEnumerator ()
693 return List
.GetEnumerator ();
698 public bool IsFixedSize
{
704 public bool IsReadOnly
{
710 object IList
.this [int index
] {
716 throw new NotSupportedException ();
720 // FIXME: try to find out whether we can avoid the cast in this indexer.
721 public MemberInfo
this [int index
] {
723 return (MemberInfo
) List
[index
];
727 public int Add (object value)
729 throw new NotSupportedException ();
734 throw new NotSupportedException ();
737 public bool Contains (object value)
739 return List
.Contains (value);
742 public int IndexOf (object value)
744 return List
.IndexOf (value);
747 public void Insert (int index
, object value)
749 throw new NotSupportedException ();
752 public void Remove (object value)
754 throw new NotSupportedException ();
757 public void RemoveAt (int index
)
759 throw new NotSupportedException ();
764 /// This interface is used to get all members of a class when creating the
765 /// member cache. It must be implemented by all DeclSpace derivatives which
766 /// want to support the member cache and by TypeHandle to get caching of
767 /// non-dynamic types.
769 public interface IMemberContainer
{
771 /// The name of the IMemberContainer. This is only used for
772 /// debugging purposes.
779 /// The type of this IMemberContainer.
786 /// Returns the IMemberContainer of the parent class or null if this
787 /// is an interface or TypeManger.object_type.
788 /// This is used when creating the member cache for a class to get all
789 /// members from the parent class.
791 IMemberContainer Parent
{
796 /// Whether this is an interface.
803 /// Returns all members of this class with the corresponding MemberTypes
804 /// and BindingFlags.
807 /// When implementing this method, make sure not to return any inherited
808 /// members and check the MemberTypes and BindingFlags properly.
809 /// Unfortunately, System.Reflection is lame and doesn't provide a way to
810 /// get the BindingFlags (static/non-static,public/non-public) in the
811 /// MemberInfo class, but the cache needs this information. That's why
812 /// this method is called multiple times with different BindingFlags.
814 MemberList
GetMembers (MemberTypes mt
, BindingFlags bf
);
817 /// Return the container's member cache.
819 MemberCache MemberCache
{
825 /// The MemberCache is used by dynamic and non-dynamic types to speed up
826 /// member lookups. It has a member name based hash table; it maps each member
827 /// name to a list of CacheEntry objects. Each CacheEntry contains a MemberInfo
828 /// and the BindingFlags that were initially used to get it. The cache contains
829 /// all members of the current class and all inherited members. If this cache is
830 /// for an interface types, it also contains all inherited members.
832 /// There are two ways to get a MemberCache:
833 /// * if this is a dynamic type, lookup the corresponding DeclSpace and then
834 /// use the DeclSpace.MemberCache property.
835 /// * if this not a dynamic type, call TypeHandle.GetTypeHandle() to get a
836 /// TypeHandle instance for the type and then use TypeHandle.MemberCache.
838 public class MemberCache
{
839 public readonly IMemberContainer Container
;
840 protected CaseInsensitiveHashtable member_hash
;
841 protected CaseInsensitiveHashtable method_hash
;
842 protected CaseInsensitiveHashtable interface_hash
;
845 /// Create a new MemberCache for the given IMemberContainer `container'.
847 public MemberCache (IMemberContainer container
)
849 this.Container
= container
;
851 Timer
.IncrementCounter (CounterType
.MemberCache
);
852 Timer
.StartTimer (TimerType
.CacheInit
);
854 interface_hash
= new CaseInsensitiveHashtable ();
856 // If we have a parent class (we have a parent class unless we're
857 // TypeManager.object_type), we deep-copy its MemberCache here.
858 if (Container
.Parent
!= null)
859 member_hash
= SetupCache (Container
.Parent
.MemberCache
);
860 else if (Container
.IsInterface
)
861 member_hash
= SetupCacheForInterface ();
863 member_hash
= new CaseInsensitiveHashtable ();
865 // If this is neither a dynamic type nor an interface, create a special
866 // method cache with all declared and inherited methods.
867 Type type
= container
.Type
;
868 if (!(type
is TypeBuilder
) && !type
.IsInterface
) {
869 method_hash
= new CaseInsensitiveHashtable ();
873 // Add all members from the current class.
874 AddMembers (Container
);
876 Timer
.StopTimer (TimerType
.CacheInit
);
880 /// Bootstrap this member cache by doing a deep-copy of our parent.
882 CaseInsensitiveHashtable
SetupCache (MemberCache parent
)
884 CaseInsensitiveHashtable hash
= new CaseInsensitiveHashtable ();
886 IDictionaryEnumerator it
= parent
.member_hash
.GetEnumerator ();
887 while (it
.MoveNext ()) {
888 ArrayList al
= (ArrayList
) it
.Value
;
889 // Constructors are never inherited and all have the same key
890 if ((((CacheEntry
)al
[0]).EntryType
& EntryType
.Constructor
) == 0)
891 hash
[it
.Key
] = ((ArrayList
) it
.Value
).Clone ();
897 void AddInterfaces (MemberCache parent
)
899 foreach (Type iface
in parent
.interface_hash
.Keys
) {
900 if (!interface_hash
.Contains (iface
))
901 interface_hash
.Add (iface
, true);
906 /// Add the contents of `new_hash' to `hash'.
908 void AddHashtable (Hashtable hash
, Hashtable new_hash
)
910 IDictionaryEnumerator it
= new_hash
.GetEnumerator ();
911 while (it
.MoveNext ()) {
912 ArrayList list
= (ArrayList
) hash
[it
.Key
];
914 list
.AddRange ((ArrayList
) it
.Value
);
916 hash
[it
.Key
] = ((ArrayList
) it
.Value
).Clone ();
921 /// Bootstrap the member cache for an interface type.
922 /// Type.GetMembers() won't return any inherited members for interface types,
923 /// so we need to do this manually. Interfaces also inherit from System.Object.
925 CaseInsensitiveHashtable
SetupCacheForInterface ()
927 CaseInsensitiveHashtable hash
= SetupCache (TypeHandle
.ObjectType
.MemberCache
);
928 Type
[] ifaces
= TypeManager
.GetInterfaces (Container
.Type
);
930 foreach (Type iface
in ifaces
) {
931 if (interface_hash
.Contains (iface
))
933 interface_hash
.Add (iface
, true);
935 IMemberContainer iface_container
=
936 TypeManager
.LookupMemberContainer (iface
);
938 MemberCache iface_cache
= iface_container
.MemberCache
;
939 AddHashtable (hash
, iface_cache
.member_hash
);
940 AddInterfaces (iface_cache
);
947 /// Add all members from class `container' to the cache.
949 void AddMembers (IMemberContainer container
)
951 // We need to call AddMembers() with a single member type at a time
952 // to get the member type part of CacheEntry.EntryType right.
953 AddMembers (MemberTypes
.Constructor
, container
);
954 AddMembers (MemberTypes
.Field
, container
);
955 AddMembers (MemberTypes
.Method
, container
);
956 AddMembers (MemberTypes
.Property
, container
);
957 AddMembers (MemberTypes
.Event
, container
);
958 // Nested types are returned by both Static and Instance searches.
959 AddMembers (MemberTypes
.NestedType
,
960 BindingFlags
.Static
| BindingFlags
.Public
, container
);
961 AddMembers (MemberTypes
.NestedType
,
962 BindingFlags
.Static
| BindingFlags
.NonPublic
, container
);
965 void AddMembers (MemberTypes mt
, IMemberContainer container
)
967 AddMembers (mt
, BindingFlags
.Static
| BindingFlags
.Public
, container
);
968 AddMembers (mt
, BindingFlags
.Static
| BindingFlags
.NonPublic
, container
);
969 AddMembers (mt
, BindingFlags
.Instance
| BindingFlags
.Public
, container
);
970 AddMembers (mt
, BindingFlags
.Instance
| BindingFlags
.NonPublic
, container
);
974 /// Add all members from class `container' with the requested MemberTypes and
975 /// BindingFlags to the cache. This method is called multiple times with different
976 /// MemberTypes and BindingFlags.
978 void AddMembers (MemberTypes mt
, BindingFlags bf
, IMemberContainer container
)
980 MemberList members
= container
.GetMembers (mt
, bf
);
981 BindingFlags new_bf
= (container
== Container
) ?
982 bf
| BindingFlags
.DeclaredOnly
: bf
;
984 foreach (MemberInfo member
in members
) {
985 string name
= member
.Name
;
987 // We use a name-based hash table of ArrayList's.
988 ArrayList list
= (ArrayList
) member_hash
[name
];
990 list
= new ArrayList ();
991 member_hash
.Add (name
, list
);
994 // When this method is called for the current class, the list will
995 // already contain all inherited members from our parent classes.
996 // We cannot add new members in front of the list since this'd be an
997 // expensive operation, that's why the list is sorted in reverse order
998 // (ie. members from the current class are coming last).
999 list
.Add (new CacheEntry (container
, member
, mt
, bf
));
1004 /// Add all declared and inherited methods from class `type' to the method cache.
1006 void AddMethods (Type type
)
1008 AddMethods (BindingFlags
.Static
| BindingFlags
.Public
, type
);
1009 AddMethods (BindingFlags
.Static
| BindingFlags
.NonPublic
, type
);
1010 AddMethods (BindingFlags
.Instance
| BindingFlags
.Public
, type
);
1011 AddMethods (BindingFlags
.Instance
| BindingFlags
.NonPublic
, type
);
1014 void AddMethods (BindingFlags bf
, Type type
)
1016 MemberInfo
[] members
= type
.GetMethods (bf
);
1018 foreach (MethodBase member
in members
) {
1019 string name
= member
.Name
;
1021 // Varargs methods aren't allowed in C# code.
1022 if ((member
.CallingConvention
& CallingConventions
.VarArgs
) != 0)
1025 // We use a name-based hash table of ArrayList's.
1026 ArrayList list
= (ArrayList
) method_hash
[name
];
1028 list
= new ArrayList ();
1029 method_hash
.Add (name
, list
);
1032 // Unfortunately, the elements returned by Type.GetMethods() aren't
1033 // sorted so we need to do this check for every member.
1034 BindingFlags new_bf
= bf
;
1035 if (member
.DeclaringType
== type
)
1036 new_bf
|= BindingFlags
.DeclaredOnly
;
1038 list
.Add (new CacheEntry (Container
, member
, MemberTypes
.Method
, new_bf
));
1043 /// Compute and return a appropriate `EntryType' magic number for the given
1044 /// MemberTypes and BindingFlags.
1046 protected static EntryType
GetEntryType (MemberTypes mt
, BindingFlags bf
)
1048 EntryType type
= EntryType
.None
;
1050 if ((mt
& MemberTypes
.Constructor
) != 0)
1051 type
|= EntryType
.Constructor
;
1052 if ((mt
& MemberTypes
.Event
) != 0)
1053 type
|= EntryType
.Event
;
1054 if ((mt
& MemberTypes
.Field
) != 0)
1055 type
|= EntryType
.Field
;
1056 if ((mt
& MemberTypes
.Method
) != 0)
1057 type
|= EntryType
.Method
;
1058 if ((mt
& MemberTypes
.Property
) != 0)
1059 type
|= EntryType
.Property
;
1060 // Nested types are returned by static and instance searches.
1061 if ((mt
& MemberTypes
.NestedType
) != 0)
1062 type
|= EntryType
.NestedType
| EntryType
.Static
| EntryType
.Instance
;
1064 if ((bf
& BindingFlags
.Instance
) != 0)
1065 type
|= EntryType
.Instance
;
1066 if ((bf
& BindingFlags
.Static
) != 0)
1067 type
|= EntryType
.Static
;
1068 if ((bf
& BindingFlags
.Public
) != 0)
1069 type
|= EntryType
.Public
;
1070 if ((bf
& BindingFlags
.NonPublic
) != 0)
1071 type
|= EntryType
.NonPublic
;
1072 if ((bf
& BindingFlags
.DeclaredOnly
) != 0)
1073 type
|= EntryType
.Declared
;
1079 /// The `MemberTypes' enumeration type is a [Flags] type which means that it may
1080 /// denote multiple member types. Returns true if the given flags value denotes a
1081 /// single member types.
1083 public static bool IsSingleMemberType (MemberTypes mt
)
1086 case MemberTypes
.Constructor
:
1087 case MemberTypes
.Event
:
1088 case MemberTypes
.Field
:
1089 case MemberTypes
.Method
:
1090 case MemberTypes
.Property
:
1091 case MemberTypes
.NestedType
:
1100 /// We encode the MemberTypes and BindingFlags of each members in a "magic"
1101 /// number to speed up the searching process.
1104 protected enum EntryType
{
1109 MaskStatic
= Instance
|Static
,
1113 MaskProtection
= Public
|NonPublic
,
1117 Constructor
= 0x020,
1124 MaskType
= Constructor
|Event
|Field
|Method
|Property
|NestedType
1127 protected struct CacheEntry
{
1128 public readonly IMemberContainer Container
;
1129 public readonly EntryType EntryType
;
1130 public readonly MemberInfo Member
;
1132 public CacheEntry (IMemberContainer container
, MemberInfo member
,
1133 MemberTypes mt
, BindingFlags bf
)
1135 this.Container
= container
;
1136 this.Member
= member
;
1137 this.EntryType
= GetEntryType (mt
, bf
);
1142 /// This is called each time we're walking up one level in the class hierarchy
1143 /// and checks whether we can abort the search since we've already found what
1144 /// we were looking for.
1146 protected bool DoneSearching (ArrayList list
)
1149 // We've found exactly one member in the current class and it's not
1150 // a method or constructor.
1152 if (list
.Count
== 1 && !(list
[0] is MethodBase
))
1156 // Multiple properties: we query those just to find out the indexer
1159 if ((list
.Count
> 0) && (list
[0] is PropertyInfo
))
1166 /// Looks up members with name `name'. If you provide an optional
1167 /// filter function, it'll only be called with members matching the
1168 /// requested member name.
1170 /// This method will try to use the cache to do the lookup if possible.
1172 /// Unlike other FindMembers implementations, this method will always
1173 /// check all inherited members - even when called on an interface type.
1175 /// If you know that you're only looking for methods, you should use
1176 /// MemberTypes.Method alone since this speeds up the lookup a bit.
1177 /// When doing a method-only search, it'll try to use a special method
1178 /// cache (unless it's a dynamic type or an interface) and the returned
1179 /// MemberInfo's will have the correct ReflectedType for inherited methods.
1180 /// The lookup process will automatically restart itself in method-only
1181 /// search mode if it discovers that it's about to return methods.
1183 public MemberList
FindMembers (MemberTypes mt
, BindingFlags bf
, string name
,
1184 MemberFilter filter
, object criteria
)
1186 bool declared_only
= (bf
& BindingFlags
.DeclaredOnly
) != 0;
1187 bool method_search
= mt
== MemberTypes
.Method
;
1188 // If we have a method cache and we aren't already doing a method-only search,
1189 // then we restart a method search if the first match is a method.
1190 bool do_method_search
= !method_search
&& (method_hash
!= null);
1191 bf
|= BindingFlags
.IgnoreCase
;
1192 ArrayList applicable
;
1194 // If this is a method-only search, we try to use the method cache if
1195 // possible; a lookup in the method cache will return a MemberInfo with
1196 // the correct ReflectedType for inherited methods.
1197 if (method_search
&& (method_hash
!= null))
1198 applicable
= (ArrayList
) method_hash
[name
];
1200 applicable
= (ArrayList
) member_hash
[name
];
1202 if (applicable
== null)
1203 return MemberList
.Empty
;
1205 ArrayList list
= new ArrayList ();
1207 Timer
.StartTimer (TimerType
.CachedLookup
);
1209 EntryType type
= GetEntryType (mt
, bf
);
1211 IMemberContainer current
= Container
;
1213 // `applicable' is a list of all members with the given member name `name'
1214 // in the current class and all its parent classes. The list is sorted in
1215 // reverse order due to the way how the cache is initialy created (to speed
1216 // things up, we're doing a deep-copy of our parent).
1218 for (int i
= applicable
.Count
-1; i
>= 0; i
--) {
1219 CacheEntry entry
= (CacheEntry
) applicable
[i
];
1221 // This happens each time we're walking one level up in the class
1222 // hierarchy. If we're doing a DeclaredOnly search, we must abort
1223 // the first time this happens (this may already happen in the first
1224 // iteration of this loop if there are no members with the name we're
1225 // looking for in the current class).
1226 if (entry
.Container
!= current
) {
1227 if (declared_only
|| DoneSearching (list
))
1230 current
= entry
.Container
;
1233 // Is the member of the correct type ?
1234 if ((entry
.EntryType
& type
& EntryType
.MaskType
) == 0)
1237 // Is the member static/non-static ?
1238 if ((entry
.EntryType
& type
& EntryType
.MaskStatic
) == 0)
1241 // Apply the filter to it.
1242 if (filter (entry
.Member
, criteria
)) {
1243 if ((entry
.EntryType
& EntryType
.MaskType
) != EntryType
.Method
)
1244 do_method_search
= false;
1245 list
.Add (entry
.Member
);
1249 Timer
.StopTimer (TimerType
.CachedLookup
);
1251 // If we have a method cache and we aren't already doing a method-only
1252 // search, we restart in method-only search mode if the first match is
1253 // a method. This ensures that we return a MemberInfo with the correct
1254 // ReflectedType for inherited methods.
1255 if (do_method_search
&& (list
.Count
> 0))
1256 return FindMembers (MemberTypes
.Method
, bf
, name
, filter
, criteria
);
1258 return new MemberList (list
);