1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 using System
.Collections
.Generic
;
6 using System
.Runtime
.CompilerServices
;
7 using System
.Runtime
.Serialization
;
9 namespace System
.Reflection
11 public abstract partial class Module
: ICustomAttributeProvider
, ISerializable
13 protected Module() { }
15 public virtual Assembly Assembly
=> throw NotImplemented
.ByDesign
;
16 public virtual string FullyQualifiedName
=> throw NotImplemented
.ByDesign
;
17 public virtual string Name
=> throw NotImplemented
.ByDesign
;
19 public virtual int MDStreamVersion
=> throw NotImplemented
.ByDesign
;
20 public virtual Guid ModuleVersionId
=> throw NotImplemented
.ByDesign
;
21 public virtual string ScopeName
=> throw NotImplemented
.ByDesign
;
22 public ModuleHandle ModuleHandle
=> GetModuleHandleImpl();
23 protected virtual ModuleHandle
GetModuleHandleImpl() => ModuleHandle
.EmptyHandle
; // Not an api but declared protected because of Reflection.Core/Corelib divide (when built by CoreRt)
24 public virtual void GetPEKind(out PortableExecutableKinds peKind
, out ImageFileMachine machine
) { throw NotImplemented.ByDesign; }
25 public virtual bool IsResource() { throw NotImplemented.ByDesign; }
27 public virtual bool IsDefined(Type attributeType
, bool inherit
) { throw NotImplemented.ByDesign; }
28 public virtual IEnumerable
<CustomAttributeData
> CustomAttributes
=> GetCustomAttributesData();
29 public virtual IList
<CustomAttributeData
> GetCustomAttributesData() { throw NotImplemented.ByDesign; }
30 public virtual object[] GetCustomAttributes(bool inherit
) { throw NotImplemented.ByDesign; }
31 public virtual object[] GetCustomAttributes(Type attributeType
, bool inherit
) { throw NotImplemented.ByDesign; }
33 public MethodInfo
? GetMethod(string name
)
36 throw new ArgumentNullException(nameof(name
));
38 return GetMethodImpl(name
, Module
.DefaultLookup
, null, CallingConventions
.Any
, null, null);
41 public MethodInfo
? GetMethod(string name
, Type
[] types
) => GetMethod(name
, Module
.DefaultLookup
, null, CallingConventions
.Any
, types
, null);
42 public MethodInfo
? GetMethod(string name
, BindingFlags bindingAttr
, Binder
? binder
, CallingConventions callConvention
, Type
[] types
, ParameterModifier
[]? modifiers
)
45 throw new ArgumentNullException(nameof(name
));
47 throw new ArgumentNullException(nameof(types
));
48 for (int i
= 0; i
< types
.Length
; i
++)
51 throw new ArgumentNullException(nameof(types
));
53 return GetMethodImpl(name
, bindingAttr
, binder
, callConvention
, types
, modifiers
);
56 protected virtual MethodInfo
? GetMethodImpl(string name
, BindingFlags bindingAttr
, Binder
? binder
, CallingConventions callConvention
, Type
[]? types
, ParameterModifier
[]? modifiers
) { throw NotImplemented.ByDesign; }
58 public MethodInfo
[] GetMethods() => GetMethods(Module
.DefaultLookup
);
59 public virtual MethodInfo
[] GetMethods(BindingFlags bindingFlags
) { throw NotImplemented.ByDesign; }
61 public FieldInfo
? GetField(string name
) => GetField(name
, Module
.DefaultLookup
);
62 public virtual FieldInfo
? GetField(string name
, BindingFlags bindingAttr
) { throw NotImplemented.ByDesign; }
64 public FieldInfo
[] GetFields() => GetFields(Module
.DefaultLookup
);
65 public virtual FieldInfo
[] GetFields(BindingFlags bindingFlags
) { throw NotImplemented.ByDesign; }
67 public virtual Type
[] GetTypes() { throw NotImplemented.ByDesign; }
69 public virtual Type
? GetType(string className
) => GetType(className
, throwOnError
: false, ignoreCase
: false);
70 public virtual Type
? GetType(string className
, bool ignoreCase
) => GetType(className
, throwOnError
: false, ignoreCase
: ignoreCase
);
71 public virtual Type
? GetType(string className
, bool throwOnError
, bool ignoreCase
) { throw NotImplemented.ByDesign; }
73 public virtual Type
[] FindTypes(TypeFilter
? filter
, object? filterCriteria
)
75 Type
[] c
= GetTypes();
77 for (int i
= 0; i
< c
.Length
; i
++)
79 if (filter
!= null && !filter(c
[i
], filterCriteria
))
87 Type
[] ret
= new Type
[cnt
];
89 for (int i
= 0; i
< c
.Length
; i
++)
97 public virtual int MetadataToken
=> throw NotImplemented
.ByDesign
;
99 public FieldInfo
? ResolveField(int metadataToken
) => ResolveField(metadataToken
, null, null);
100 public virtual FieldInfo
? ResolveField(int metadataToken
, Type
[]? genericTypeArguments
, Type
[]? genericMethodArguments
) { throw NotImplemented.ByDesign; }
102 public MemberInfo
? ResolveMember(int metadataToken
) => ResolveMember(metadataToken
, null, null);
103 public virtual MemberInfo
? ResolveMember(int metadataToken
, Type
[]? genericTypeArguments
, Type
[]? genericMethodArguments
) { throw NotImplemented.ByDesign; }
105 public MethodBase
? ResolveMethod(int metadataToken
) => ResolveMethod(metadataToken
, null, null);
106 public virtual MethodBase
? ResolveMethod(int metadataToken
, Type
[]? genericTypeArguments
, Type
[]? genericMethodArguments
) { throw NotImplemented.ByDesign; }
108 public virtual byte[] ResolveSignature(int metadataToken
) { throw NotImplemented.ByDesign; }
109 public virtual string ResolveString(int metadataToken
) { throw NotImplemented.ByDesign; }
111 public Type
ResolveType(int metadataToken
) => ResolveType(metadataToken
, null, null);
112 public virtual Type
ResolveType(int metadataToken
, Type
[]? genericTypeArguments
, Type
[]? genericMethodArguments
) { throw NotImplemented.ByDesign; }
114 public virtual void GetObjectData(SerializationInfo info
, StreamingContext context
) { throw NotImplemented.ByDesign; }
116 public override bool Equals(object? o
) => base.Equals(o
);
117 public override int GetHashCode() => base.GetHashCode();
119 [MethodImpl(MethodImplOptions
.AggressiveInlining
)]
120 public static bool operator ==(Module
? left
, Module
? right
)
122 // Test "right" first to allow branch elimination when inlined for null checks (== null)
123 // so it can become a simple test
126 // return true/false not the test result https://github.com/dotnet/coreclr/issues/914
127 return (left
is null) ? true : false;
130 // Try fast reference equality and opposite null check prior to calling the slower virtual Equals
131 if ((object?)left
== (object)right
)
136 return (left
is null) ? false : left
.Equals(right
);
139 public static bool operator !=(Module
? left
, Module
? right
) => !(left
== right
);
141 public override string ToString() => ScopeName
;
143 public static readonly TypeFilter FilterTypeName
= (m
, c
) => FilterTypeNameImpl(m
, c
!, StringComparison
.Ordinal
);
144 public static readonly TypeFilter FilterTypeNameIgnoreCase
= (m
, c
) => FilterTypeNameImpl(m
, c
!, StringComparison
.OrdinalIgnoreCase
);
146 private const BindingFlags DefaultLookup
= BindingFlags
.Instance
| BindingFlags
.Static
| BindingFlags
.Public
;
149 // This method will filter the class based upon the name. It supports
150 // a trailing wild card.
151 private static bool FilterTypeNameImpl(Type cls
, object filterCriteria
, StringComparison comparison
)
153 // Check that the criteria object is a String object
154 if (!(filterCriteria
is string str
))
156 throw new InvalidFilterCriteriaException(SR
.InvalidFilterCriteriaException_CritString
);
158 // Check to see if this is a prefix or exact match requirement
159 if (str
.Length
> 0 && str
[^
1] == '*')
161 ReadOnlySpan
<char> slice
= str
.AsSpan(0, str
.Length
- 1);
162 return cls
.Name
.AsSpan().StartsWith(slice
, comparison
);
165 return cls
.Name
.Equals(str
, comparison
);