diff options
Diffstat (limited to 'src/mscorlib/shared/System/Reflection')
76 files changed, 3135 insertions, 0 deletions
diff --git a/src/mscorlib/shared/System/Reflection/AmbiguousMatchException.cs b/src/mscorlib/shared/System/Reflection/AmbiguousMatchException.cs new file mode 100644 index 0000000000..459a19cb71 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/AmbiguousMatchException.cs @@ -0,0 +1,35 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.Serialization; + +namespace System.Reflection +{ + [Serializable] + public sealed class AmbiguousMatchException : SystemException + { + public AmbiguousMatchException() + : base(SR.RFLCT_Ambiguous) + { + HResult = __HResults.COR_E_AMBIGUOUSMATCH; + } + + public AmbiguousMatchException(string message) + : base(message) + { + HResult = __HResults.COR_E_AMBIGUOUSMATCH; + } + + public AmbiguousMatchException(string message, Exception inner) + : base(message, inner) + { + HResult = __HResults.COR_E_AMBIGUOUSMATCH; + } + + internal AmbiguousMatchException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } + } +} diff --git a/src/mscorlib/shared/System/Reflection/Assembly.cs b/src/mscorlib/shared/System/Reflection/Assembly.cs new file mode 100644 index 0000000000..d35ffc7066 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/Assembly.cs @@ -0,0 +1,200 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.IO; +using System.Globalization; +using System.Collections.Generic; +using System.Configuration.Assemblies; +using System.Runtime.Serialization; +using System.Security; + +namespace System.Reflection +{ + public abstract partial class Assembly : ICustomAttributeProvider, ISerializable + { + protected Assembly() { } + + public virtual IEnumerable<TypeInfo> DefinedTypes + { + get + { + Type[] types = GetTypes(); + TypeInfo[] typeinfos = new TypeInfo[types.Length]; + for (int i = 0; i < types.Length; i++) + { + TypeInfo typeinfo = types[i].GetTypeInfo(); + if (typeinfo == null) + throw new NotSupportedException(SR.Format(SR.NotSupported_NoTypeInfo, types[i].FullName)); + + typeinfos[i] = typeinfo; + } + return typeinfos; + } + } + + public virtual Type[] GetTypes() + { + Module[] m = GetModules(false); + + int finalLength = 0; + Type[][] moduleTypes = new Type[m.Length][]; + + for (int i = 0; i < moduleTypes.Length; i++) + { + moduleTypes[i] = m[i].GetTypes(); + finalLength += moduleTypes[i].Length; + } + + int current = 0; + Type[] ret = new Type[finalLength]; + for (int i = 0; i < moduleTypes.Length; i++) + { + int length = moduleTypes[i].Length; + Array.Copy(moduleTypes[i], 0, ret, current, length); + current += length; + } + + return ret; + } + + public virtual IEnumerable<Type> ExportedTypes => GetExportedTypes(); + public virtual Type[] GetExportedTypes() { throw NotImplemented.ByDesign; } + + public virtual string CodeBase { get { throw NotImplemented.ByDesign; } } + public virtual MethodInfo EntryPoint { get { throw NotImplemented.ByDesign; } } + public virtual string FullName { get { throw NotImplemented.ByDesign; } } + public virtual string ImageRuntimeVersion { get { throw NotImplemented.ByDesign; } } + public virtual bool IsDynamic => false; + public virtual string Location { get { throw NotImplemented.ByDesign; } } + public virtual bool ReflectionOnly { get { throw NotImplemented.ByDesign; } } + + public virtual ManifestResourceInfo GetManifestResourceInfo(string resourceName) { throw NotImplemented.ByDesign; } + public virtual string[] GetManifestResourceNames() { throw NotImplemented.ByDesign; } + public virtual Stream GetManifestResourceStream(string name) { throw NotImplemented.ByDesign; } + public virtual Stream GetManifestResourceStream(Type type, string name) { throw NotImplemented.ByDesign; } + + public bool IsFullyTrusted => true; + + public virtual AssemblyName GetName() => GetName(copiedName: false); + public virtual AssemblyName GetName(bool copiedName) { throw NotImplemented.ByDesign; } + + public virtual Type GetType(string name) => GetType(name, throwOnError: false, ignoreCase: false); + public virtual Type GetType(string name, bool throwOnError) => GetType(name, throwOnError: throwOnError, ignoreCase: false); + public virtual Type GetType(string name, bool throwOnError, bool ignoreCase) { throw NotImplemented.ByDesign; } + + public virtual bool IsDefined(Type attributeType, bool inherit) { throw NotImplemented.ByDesign; } + + public virtual IEnumerable<CustomAttributeData> CustomAttributes => GetCustomAttributesData(); + public virtual IList<CustomAttributeData> GetCustomAttributesData() { throw NotImplemented.ByDesign; } + + public virtual object[] GetCustomAttributes(bool inherit) { throw NotImplemented.ByDesign; } + public virtual object[] GetCustomAttributes(Type attributeType, bool inherit) { throw NotImplemented.ByDesign; } + + public virtual string EscapedCodeBase => AssemblyName.EscapeCodeBase(CodeBase); + + public object CreateInstance(string typeName) => CreateInstance(typeName, false, BindingFlags.Public | BindingFlags.Instance, binder: null, args: null, culture: null, activationAttributes: null); + public object CreateInstance(string typeName, bool ignoreCase) => CreateInstance(typeName, ignoreCase, BindingFlags.Public | BindingFlags.Instance, binder: null, args: null, culture: null, activationAttributes: null); + public virtual object CreateInstance(string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes) + { + Type t = GetType(typeName, throwOnError: false, ignoreCase: ignoreCase); + if (t == null) + return null; + + return Activator.CreateInstance(t, bindingAttr, binder, args, culture, activationAttributes); + } + + public virtual event ModuleResolveEventHandler ModuleResolve { add { throw NotImplemented.ByDesign; } remove { throw NotImplemented.ByDesign; } } + + public virtual Module ManifestModule { get { throw NotImplemented.ByDesign; } } + public virtual Module GetModule(string name) { throw NotImplemented.ByDesign; } + + public Module[] GetModules() => GetModules(getResourceModules: false); + public virtual Module[] GetModules(bool getResourceModules) { throw NotImplemented.ByDesign; } + + public virtual IEnumerable<Module> Modules => GetLoadedModules(getResourceModules: true); + public Module[] GetLoadedModules() => GetLoadedModules(getResourceModules: false); + public virtual Module[] GetLoadedModules(bool getResourceModules) { throw NotImplemented.ByDesign; } + + public virtual AssemblyName[] GetReferencedAssemblies() { throw NotImplemented.ByDesign; } + + public virtual Assembly GetSatelliteAssembly(CultureInfo culture) { throw NotImplemented.ByDesign; } + public virtual Assembly GetSatelliteAssembly(CultureInfo culture, Version version) { throw NotImplemented.ByDesign; } + + public virtual FileStream GetFile(string name) { throw NotImplemented.ByDesign; } + public virtual FileStream[] GetFiles() => GetFiles(getResourceModules: false); + public virtual FileStream[] GetFiles(bool getResourceModules) { throw NotImplemented.ByDesign; } + + public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { throw NotImplemented.ByDesign; } + + public override string ToString() + { + string displayName = FullName; + if (displayName == null) + return base.ToString(); + else + return displayName; + } + + /* + Returns true if the assembly was loaded from the global assembly cache. + */ + public virtual bool GlobalAssemblyCache { get { throw NotImplemented.ByDesign; } } + public virtual Int64 HostContext { get { throw NotImplemented.ByDesign; } } + + public override bool Equals(object o) => base.Equals(o); + public override int GetHashCode() => base.GetHashCode(); + + public static bool operator ==(Assembly left, Assembly right) + { + if (object.ReferenceEquals(left, right)) + return true; + + if ((object)left == null || (object)right == null) + return false; + + return left.Equals(right); + } + + public static bool operator !=(Assembly left, Assembly right) + { + return !(left == right); + } + + public static string CreateQualifiedName(string assemblyName, string typeName) => typeName + ", " + assemblyName; + + public static Assembly GetAssembly(Type type) + { + if (type == null) + throw new ArgumentNullException(nameof(type)); + + Module m = type.Module; + if (m == null) + return null; + else + return m.Assembly; + } + + public static Assembly Load(byte[] rawAssembly) => Load(rawAssembly, rawSymbolStore: null); + + [Obsolete("This method has been deprecated. Please use Assembly.Load() instead. http://go.microsoft.com/fwlink/?linkid=14202")] + public static Assembly LoadWithPartialName(string partialName) + { + if (partialName == null) + throw new ArgumentNullException(nameof(partialName)); + + return Load(partialName); + } + + public static Assembly UnsafeLoadFrom(string assemblyFile) => LoadFrom(assemblyFile); + + public Module LoadModule(string moduleName, byte[] rawModule) => LoadModule(moduleName, rawModule, null); + public virtual Module LoadModule(string moduleName, byte[] rawModule, byte[] rawSymbolStore) { throw NotImplemented.ByDesign; } + + public static Assembly ReflectionOnlyLoad(byte[] rawAssembly) { throw new PlatformNotSupportedException(SR.PlatformNotSupported_ReflectionOnly); } + public static Assembly ReflectionOnlyLoad(string assemblyString) { throw new PlatformNotSupportedException(SR.PlatformNotSupported_ReflectionOnly); } + public static Assembly ReflectionOnlyLoadFrom(string assemblyFile) { throw new PlatformNotSupportedException(SR.PlatformNotSupported_ReflectionOnly); } + + public virtual SecurityRuleSet SecurityRuleSet => SecurityRuleSet.None; + } +} diff --git a/src/mscorlib/shared/System/Reflection/AssemblyAlgorithmIdAttribute.cs b/src/mscorlib/shared/System/Reflection/AssemblyAlgorithmIdAttribute.cs new file mode 100644 index 0000000000..fe24f353be --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/AssemblyAlgorithmIdAttribute.cs @@ -0,0 +1,27 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Configuration.Assemblies; + +namespace System.Reflection +{ + [AttributeUsage(AttributeTargets.Assembly, Inherited = false)] + public sealed class AssemblyAlgorithmIdAttribute : Attribute + { + public AssemblyAlgorithmIdAttribute(AssemblyHashAlgorithm algorithmId) + { + AlgorithmId = (uint)algorithmId; + } + + [CLSCompliant(false)] + public AssemblyAlgorithmIdAttribute(uint algorithmId) + { + AlgorithmId = algorithmId; + } + + [CLSCompliant(false)] + public uint AlgorithmId { get; } + } +} + diff --git a/src/mscorlib/shared/System/Reflection/AssemblyCompanyAttribute.cs b/src/mscorlib/shared/System/Reflection/AssemblyCompanyAttribute.cs new file mode 100644 index 0000000000..d986db60a3 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/AssemblyCompanyAttribute.cs @@ -0,0 +1,18 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [AttributeUsage(AttributeTargets.Assembly, Inherited = false)] + public sealed class AssemblyCompanyAttribute : Attribute + { + public AssemblyCompanyAttribute(string company) + { + Company = company; + } + + public string Company { get; } + } +} + diff --git a/src/mscorlib/shared/System/Reflection/AssemblyConfigurationAttribute.cs b/src/mscorlib/shared/System/Reflection/AssemblyConfigurationAttribute.cs new file mode 100644 index 0000000000..195c4d0ca6 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/AssemblyConfigurationAttribute.cs @@ -0,0 +1,18 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [AttributeUsage(AttributeTargets.Assembly, Inherited = false)] + public sealed class AssemblyConfigurationAttribute : Attribute + { + public AssemblyConfigurationAttribute(string configuration) + { + Configuration = configuration; + } + + public string Configuration { get; } + } +} + diff --git a/src/mscorlib/shared/System/Reflection/AssemblyContentType.cs b/src/mscorlib/shared/System/Reflection/AssemblyContentType.cs new file mode 100644 index 0000000000..2ee1a00818 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/AssemblyContentType.cs @@ -0,0 +1,13 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + public enum AssemblyContentType + { + Default = 0, + WindowsRuntime = 1, + } +} + diff --git a/src/mscorlib/shared/System/Reflection/AssemblyCopyrightAttribute.cs b/src/mscorlib/shared/System/Reflection/AssemblyCopyrightAttribute.cs new file mode 100644 index 0000000000..e50e19932b --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/AssemblyCopyrightAttribute.cs @@ -0,0 +1,18 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [AttributeUsage(AttributeTargets.Assembly, Inherited = false)] + public sealed class AssemblyCopyrightAttribute : Attribute + { + public AssemblyCopyrightAttribute(string copyright) + { + Copyright = copyright; + } + + public string Copyright { get; } + } +} + diff --git a/src/mscorlib/shared/System/Reflection/AssemblyCultureAttribute.cs b/src/mscorlib/shared/System/Reflection/AssemblyCultureAttribute.cs new file mode 100644 index 0000000000..e31c6f9c1c --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/AssemblyCultureAttribute.cs @@ -0,0 +1,18 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [AttributeUsage(AttributeTargets.Assembly, Inherited = false)] + public sealed class AssemblyCultureAttribute : Attribute + { + public AssemblyCultureAttribute(string culture) + { + Culture = culture; + } + + public string Culture { get; } + } +} + diff --git a/src/mscorlib/shared/System/Reflection/AssemblyDefaultAliasAttribute.cs b/src/mscorlib/shared/System/Reflection/AssemblyDefaultAliasAttribute.cs new file mode 100644 index 0000000000..ced35ed3fd --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/AssemblyDefaultAliasAttribute.cs @@ -0,0 +1,18 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [AttributeUsage(AttributeTargets.Assembly, Inherited = false)] + public sealed class AssemblyDefaultAliasAttribute : Attribute + { + public AssemblyDefaultAliasAttribute(string defaultAlias) + { + DefaultAlias = defaultAlias; + } + + public string DefaultAlias { get; } + } +} + diff --git a/src/mscorlib/shared/System/Reflection/AssemblyDelaySignAttribute.cs b/src/mscorlib/shared/System/Reflection/AssemblyDelaySignAttribute.cs new file mode 100644 index 0000000000..eae2cf613c --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/AssemblyDelaySignAttribute.cs @@ -0,0 +1,18 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [AttributeUsage(AttributeTargets.Assembly, Inherited = false)] + public sealed class AssemblyDelaySignAttribute : Attribute + { + public AssemblyDelaySignAttribute(bool delaySign) + { + DelaySign = delaySign; + } + + public bool DelaySign { get; } + } +} + diff --git a/src/mscorlib/shared/System/Reflection/AssemblyDescriptionAttribute.cs b/src/mscorlib/shared/System/Reflection/AssemblyDescriptionAttribute.cs new file mode 100644 index 0000000000..50f57c96a6 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/AssemblyDescriptionAttribute.cs @@ -0,0 +1,18 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [AttributeUsage(AttributeTargets.Assembly, Inherited = false)] + public sealed class AssemblyDescriptionAttribute : Attribute + { + public AssemblyDescriptionAttribute(string description) + { + Description = description; + } + + public string Description { get; } + } +} + diff --git a/src/mscorlib/shared/System/Reflection/AssemblyFileVersionAttribute.cs b/src/mscorlib/shared/System/Reflection/AssemblyFileVersionAttribute.cs new file mode 100644 index 0000000000..b5face65bc --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/AssemblyFileVersionAttribute.cs @@ -0,0 +1,20 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [AttributeUsage(AttributeTargets.Assembly, Inherited = false)] + public sealed class AssemblyFileVersionAttribute : Attribute + { + public AssemblyFileVersionAttribute(string version) + { + if (version == null) + throw new ArgumentNullException(nameof(version)); + Version = version; + } + + public string Version { get; } + } +} + diff --git a/src/mscorlib/shared/System/Reflection/AssemblyFlagsAttribute.cs b/src/mscorlib/shared/System/Reflection/AssemblyFlagsAttribute.cs new file mode 100644 index 0000000000..103413340c --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/AssemblyFlagsAttribute.cs @@ -0,0 +1,43 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [AttributeUsage(AttributeTargets.Assembly, Inherited = false)] + public sealed class AssemblyFlagsAttribute : Attribute + { + private AssemblyNameFlags _flags; + + [Obsolete("This constructor has been deprecated. Please use AssemblyFlagsAttribute(AssemblyNameFlags) instead. http://go.microsoft.com/fwlink/?linkid=14202")] + [CLSCompliant(false)] + public AssemblyFlagsAttribute(uint flags) + { + _flags = (AssemblyNameFlags)flags; + } + + [Obsolete("This property has been deprecated. Please use AssemblyFlags instead. http://go.microsoft.com/fwlink/?linkid=14202")] + [CLSCompliant(false)] + public uint Flags + { + get { return (uint)_flags; } + } + + public int AssemblyFlags + { + get { return (int)_flags; } + } + + [Obsolete("This constructor has been deprecated. Please use AssemblyFlagsAttribute(AssemblyNameFlags) instead. http://go.microsoft.com/fwlink/?linkid=14202")] + public AssemblyFlagsAttribute(int assemblyFlags) + { + _flags = (AssemblyNameFlags)assemblyFlags; + } + + public AssemblyFlagsAttribute(AssemblyNameFlags assemblyFlags) + { + _flags = assemblyFlags; + } + } +} + diff --git a/src/mscorlib/shared/System/Reflection/AssemblyInformationalVersionAttribute.cs b/src/mscorlib/shared/System/Reflection/AssemblyInformationalVersionAttribute.cs new file mode 100644 index 0000000000..915b973ab9 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/AssemblyInformationalVersionAttribute.cs @@ -0,0 +1,18 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [AttributeUsage(AttributeTargets.Assembly, Inherited = false)] + public sealed class AssemblyInformationalVersionAttribute : Attribute + { + public AssemblyInformationalVersionAttribute(string informationalVersion) + { + InformationalVersion = informationalVersion; + } + + public string InformationalVersion { get; } + } +} + diff --git a/src/mscorlib/shared/System/Reflection/AssemblyKeyFileAttribute.cs b/src/mscorlib/shared/System/Reflection/AssemblyKeyFileAttribute.cs new file mode 100644 index 0000000000..9f7387d8af --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/AssemblyKeyFileAttribute.cs @@ -0,0 +1,18 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [AttributeUsage(AttributeTargets.Assembly, Inherited = false)] + public sealed class AssemblyKeyFileAttribute : Attribute + { + public AssemblyKeyFileAttribute(string keyFile) + { + KeyFile = keyFile; + } + + public string KeyFile { get; } + } +} + diff --git a/src/mscorlib/shared/System/Reflection/AssemblyKeyNameAttribute.cs b/src/mscorlib/shared/System/Reflection/AssemblyKeyNameAttribute.cs new file mode 100644 index 0000000000..4cf51754ea --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/AssemblyKeyNameAttribute.cs @@ -0,0 +1,18 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [AttributeUsage(AttributeTargets.Assembly, Inherited = false)] + public sealed class AssemblyKeyNameAttribute : Attribute + { + public AssemblyKeyNameAttribute(string keyName) + { + KeyName = keyName; + } + + public string KeyName { get; } + } +} + diff --git a/src/mscorlib/shared/System/Reflection/AssemblyMetadataAttribute.cs b/src/mscorlib/shared/System/Reflection/AssemblyMetadataAttribute.cs new file mode 100644 index 0000000000..de9f6351ec --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/AssemblyMetadataAttribute.cs @@ -0,0 +1,21 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)] + public sealed class AssemblyMetadataAttribute : Attribute + { + public AssemblyMetadataAttribute(string key, string value) + { + Key = key; + Value = value; + } + + public string Key { get; } + + public string Value { get; } + } +} + diff --git a/src/mscorlib/shared/System/Reflection/AssemblyNameFlags.cs b/src/mscorlib/shared/System/Reflection/AssemblyNameFlags.cs new file mode 100644 index 0000000000..d321032031 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/AssemblyNameFlags.cs @@ -0,0 +1,21 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [Flags] + public enum AssemblyNameFlags + { + None = 0x0000, + // Flag used to indicate that an assembly ref contains the full public key, not the compressed token. + // Must match afPublicKey in CorHdr.h. + PublicKey = 0x0001, + //ProcArchMask = 0x00F0, // Bits describing the processor architecture + // Accessible via AssemblyName.ProcessorArchitecture + EnableJITcompileOptimizer = 0x4000, + EnableJITcompileTracking = 0x8000, + Retargetable = 0x0100, + //ContentType = 0x0E00, // Bits describing the ContentType are accessible via AssemblyName.ContentType + } +} diff --git a/src/mscorlib/shared/System/Reflection/AssemblyProductAttribute.cs b/src/mscorlib/shared/System/Reflection/AssemblyProductAttribute.cs new file mode 100644 index 0000000000..43cb62df99 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/AssemblyProductAttribute.cs @@ -0,0 +1,18 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [AttributeUsage(AttributeTargets.Assembly, Inherited = false)] + public sealed class AssemblyProductAttribute : Attribute + { + public AssemblyProductAttribute(string product) + { + Product = product; + } + + public string Product { get; } + } +} + diff --git a/src/mscorlib/shared/System/Reflection/AssemblySignatureKeyAttribute.cs b/src/mscorlib/shared/System/Reflection/AssemblySignatureKeyAttribute.cs new file mode 100644 index 0000000000..e6ec8af1b3 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/AssemblySignatureKeyAttribute.cs @@ -0,0 +1,21 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + public sealed class AssemblySignatureKeyAttribute : Attribute + { + public AssemblySignatureKeyAttribute(string publicKey, string countersignature) + { + PublicKey = publicKey; + Countersignature = countersignature; + } + + public string PublicKey { get; } + + public string Countersignature { get; } + } +} + diff --git a/src/mscorlib/shared/System/Reflection/AssemblyTitleAttribute.cs b/src/mscorlib/shared/System/Reflection/AssemblyTitleAttribute.cs new file mode 100644 index 0000000000..26d7a2e66c --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/AssemblyTitleAttribute.cs @@ -0,0 +1,18 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [AttributeUsage(AttributeTargets.Assembly, Inherited = false)] + public sealed class AssemblyTitleAttribute : Attribute + { + public AssemblyTitleAttribute(string title) + { + Title = title; + } + + public string Title { get; } + } +} + diff --git a/src/mscorlib/shared/System/Reflection/AssemblyTrademarkAttribute.cs b/src/mscorlib/shared/System/Reflection/AssemblyTrademarkAttribute.cs new file mode 100644 index 0000000000..1d3edf51d5 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/AssemblyTrademarkAttribute.cs @@ -0,0 +1,18 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [AttributeUsage(AttributeTargets.Assembly, Inherited = false)] + public sealed class AssemblyTrademarkAttribute : Attribute + { + public AssemblyTrademarkAttribute(string trademark) + { + Trademark = trademark; + } + + public string Trademark { get; } + } +} + diff --git a/src/mscorlib/shared/System/Reflection/AssemblyVersionAttribute.cs b/src/mscorlib/shared/System/Reflection/AssemblyVersionAttribute.cs new file mode 100644 index 0000000000..b3557bac97 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/AssemblyVersionAttribute.cs @@ -0,0 +1,18 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [AttributeUsage(AttributeTargets.Assembly, Inherited = false)] + public sealed class AssemblyVersionAttribute : Attribute + { + public AssemblyVersionAttribute(string version) + { + Version = version; + } + + public string Version { get; } + } +} + diff --git a/src/mscorlib/shared/System/Reflection/Binder.cs b/src/mscorlib/shared/System/Reflection/Binder.cs new file mode 100644 index 0000000000..3dc5665d52 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/Binder.cs @@ -0,0 +1,19 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Globalization; + +namespace System.Reflection +{ + public abstract class Binder + { + protected Binder() { } + public abstract FieldInfo BindToField(BindingFlags bindingAttr, FieldInfo[] match, object value, CultureInfo culture); + public abstract MethodBase BindToMethod(BindingFlags bindingAttr, MethodBase[] match, ref object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] names, out object state); + public abstract object ChangeType(object value, Type type, CultureInfo culture); + public abstract void ReorderArgumentArray(ref object[] args, object state); + public abstract MethodBase SelectMethod(BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers); + public abstract PropertyInfo SelectProperty(BindingFlags bindingAttr, PropertyInfo[] match, Type returnType, Type[] indexes, ParameterModifier[] modifiers); + } +} diff --git a/src/mscorlib/shared/System/Reflection/BindingFlags.cs b/src/mscorlib/shared/System/Reflection/BindingFlags.cs new file mode 100644 index 0000000000..26c875d0f9 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/BindingFlags.cs @@ -0,0 +1,50 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [Flags] + public enum BindingFlags + { + // NOTES: We have lookup masks defined in RuntimeType and Activator. If we + // change the lookup values then these masks may need to change also. + + // a place holder for no flag specifed + Default = 0x00, + + // These flags indicate what to search for when binding + IgnoreCase = 0x01, // Ignore the case of Names while searching + DeclaredOnly = 0x02, // Only look at the members declared on the Type + Instance = 0x04, // Include Instance members in search + Static = 0x08, // Include Static members in search + Public = 0x10, // Include Public members in search + NonPublic = 0x20, // Include Non-Public members in search + FlattenHierarchy = 0x40, // Rollup the statics into the class. + + // These flags are used by InvokeMember to determine + // what type of member we are trying to Invoke. + // BindingAccess = 0xFF00; + InvokeMethod = 0x0100, + CreateInstance = 0x0200, + GetField = 0x0400, + SetField = 0x0800, + GetProperty = 0x1000, + SetProperty = 0x2000, + + // These flags are also used by InvokeMember but they should only + // be used when calling InvokeMember on a COM object. + PutDispProperty = 0x4000, + PutRefDispProperty = 0x8000, + + ExactBinding = 0x010000, // Bind with Exact Type matching, No Change type + SuppressChangeType = 0x020000, + + // DefaultValueBinding will return the set of methods having ArgCount or + // more parameters. This is used for default values, etc. + OptionalParamBinding = 0x040000, + + // These are a couple of misc attributes used + IgnoreReturn = 0x01000000, // This is used in COM Interop + } +} diff --git a/src/mscorlib/shared/System/Reflection/CallingConventions.cs b/src/mscorlib/shared/System/Reflection/CallingConventions.cs new file mode 100644 index 0000000000..bb6d6cd809 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/CallingConventions.cs @@ -0,0 +1,20 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +// CallingConventions is a set of Bits representing the calling conventions in the system. + +namespace System.Reflection +{ + [Flags] + public enum CallingConventions + { + //NOTE: If you change this please update COMMember.cpp. These + // are defined there. + Standard = 0x0001, + VarArgs = 0x0002, + Any = Standard | VarArgs, + HasThis = 0x0020, + ExplicitThis = 0x0040, + } +} diff --git a/src/mscorlib/shared/System/Reflection/ConstructorInfo.cs b/src/mscorlib/shared/System/Reflection/ConstructorInfo.cs new file mode 100644 index 0000000000..3ee1dbf855 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/ConstructorInfo.cs @@ -0,0 +1,40 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Diagnostics; +using System.Globalization; + +namespace System.Reflection +{ + public abstract partial class ConstructorInfo : MethodBase + { + protected ConstructorInfo() { } + + public override MemberTypes MemberType => MemberTypes.Constructor; + + [DebuggerHidden] + [DebuggerStepThrough] + public object Invoke(object[] parameters) => Invoke(BindingFlags.Default, binder: null, parameters: parameters, culture: null); + public abstract object Invoke(BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture); + + public override bool Equals(object obj) => base.Equals(obj); + public override int GetHashCode() => base.GetHashCode(); + + public static bool operator ==(ConstructorInfo left, ConstructorInfo right) + { + if (object.ReferenceEquals(left, right)) + return true; + + if ((object)left == null || (object)right == null) + return false; + + return left.Equals(right); + } + + public static bool operator !=(ConstructorInfo left, ConstructorInfo right) => !(left == right); + + public static readonly string ConstructorName = ".ctor"; + public static readonly string TypeConstructorName = ".cctor"; + } +} diff --git a/src/mscorlib/shared/System/Reflection/CustomAttributeFormatException.cs b/src/mscorlib/shared/System/Reflection/CustomAttributeFormatException.cs new file mode 100644 index 0000000000..6e11540505 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/CustomAttributeFormatException.cs @@ -0,0 +1,33 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.Serialization; + +namespace System.Reflection +{ + [Serializable] + public class CustomAttributeFormatException : FormatException + { + public CustomAttributeFormatException() + : this(SR.Arg_CustomAttributeFormatException) + { + } + + public CustomAttributeFormatException(string message) + : this(message, null) + { + } + + public CustomAttributeFormatException(string message, Exception inner) + : base(message, inner) + { + HResult = __HResults.COR_E_CUSTOMATTRIBUTEFORMAT; + } + + protected CustomAttributeFormatException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } + } +} diff --git a/src/mscorlib/shared/System/Reflection/DefaultMemberAttribute.cs b/src/mscorlib/shared/System/Reflection/DefaultMemberAttribute.cs new file mode 100644 index 0000000000..3511433713 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/DefaultMemberAttribute.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [Serializable] + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface)] + public sealed class DefaultMemberAttribute : Attribute + { + // You must provide the name of the member, this is required + public DefaultMemberAttribute(string memberName) + { + MemberName = memberName; + } + + // A get accessor to return the name from the attribute. + // NOTE: There is no setter because the name must be provided + // to the constructor. The name is not optional. + public string MemberName { get; } + } +} diff --git a/src/mscorlib/shared/System/Reflection/EventAttributes.cs b/src/mscorlib/shared/System/Reflection/EventAttributes.cs new file mode 100644 index 0000000000..fbc2972f69 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/EventAttributes.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +// EventAttributes are an enum defining the attributes associated with and Event. +// These are defined in CorHdr.h and are a combination of bits and enums. + +namespace System.Reflection +{ + [Flags] + public enum EventAttributes + { + None = 0x0000, + + // This Enum matchs the CorEventAttr defined in CorHdr.h + SpecialName = 0x0200, // event is special. Name describes how. + + RTSpecialName = 0x0400, // Runtime(metadata internal APIs) should check name encoding. + + ReservedMask = 0x0400, + } +} diff --git a/src/mscorlib/shared/System/Reflection/EventInfo.cs b/src/mscorlib/shared/System/Reflection/EventInfo.cs new file mode 100644 index 0000000000..ccd9acf648 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/EventInfo.cs @@ -0,0 +1,115 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Diagnostics; + +#if FEATURE_COMINTEROP +using EventRegistrationToken = System.Runtime.InteropServices.WindowsRuntime.EventRegistrationToken; +#endif //#if FEATURE_COMINTEROP + +namespace System.Reflection +{ + public abstract class EventInfo : MemberInfo + { + protected EventInfo() { } + + public override MemberTypes MemberType => MemberTypes.Event; + + public abstract EventAttributes Attributes { get; } + public bool IsSpecialName => (Attributes & EventAttributes.SpecialName) != 0; + + public MethodInfo[] GetOtherMethods() => GetOtherMethods(nonPublic: false); + public virtual MethodInfo[] GetOtherMethods(bool nonPublic) { throw NotImplemented.ByDesign; } + + public virtual MethodInfo AddMethod => GetAddMethod(nonPublic: true); + public virtual MethodInfo RemoveMethod => GetRemoveMethod(nonPublic: true); + public virtual MethodInfo RaiseMethod => GetRaiseMethod(nonPublic: true); + + public MethodInfo GetAddMethod() => GetAddMethod(nonPublic: false); + public MethodInfo GetRemoveMethod() => GetRemoveMethod(nonPublic: false); + public MethodInfo GetRaiseMethod() => GetRaiseMethod(nonPublic: false); + + public abstract MethodInfo GetAddMethod(bool nonPublic); + public abstract MethodInfo GetRemoveMethod(bool nonPublic); + public abstract MethodInfo GetRaiseMethod(bool nonPublic); + + public virtual bool IsMulticast + { + get + { + Type cl = EventHandlerType; + Type mc = typeof(MulticastDelegate); + return mc.IsAssignableFrom(cl); + } + } + + public virtual Type EventHandlerType + { + get + { + MethodInfo m = GetAddMethod(true); + ParameterInfo[] p = m.GetParametersNoCopy(); + Type del = typeof(Delegate); + for (int i = 0; i < p.Length; i++) + { + Type c = p[i].ParameterType; + if (c.IsSubclassOf(del)) + return c; + } + return null; + } + } + + [DebuggerHidden] + [DebuggerStepThrough] + public virtual void AddEventHandler(object target, Delegate handler) + { + MethodInfo addMethod = GetAddMethod(nonPublic: false); + + if (addMethod == null) + throw new InvalidOperationException(SR.InvalidOperation_NoPublicAddMethod); + +#if FEATURE_COMINTEROP + if (addMethod.ReturnType == typeof(EventRegistrationToken)) + throw new InvalidOperationException(SR.InvalidOperation_NotSupportedOnWinRTEvent); +#endif //#if FEATURE_COMINTEROP + + addMethod.Invoke(target, new object[] { handler }); + } + + [DebuggerHidden] + [DebuggerStepThrough] + public virtual void RemoveEventHandler(object target, Delegate handler) + { + MethodInfo removeMethod = GetRemoveMethod(nonPublic: false); + + if (removeMethod == null) + throw new InvalidOperationException(SR.InvalidOperation_NoPublicRemoveMethod); + +#if FEATURE_COMINTEROP + ParameterInfo[] parameters = removeMethod.GetParametersNoCopy(); + if (parameters[0].ParameterType == typeof(EventRegistrationToken)) + throw new InvalidOperationException(SR.InvalidOperation_NotSupportedOnWinRTEvent); +#endif //#if FEATURE_COMINTEROP + + removeMethod.Invoke(target, new object[] { handler }); + } + + public override bool Equals(object obj) => base.Equals(obj); + public override int GetHashCode() => base.GetHashCode(); + + public static bool operator ==(EventInfo left, EventInfo right) + { + if (object.ReferenceEquals(left, right)) + return true; + + if ((object)left == null || (object)right == null) + return false; + + return left.Equals(right); + } + + public static bool operator !=(EventInfo left, EventInfo right) => !(left == right); + } +} diff --git a/src/mscorlib/shared/System/Reflection/ExceptionHandlingClauseOptions.cs b/src/mscorlib/shared/System/Reflection/ExceptionHandlingClauseOptions.cs new file mode 100644 index 0000000000..46285f7c82 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/ExceptionHandlingClauseOptions.cs @@ -0,0 +1,16 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [Flags] + public enum ExceptionHandlingClauseOptions : int + { + Clause = 0x0, + Filter = 0x1, + Finally = 0x2, + Fault = 0x4, + } +} + diff --git a/src/mscorlib/shared/System/Reflection/FieldAttributes.cs b/src/mscorlib/shared/System/Reflection/FieldAttributes.cs new file mode 100644 index 0000000000..048d0e7031 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/FieldAttributes.cs @@ -0,0 +1,40 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + // This Enum matchs the CorFieldAttr defined in CorHdr.h + [Flags] + public enum FieldAttributes + { + // member access mask - Use this mask to retrieve accessibility information. + FieldAccessMask = 0x0007, + PrivateScope = 0x0000, // Member not referenceable. + Private = 0x0001, // Accessible only by the parent type. + FamANDAssem = 0x0002, // Accessible by sub-types only in this Assembly. + Assembly = 0x0003, // Accessibly by anyone in the Assembly. + Family = 0x0004, // Accessible only by type and sub-types. + FamORAssem = 0x0005, // Accessibly by sub-types anywhere, plus anyone in assembly. + Public = 0x0006, // Accessibly by anyone who has visibility to this scope. + // end member access mask + + // field contract attributes. + Static = 0x0010, // Defined on type, else per instance. + InitOnly = 0x0020, // Field may only be initialized, not written to after init. + Literal = 0x0040, // Value is compile time constant. + NotSerialized = 0x0080, // Field does not have to be serialized when type is remoted. + + SpecialName = 0x0200, // field is special. Name describes how. + + // interop attributes + PinvokeImpl = 0x2000, // Implementation is forwarded through pinvoke. + + RTSpecialName = 0x0400, // Runtime(metadata internal APIs) should check name encoding. + HasFieldMarshal = 0x1000, // Field has marshalling information. + HasDefault = 0x8000, // Field has default. + HasFieldRVA = 0x0100, // Field has RVA. + + ReservedMask = 0x9500, + } +} diff --git a/src/mscorlib/shared/System/Reflection/FieldInfo.cs b/src/mscorlib/shared/System/Reflection/FieldInfo.cs new file mode 100644 index 0000000000..0863c2b019 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/FieldInfo.cs @@ -0,0 +1,72 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Diagnostics; +using System.Globalization; + +namespace System.Reflection +{ + public abstract partial class FieldInfo : MemberInfo + { + protected FieldInfo() { } + + public override MemberTypes MemberType => MemberTypes.Field; + + public abstract FieldAttributes Attributes { get; } + public abstract Type FieldType { get; } + + public bool IsInitOnly => (Attributes & FieldAttributes.InitOnly) != 0; + public bool IsLiteral => (Attributes & FieldAttributes.Literal) != 0; + public bool IsNotSerialized => (Attributes & FieldAttributes.NotSerialized) != 0; + public bool IsPinvokeImpl => (Attributes & FieldAttributes.PinvokeImpl) != 0; + public bool IsSpecialName => (Attributes & FieldAttributes.SpecialName) != 0; + public bool IsStatic => (Attributes & FieldAttributes.Static) != 0; + + public bool IsAssembly => (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Assembly; + public bool IsFamily => (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Family; + public bool IsFamilyAndAssembly => (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamANDAssem; + public bool IsFamilyOrAssembly => (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamORAssem; + public bool IsPrivate => (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Private; + public bool IsPublic => (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public; + + public virtual bool IsSecurityCritical => true; + public virtual bool IsSecuritySafeCritical => false; + public virtual bool IsSecurityTransparent => false; + + public abstract RuntimeFieldHandle FieldHandle { get; } + + public override bool Equals(object obj) => base.Equals(obj); + public override int GetHashCode() => base.GetHashCode(); + + public static bool operator ==(FieldInfo left, FieldInfo right) + { + if (object.ReferenceEquals(left, right)) + return true; + + if ((object)left == null || (object)right == null) + return false; + + return left.Equals(right); + } + + public static bool operator !=(FieldInfo left, FieldInfo right) => !(left == right); + + public abstract object GetValue(object obj); + + [DebuggerHidden] + [DebuggerStepThrough] + public void SetValue(object obj, object value) => SetValue(obj, value, BindingFlags.Default, Type.DefaultBinder, null); + public abstract void SetValue(object obj, object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture); + + [CLSCompliant(false)] + public virtual void SetValueDirect(TypedReference obj, object value) { throw new NotSupportedException(SR.NotSupported_AbstractNonCLS); } + [CLSCompliant(false)] + public virtual object GetValueDirect(TypedReference obj) { throw new NotSupportedException(SR.NotSupported_AbstractNonCLS); } + + public virtual object GetRawConstantValue() { throw new NotSupportedException(SR.NotSupported_AbstractNonCLS); } + + public virtual Type[] GetOptionalCustomModifiers() { throw NotImplemented.ByDesign; } + public virtual Type[] GetRequiredCustomModifiers() { throw NotImplemented.ByDesign; } + } +} diff --git a/src/mscorlib/shared/System/Reflection/GenericParameterAttributes.cs b/src/mscorlib/shared/System/Reflection/GenericParameterAttributes.cs new file mode 100644 index 0000000000..4b579d273e --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/GenericParameterAttributes.cs @@ -0,0 +1,20 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [Flags] + public enum GenericParameterAttributes + { + None = 0x0000, + VarianceMask = 0x0003, + Covariant = 0x0001, + Contravariant = 0x0002, + SpecialConstraintMask = 0x001C, + ReferenceTypeConstraint = 0x0004, + NotNullableValueTypeConstraint = 0x0008, + DefaultConstructorConstraint = 0x0010, + } +} + diff --git a/src/mscorlib/shared/System/Reflection/ICustomAttributeProvider.cs b/src/mscorlib/shared/System/Reflection/ICustomAttributeProvider.cs new file mode 100644 index 0000000000..3cae295bc4 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/ICustomAttributeProvider.cs @@ -0,0 +1,13 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + public interface ICustomAttributeProvider + { + object[] GetCustomAttributes(bool inherit); + object[] GetCustomAttributes(Type attributeType, bool inherit); + bool IsDefined(Type attributeType, bool inherit); + } +}
\ No newline at end of file diff --git a/src/mscorlib/shared/System/Reflection/IReflect.cs b/src/mscorlib/shared/System/Reflection/IReflect.cs new file mode 100644 index 0000000000..51141ae47c --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/IReflect.cs @@ -0,0 +1,76 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Globalization; + +namespace System.Reflection +{ + public interface IReflect + { + // Return the requested method if it is implemented by the Reflection object. The + // match is based upon the name and DescriptorInfo which describes the signature + // of the method. + MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers); + + // Return the requested method if it is implemented by the Reflection object. The + // match is based upon the name of the method. If the object implementes multiple methods + // with the same name an AmbiguousMatchException is thrown. + MethodInfo GetMethod(string name, BindingFlags bindingAttr); + + MethodInfo[] GetMethods(BindingFlags bindingAttr); + + // Return the requestion field if it is implemented by the Reflection object. The + // match is based upon a name. There cannot be more than a single field with + // a name. + FieldInfo GetField(string name, BindingFlags bindingAttr); + + FieldInfo[] GetFields(BindingFlags bindingAttr); + + // Return the property based upon name. If more than one property has the given + // name an AmbiguousMatchException will be thrown. Returns null if no property + // is found. + PropertyInfo GetProperty(string name, BindingFlags bindingAttr); + + // Return the property based upon the name and Descriptor info describing the property + // indexing. Return null if no property is found. + PropertyInfo GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers); + + // Returns an array of PropertyInfos for all the properties defined on + // the Reflection object. + PropertyInfo[] GetProperties(BindingFlags bindingAttr); + + // Return an array of members which match the passed in name. + MemberInfo[] GetMember(string name, BindingFlags bindingAttr); + + // Return an array of all of the members defined for this object. + MemberInfo[] GetMembers(BindingFlags bindingAttr); + + // Description of the Binding Process. + // We must invoke a method that is accessable and for which the provided + // parameters have the most specific match. A method may be called if + // 1. The number of parameters in the method declaration equals the number of + // arguments provided to the invocation + // 2. The type of each argument can be converted by the binder to the + // type of the type of the parameter. + // + // The binder will find all of the matching methods. These method are found based + // upon the type of binding requested (MethodInvoke, Get/Set Properties). The set + // of methods is filtered by the name, number of arguments and a set of search modifiers + // defined in the Binder. + // + // After the method is selected, it will be invoked. Accessability is checked + // at that point. The search may be control which set of methods are searched based + // upon the accessibility attribute associated with the method. + // + // The BindToMethod method is responsible for selecting the method to be invoked. + // For the default binder, the most specific method will be selected. + // + // This will invoke a specific member... + object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters); + + // Return the underlying Type that represents the IReflect Object. For expando object, + // this is the (Object) IReflectInstance.GetType(). For Type object it is this. + Type UnderlyingSystemType { get; } + } +} diff --git a/src/mscorlib/shared/System/Reflection/IReflectableType.cs b/src/mscorlib/shared/System/Reflection/IReflectableType.cs new file mode 100644 index 0000000000..5e2c0edab4 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/IReflectableType.cs @@ -0,0 +1,12 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + public interface IReflectableType + { + TypeInfo GetTypeInfo(); + } +} + diff --git a/src/mscorlib/shared/System/Reflection/ImageFileMachine.cs b/src/mscorlib/shared/System/Reflection/ImageFileMachine.cs new file mode 100644 index 0000000000..230bc952e5 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/ImageFileMachine.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + public enum ImageFileMachine + { + I386 = 0x014c, + IA64 = 0x0200, + AMD64 = 0x8664, + ARM = 0x01c4, + } +} + diff --git a/src/mscorlib/shared/System/Reflection/InterfaceMapping.cs b/src/mscorlib/shared/System/Reflection/InterfaceMapping.cs new file mode 100644 index 0000000000..2e0c0d8a28 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/InterfaceMapping.cs @@ -0,0 +1,14 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + public struct InterfaceMapping + { + public Type TargetType; // The type implementing the interface + public Type InterfaceType; // The type representing the interface + public MethodInfo[] TargetMethods; // The methods implementing the interface + public MethodInfo[] InterfaceMethods; // The methods defined on the interface + } +} diff --git a/src/mscorlib/shared/System/Reflection/IntrospectionExtensions.cs b/src/mscorlib/shared/System/Reflection/IntrospectionExtensions.cs new file mode 100644 index 0000000000..6a18fdaa72 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/IntrospectionExtensions.cs @@ -0,0 +1,20 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Diagnostics; + +namespace System.Reflection +{ + public static class IntrospectionExtensions + { + public static TypeInfo GetTypeInfo(this Type type) + { + if (type == null) + throw new ArgumentNullException(nameof(type)); + + return ((IReflectableType)type).GetTypeInfo(); // Unguarded cast is unbecoming but kept for compatibility. + } + } +} + diff --git a/src/mscorlib/shared/System/Reflection/InvalidFilterCriteriaException.cs b/src/mscorlib/shared/System/Reflection/InvalidFilterCriteriaException.cs new file mode 100644 index 0000000000..e3f882c409 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/InvalidFilterCriteriaException.cs @@ -0,0 +1,33 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.Serialization; + +namespace System.Reflection +{ + [Serializable] + public class InvalidFilterCriteriaException : ApplicationException + { + public InvalidFilterCriteriaException() + : this(SR.Arg_InvalidFilterCriteriaException) + { + } + + public InvalidFilterCriteriaException(string message) + : this(message, null) + { + } + + public InvalidFilterCriteriaException(string message, Exception inner) + : base(message, inner) + { + HResult = __HResults.COR_E_INVALIDFILTERCRITERIA; + } + + protected InvalidFilterCriteriaException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } + } +} diff --git a/src/mscorlib/shared/System/Reflection/ManifestResourceInfo.cs b/src/mscorlib/shared/System/Reflection/ManifestResourceInfo.cs new file mode 100644 index 0000000000..b9c56ab857 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/ManifestResourceInfo.cs @@ -0,0 +1,23 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + public class ManifestResourceInfo + { + public ManifestResourceInfo(Assembly containingAssembly, + string containingFileName, + ResourceLocation resourceLocation) + { + ReferencedAssembly = containingAssembly; + FileName = containingFileName; + ResourceLocation = resourceLocation; + } + + public virtual Assembly ReferencedAssembly { get; } + public virtual string FileName { get; } + public virtual ResourceLocation ResourceLocation { get; } + } +} + diff --git a/src/mscorlib/shared/System/Reflection/MemberFilter.cs b/src/mscorlib/shared/System/Reflection/MemberFilter.cs new file mode 100644 index 0000000000..bb1b15796a --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/MemberFilter.cs @@ -0,0 +1,8 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + public delegate bool MemberFilter(MemberInfo m, object filterCriteria); +}
\ No newline at end of file diff --git a/src/mscorlib/shared/System/Reflection/MemberInfo.cs b/src/mscorlib/shared/System/Reflection/MemberInfo.cs new file mode 100644 index 0000000000..1275cc15a0 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/MemberInfo.cs @@ -0,0 +1,75 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; + +namespace System.Reflection +{ + public abstract partial class MemberInfo : ICustomAttributeProvider + { + protected MemberInfo() { } + + public abstract MemberTypes MemberType { get; } + public abstract string Name { get; } + public abstract Type DeclaringType { get; } + public abstract Type ReflectedType { get; } + + public virtual Module Module + { + get + { + // This check is necessary because for some reason, Type adds a new "Module" property that hides the inherited one instead + // of overriding. + + Type type = this as Type; + if (type != null) + return type.Module; + + throw NotImplemented.ByDesign; + } + } + + public abstract bool IsDefined(Type attributeType, bool inherit); + public abstract object[] GetCustomAttributes(bool inherit); + public abstract object[] GetCustomAttributes(Type attributeType, bool inherit); + + public virtual IEnumerable<CustomAttributeData> CustomAttributes => GetCustomAttributesData(); + public virtual IList<CustomAttributeData> GetCustomAttributesData() { throw NotImplemented.ByDesign; } + + public virtual int MetadataToken { get { throw new InvalidOperationException(); } } + + public override bool Equals(object obj) => base.Equals(obj); + public override int GetHashCode() => base.GetHashCode(); + + public static bool operator ==(MemberInfo left, MemberInfo right) + { + if (object.ReferenceEquals(left, right)) + return true; + + if ((object)left == null || (object)right == null) + return false; + + Type type1, type2; + MethodBase method1, method2; + FieldInfo field1, field2; + EventInfo event1, event2; + PropertyInfo property1, property2; + + if ((type1 = left as Type) != null && (type2 = right as Type) != null) + return type1 == type2; + else if ((method1 = left as MethodBase) != null && (method2 = right as MethodBase) != null) + return method1 == method2; + else if ((field1 = left as FieldInfo) != null && (field2 = right as FieldInfo) != null) + return field1 == field2; + else if ((event1 = left as EventInfo) != null && (event2 = right as EventInfo) != null) + return event1 == event2; + else if ((property1 = left as PropertyInfo) != null && (property2 = right as PropertyInfo) != null) + return property1 == property2; + + return false; + } + + public static bool operator !=(MemberInfo left, MemberInfo right) => !(left == right); + } +} diff --git a/src/mscorlib/shared/System/Reflection/MemberInfoSerializationHolder.cs b/src/mscorlib/shared/System/Reflection/MemberInfoSerializationHolder.cs new file mode 100644 index 0000000000..dfc56667bd --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/MemberInfoSerializationHolder.cs @@ -0,0 +1,315 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.Serialization; +using System.Globalization; +using System.Diagnostics.Contracts; + +namespace System.Reflection +{ + [Serializable] +#if CORECLR + internal +#else + public // On CoreRT, this must be public because of the Reflection.Core/CoreLib divide and the need to whitelist past the ReflectionBlock. +#endif + class MemberInfoSerializationHolder : ISerializable, IObjectReference + { + #region Staitc Public Members + public static void GetSerializationInfo(SerializationInfo info, FieldInfo f) + { + // Compat: Serializing ToString() since the full framework does it but the deserialization logic makes no use of it. + GetSerializationInfo(info, f.Name, f.ReflectedType, f.ToString(), MemberTypes.Field); + } + + public static void GetSerializationInfo(SerializationInfo info, EventInfo e) + { + GetSerializationInfo(info, e.Name, e.ReflectedType, null, MemberTypes.Event); + } + + public static void GetSerializationInfo(SerializationInfo info, ConstructorInfo c) + { + GetSerializationInfo(info, c.Name, c.ReflectedType, c.ToString(), c.SerializationToString(), MemberTypes.Constructor, genericArguments: null); + } + + public static void GetSerializationInfo(SerializationInfo info, MethodInfo m) + { + Type[] genericArguments = m.IsConstructedGenericMethod ? m.GetGenericArguments() : null; + GetSerializationInfo(info, m.Name, m.ReflectedType, m.ToString(), m.SerializationToString(), MemberTypes.Method, genericArguments); + } + + public static void GetSerializationInfo(SerializationInfo info, PropertyInfo p) + { + GetSerializationInfo(info, p.Name, p.ReflectedType, p.ToString(), p.SerializationToString(), MemberTypes.Property, genericArguments: null); + } + #endregion + + #region Private Static Members + private static void GetSerializationInfo(SerializationInfo info, string name, Type reflectedClass, string signature, MemberTypes type) + { + GetSerializationInfo(info, name, reflectedClass, signature, null, type, null); + } + + private static void GetSerializationInfo( + SerializationInfo info, + string name, + Type reflectedClass, + string signature, + string signature2, + MemberTypes type, + Type[] genericArguments) + { + if (info == null) + throw new ArgumentNullException(nameof(info)); + Contract.EndContractBlock(); + + string assemblyName = reflectedClass.Module.Assembly.FullName; + string typeName = reflectedClass.FullName; + + info.SetType(typeof(MemberInfoSerializationHolder)); + info.AddValue("Name", name, typeof(string)); + info.AddValue("AssemblyName", assemblyName, typeof(string)); + info.AddValue("ClassName", typeName, typeof(string)); + info.AddValue("Signature", signature, typeof(string)); + info.AddValue("Signature2", signature2, typeof(string)); + info.AddValue("MemberType", (int)type); + info.AddValue("GenericArguments", genericArguments, typeof(Type[])); + } + #endregion + + #region Private Data Members + private readonly string _memberName; + private readonly Type _reflectedType; + // _signature stores the ToString() representation of the member which is sometimes ambiguous. + // Mulitple overloads of the same methods or properties can identical ToString(). + // _signature2 stores the SerializationToString() representation which should be unique for each member. + // It is only written and used by post 4.0 CLR versions. + private readonly string _signature; + private readonly string _signature2; + private readonly MemberTypes _memberType; + private readonly SerializationInfo _info; + #endregion + + #region Constructor + // Needs to be public so it can be whitelisted in Reflection. + public MemberInfoSerializationHolder(SerializationInfo info, StreamingContext context) + { + if (info == null) + throw new ArgumentNullException(nameof(info)); + Contract.EndContractBlock(); + + string assemblyName = info.GetString("AssemblyName"); + string typeName = info.GetString("ClassName"); + + if (assemblyName == null || typeName == null) + throw new SerializationException(SR.Serialization_InsufficientState); + + Assembly assem = Assembly.Load(assemblyName); + _reflectedType = assem.GetType(typeName, true, false); + _memberName = info.GetString("Name"); + _signature = info.GetString("Signature"); + // Only v4.0 and later generates and consumes Signature2 + _signature2 = (string)info.GetValueNoThrow("Signature2", typeof(string)); + _memberType = (MemberTypes)info.GetInt32("MemberType"); + _info = info; + } + #endregion + + #region ISerializable + public virtual void GetObjectData(SerializationInfo info, StreamingContext context) + { + throw new NotSupportedException(); + } + #endregion + + #region IObjectReference + public virtual object GetRealObject(StreamingContext context) + { + if (_memberName == null || _reflectedType == null || _memberType == 0) + throw new SerializationException(SR.Serialization_InsufficientState); + + BindingFlags bindingFlags = + BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | + BindingFlags.Static | BindingFlags.OptionalParamBinding; + + switch (_memberType) + { + #region case MemberTypes.Field: + case MemberTypes.Field: + { + FieldInfo[] fields = _reflectedType.GetMember(_memberName, MemberTypes.Field, bindingFlags) as FieldInfo[]; + + if (fields.Length == 0) + throw new SerializationException(SR.Format(SR.Serialization_UnknownMember, _memberName)); + + return fields[0]; + } + #endregion + + #region case MemberTypes.Event: + case MemberTypes.Event: + { + EventInfo[] events = _reflectedType.GetMember(_memberName, MemberTypes.Event, bindingFlags) as EventInfo[]; + + if (events.Length == 0) + throw new SerializationException(SR.Format(SR.Serialization_UnknownMember, _memberName)); + + return events[0]; + } + #endregion + + #region case MemberTypes.Property: + case MemberTypes.Property: + { + PropertyInfo[] properties = _reflectedType.GetMember(_memberName, MemberTypes.Property, bindingFlags) as PropertyInfo[]; + + if (properties.Length == 0) + throw new SerializationException(SR.Format(SR.Serialization_UnknownMember, _memberName)); + + if (properties.Length == 1) + return properties[0]; + + if (properties.Length > 1) + { + for (int i = 0; i < properties.Length; i++) + { + if (_signature2 != null) + { + if (properties[i].SerializationToString().Equals(_signature2)) + return properties[i]; + } + else + { + if ((properties[i]).ToString().Equals(_signature)) + return properties[i]; + } + } + } + + throw new SerializationException(SR.Format(SR.Serialization_UnknownMember, _memberName)); + } + #endregion + + #region case MemberTypes.Constructor: + case MemberTypes.Constructor: + { + if (_signature == null) + throw new SerializationException(SR.Serialization_NullSignature); + + ConstructorInfo[] constructors = _reflectedType.GetMember(_memberName, MemberTypes.Constructor, bindingFlags) as ConstructorInfo[]; + + if (constructors.Length == 1) + return constructors[0]; + + if (constructors.Length > 1) + { + for (int i = 0; i < constructors.Length; i++) + { + if (_signature2 != null) + { + if (constructors[i].SerializationToString().Equals(_signature2)) + return constructors[i]; + } + else + { + if (constructors[i].ToString().Equals(_signature)) + return constructors[i]; + } + } + } + + throw new SerializationException(SR.Format(SR.Serialization_UnknownMember, _memberName)); + } + #endregion + + #region case MemberTypes.Method: + case MemberTypes.Method: + { + MethodInfo methodInfo = null; + + if (_signature == null) + throw new SerializationException(SR.Serialization_NullSignature); + + Type[] genericArguments = _info.GetValueNoThrow("GenericArguments", typeof(Type[])) as Type[]; + + MethodInfo[] methods = _reflectedType.GetMember(_memberName, MemberTypes.Method, bindingFlags) as MethodInfo[]; + + if (methods.Length == 1) + methodInfo = methods[0]; + + else if (methods.Length > 1) + { + for (int i = 0; i < methods.Length; i++) + { + if (_signature2 != null) + { + if (methods[i].SerializationToString().Equals(_signature2)) + { + methodInfo = methods[i]; + break; + } + } + else + { + if (methods[i].ToString().Equals(_signature)) + { + methodInfo = methods[i]; + break; + } + } + + // Handle generic methods specially since the signature match above probably won't work (the candidate + // method info hasn't been instantiated). If our target method is generic as well we can skip this. + if (genericArguments != null && methods[i].IsGenericMethod) + { + if (methods[i].GetGenericArguments().Length == genericArguments.Length) + { + MethodInfo candidateMethod = methods[i].MakeGenericMethod(genericArguments); + + if (_signature2 != null) + { + if (candidateMethod.SerializationToString().Equals(_signature2)) + { + methodInfo = candidateMethod; + break; + } + } + else + { + if (candidateMethod.ToString().Equals(_signature)) + { + methodInfo = candidateMethod; + break; + } + } + } + } + } + } + + if (methodInfo == null) + throw new SerializationException(SR.Format(SR.Serialization_UnknownMember, _memberName)); + + if (!methodInfo.IsGenericMethodDefinition) + return methodInfo; + + if (genericArguments == null) + return methodInfo; + + if (genericArguments[0] == null) + return null; + + return methodInfo.MakeGenericMethod(genericArguments); + } + #endregion + + default: + throw new ArgumentException(SR.Serialization_MemberTypeNotRecognized); + } + } + #endregion + } +} + diff --git a/src/mscorlib/shared/System/Reflection/MemberTypes.cs b/src/mscorlib/shared/System/Reflection/MemberTypes.cs new file mode 100644 index 0000000000..57072dcfbe --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/MemberTypes.cs @@ -0,0 +1,20 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [Flags] + public enum MemberTypes + { + Constructor = 0x01, + Event = 0x02, + Field = 0x04, + Method = 0x08, + Property = 0x10, + TypeInfo = 0x20, + Custom = 0x40, + NestedType = 0x80, + All = Constructor | Event | Field | Method | Property | TypeInfo | NestedType, + } +}
\ No newline at end of file diff --git a/src/mscorlib/shared/System/Reflection/MethodAttributes.cs b/src/mscorlib/shared/System/Reflection/MethodAttributes.cs new file mode 100644 index 0000000000..1a7c7bf154 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/MethodAttributes.cs @@ -0,0 +1,50 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [Flags] + public enum MethodAttributes + { + // NOTE: This Enum matchs the CorMethodAttr defined in CorHdr.h + + // member access mask - Use this mask to retrieve accessibility information. + MemberAccessMask = 0x0007, + PrivateScope = 0x0000, // Member not referenceable. + Private = 0x0001, // Accessible only by the parent type. + FamANDAssem = 0x0002, // Accessible by sub-types only in this Assembly. + Assembly = 0x0003, // Accessibly by anyone in the Assembly. + Family = 0x0004, // Accessible only by type and sub-types. + FamORAssem = 0x0005, // Accessibly by sub-types anywhere, plus anyone in assembly. + Public = 0x0006, // Accessibly by anyone who has visibility to this scope. + // end member access mask + + // method contract attributes. + Static = 0x0010, // Defined on type, else per instance. + Final = 0x0020, // Method may not be overridden. + Virtual = 0x0040, // Method virtual. + HideBySig = 0x0080, // Method hides by name+sig, else just by name. + CheckAccessOnOverride = 0x0200, + + // vtable layout mask - Use this mask to retrieve vtable attributes. + VtableLayoutMask = 0x0100, + ReuseSlot = 0x0000, // The default. + NewSlot = 0x0100, // Method always gets a new slot in the vtable. + // end vtable layout mask + + // method implementation attributes. + Abstract = 0x0400, // Method does not provide an implementation. + SpecialName = 0x0800, // Method is special. Name describes how. + + // interop attributes + PinvokeImpl = 0x2000, // Implementation is forwarded through pinvoke. + UnmanagedExport = 0x0008, // Managed method exported via thunk to unmanaged code. + RTSpecialName = 0x1000, // Runtime should check name encoding. + + HasSecurity = 0x4000, // Method has security associate with it. + RequireSecObject = 0x8000, // Method calls another method containing security code. + + ReservedMask = 0xd000, + } +} diff --git a/src/mscorlib/shared/System/Reflection/MethodBase.cs b/src/mscorlib/shared/System/Reflection/MethodBase.cs new file mode 100644 index 0000000000..0037c74f4c --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/MethodBase.cs @@ -0,0 +1,86 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Globalization; +using System.Diagnostics; + +namespace System.Reflection +{ + public abstract partial class MethodBase : MemberInfo + { + protected MethodBase() { } + + public abstract ParameterInfo[] GetParameters(); + public abstract MethodAttributes Attributes { get; } + public virtual MethodImplAttributes MethodImplementationFlags => GetMethodImplementationFlags(); + public abstract MethodImplAttributes GetMethodImplementationFlags(); + public virtual MethodBody GetMethodBody() { throw new InvalidOperationException(); } + public virtual CallingConventions CallingConvention => CallingConventions.Standard; + + public bool IsAbstract => (Attributes & MethodAttributes.Abstract) != 0; + public bool IsConstructor + { + get + { + // To be backward compatible we only return true for instance RTSpecialName ctors. + return (this is ConstructorInfo && + !IsStatic && + ((Attributes & MethodAttributes.RTSpecialName) == MethodAttributes.RTSpecialName)); + } + } + public bool IsFinal => (Attributes & MethodAttributes.Final) != 0; + public bool IsHideBySig => (Attributes & MethodAttributes.HideBySig) != 0; + public bool IsSpecialName => (Attributes & MethodAttributes.SpecialName) != 0; + public bool IsStatic => (Attributes & MethodAttributes.Static) != 0; + public bool IsVirtual => (Attributes & MethodAttributes.Virtual) != 0; + + public bool IsAssembly => (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Assembly; + public bool IsFamily => (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Family; + public bool IsFamilyAndAssembly => (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamANDAssem; + public bool IsFamilyOrAssembly => (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamORAssem; + public bool IsPrivate => (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Private; + public bool IsPublic => (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Public; + + public virtual bool IsConstructedGenericMethod => IsGenericMethod && !IsGenericMethodDefinition; + public virtual bool IsGenericMethod => false; + public virtual bool IsGenericMethodDefinition => false; + public virtual Type[] GetGenericArguments() { throw new NotSupportedException(SR.NotSupported_SubclassOverride); } + public virtual bool ContainsGenericParameters => false; + + [DebuggerHidden] + [DebuggerStepThrough] + public object Invoke(object obj, object[] parameters) => Invoke(obj, BindingFlags.Default, binder: null, parameters: parameters, culture: null); + public abstract object Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture); + + public abstract RuntimeMethodHandle MethodHandle { get; } + + public virtual bool IsSecurityCritical { get { throw NotImplemented.ByDesign; } } + public virtual bool IsSecuritySafeCritical { get { throw NotImplemented.ByDesign; } } + public virtual bool IsSecurityTransparent { get { throw NotImplemented.ByDesign; } } + + public override bool Equals(object obj) => base.Equals(obj); + public override int GetHashCode() => base.GetHashCode(); + + public static bool operator ==(MethodBase left, MethodBase right) + { + if (object.ReferenceEquals(left, right)) + return true; + + if ((object)left == null || (object)right == null) + return false; + + MethodInfo method1, method2; + ConstructorInfo constructor1, constructor2; + + if ((method1 = left as MethodInfo) != null && (method2 = right as MethodInfo) != null) + return method1 == method2; + else if ((constructor1 = left as ConstructorInfo) != null && (constructor2 = right as ConstructorInfo) != null) + return constructor1 == constructor2; + + return false; + } + + public static bool operator !=(MethodBase left, MethodBase right) => !(left == right); + } +} diff --git a/src/mscorlib/shared/System/Reflection/MethodImplAttributes.cs b/src/mscorlib/shared/System/Reflection/MethodImplAttributes.cs new file mode 100644 index 0000000000..a1ed326002 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/MethodImplAttributes.cs @@ -0,0 +1,37 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + // This Enum matchs the CorMethodImpl defined in CorHdr.h + public enum MethodImplAttributes + { + // code impl mask + CodeTypeMask = 0x0003, // Flags about code type. + IL = 0x0000, // Method impl is IL. + Native = 0x0001, // Method impl is native. + OPTIL = 0x0002, // Method impl is OPTIL + Runtime = 0x0003, // Method impl is provided by the runtime. + // end code impl mask + + // managed mask + ManagedMask = 0x0004, // Flags specifying whether the code is managed or unmanaged. + Unmanaged = 0x0004, // Method impl is unmanaged, otherwise managed. + Managed = 0x0000, // Method impl is managed. + // end managed mask + + // implementation info and interop + ForwardRef = 0x0010, // Indicates method is not defined; used primarily in merge scenarios. + PreserveSig = 0x0080, // Indicates method sig is exported exactly as declared. + + InternalCall = 0x1000, // Internal Call... + + Synchronized = 0x0020, // Method is single threaded through the body. + NoInlining = 0x0008, // Method may not be inlined. + AggressiveInlining = 0x0100, // Method should be inlined if possible. + NoOptimization = 0x0040, // Method may not be optimized. + + MaxMethodImplVal = 0xffff, + } +} diff --git a/src/mscorlib/shared/System/Reflection/MethodInfo.cs b/src/mscorlib/shared/System/Reflection/MethodInfo.cs new file mode 100644 index 0000000000..3f60149e87 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/MethodInfo.cs @@ -0,0 +1,43 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + public abstract class MethodInfo : MethodBase + { + protected MethodInfo() { } + + public override MemberTypes MemberType => MemberTypes.Method; + + public virtual ParameterInfo ReturnParameter { get { throw NotImplemented.ByDesign; } } + public virtual Type ReturnType { get { throw NotImplemented.ByDesign; } } + + public override Type[] GetGenericArguments() { throw new NotSupportedException(SR.NotSupported_SubclassOverride); } + public virtual MethodInfo GetGenericMethodDefinition() { throw new NotSupportedException(SR.NotSupported_SubclassOverride); } + public virtual MethodInfo MakeGenericMethod(params Type[] typeArguments) { throw new NotSupportedException(SR.NotSupported_SubclassOverride); } + + public abstract MethodInfo GetBaseDefinition(); + + public abstract ICustomAttributeProvider ReturnTypeCustomAttributes { get; } + + public virtual Delegate CreateDelegate(Type delegateType) { throw new NotSupportedException(SR.NotSupported_SubclassOverride); } + public virtual Delegate CreateDelegate(Type delegateType, object target) { throw new NotSupportedException(SR.NotSupported_SubclassOverride); } + + public override bool Equals(object obj) => base.Equals(obj); + public override int GetHashCode() => base.GetHashCode(); + + public static bool operator ==(MethodInfo left, MethodInfo right) + { + if (object.ReferenceEquals(left, right)) + return true; + + if ((object)left == null || (object)right == null) + return false; + + return left.Equals(right); + } + + public static bool operator !=(MethodInfo left, MethodInfo right) => !(left == right); + } +} diff --git a/src/mscorlib/shared/System/Reflection/Missing.cs b/src/mscorlib/shared/System/Reflection/Missing.cs new file mode 100644 index 0000000000..fa32d43ccb --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/Missing.cs @@ -0,0 +1,24 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.Serialization; + +namespace System.Reflection +{ + [Serializable] + public sealed class Missing : ISerializable + { + public static readonly Missing Value = new Missing(); + + private Missing() { } + + void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) + { + if (info == null) + throw new ArgumentNullException(nameof(info)); + + UnitySerializationHolder.GetUnitySerializationInfo(info, this); + } + } +} diff --git a/src/mscorlib/shared/System/Reflection/Module.cs b/src/mscorlib/shared/System/Reflection/Module.cs new file mode 100644 index 0000000000..56f83c40d9 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/Module.cs @@ -0,0 +1,182 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using System.Runtime.Serialization; + +namespace System.Reflection +{ + public abstract class Module : ICustomAttributeProvider, ISerializable + { + protected Module() { } + + public virtual Assembly Assembly { get { throw NotImplemented.ByDesign; } } + public virtual string FullyQualifiedName { get { throw NotImplemented.ByDesign; } } + public virtual string Name { get { throw NotImplemented.ByDesign; } } + + public virtual int MDStreamVersion { get { throw NotImplemented.ByDesign; } } + public virtual Guid ModuleVersionId { get { throw NotImplemented.ByDesign; } } + public virtual string ScopeName { get { throw NotImplemented.ByDesign; } } + public ModuleHandle ModuleHandle => GetModuleHandleImpl(); + protected virtual ModuleHandle GetModuleHandleImpl() => ModuleHandle.EmptyHandle; // Not an api but declared protected because of Reflection.Core/Corelib divide (when built by CoreRt) + public virtual void GetPEKind(out PortableExecutableKinds peKind, out ImageFileMachine machine) { throw NotImplemented.ByDesign; } + public virtual bool IsResource() { throw NotImplemented.ByDesign; } + + public virtual bool IsDefined(Type attributeType, bool inherit) { throw NotImplemented.ByDesign; } + public virtual IEnumerable<CustomAttributeData> CustomAttributes => GetCustomAttributesData(); + public virtual IList<CustomAttributeData> GetCustomAttributesData() { throw NotImplemented.ByDesign; } + public virtual object[] GetCustomAttributes(bool inherit) { throw NotImplemented.ByDesign; } + public virtual object[] GetCustomAttributes(Type attributeType, bool inherit) { throw NotImplemented.ByDesign; } + + public MethodInfo GetMethod(string name) + { + if (name == null) + throw new ArgumentNullException(nameof(name)); + + return GetMethodImpl(name, Module.DefaultLookup, null, CallingConventions.Any, null, null); + } + + public MethodInfo GetMethod(string name, Type[] types) => GetMethod(name, Module.DefaultLookup, null, CallingConventions.Any, types, null); + public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) + { + if (name == null) + throw new ArgumentNullException(nameof(name)); + if (types == null) + throw new ArgumentNullException(nameof(types)); + for (int i = 0; i < types.Length; i++) + { + if (types[i] == null) + throw new ArgumentNullException(nameof(types)); + } + return GetMethodImpl(name, bindingAttr, binder, callConvention, types, modifiers); + } + + protected virtual MethodInfo GetMethodImpl(string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) { throw NotImplemented.ByDesign; } + + public MethodInfo[] GetMethods() => GetMethods(Module.DefaultLookup); + public virtual MethodInfo[] GetMethods(BindingFlags bindingFlags) { throw NotImplemented.ByDesign; } + + public FieldInfo GetField(string name) => GetField(name, Module.DefaultLookup); + public virtual FieldInfo GetField(string name, BindingFlags bindingAttr) { throw NotImplemented.ByDesign; } + + public FieldInfo[] GetFields() => GetFields(Module.DefaultLookup); + public virtual FieldInfo[] GetFields(BindingFlags bindingFlags) { throw NotImplemented.ByDesign; } + + public virtual Type[] GetTypes() { throw NotImplemented.ByDesign; } + + public virtual Type GetType(string className) => GetType(className, throwOnError: false, ignoreCase: false); + public virtual Type GetType(string className, bool ignoreCase) => GetType(className, throwOnError: false, ignoreCase: ignoreCase); + public virtual Type GetType(string className, bool throwOnError, bool ignoreCase) { throw NotImplemented.ByDesign; } + + public virtual Type[] FindTypes(TypeFilter filter, object filterCriteria) + { + Type[] c = GetTypes(); + int cnt = 0; + for (int i = 0; i < c.Length; i++) + { + if (filter != null && !filter(c[i], filterCriteria)) + c[i] = null; + else + cnt++; + } + if (cnt == c.Length) + return c; + + Type[] ret = new Type[cnt]; + cnt = 0; + for (int i = 0; i < c.Length; i++) + { + if (c[i] != null) + ret[cnt++] = c[i]; + } + return ret; + } + + public virtual int MetadataToken { get { throw NotImplemented.ByDesign; } } + + public FieldInfo ResolveField(int metadataToken) => ResolveField(metadataToken, null, null); + public virtual FieldInfo ResolveField(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) { throw NotImplemented.ByDesign; } + + public MemberInfo ResolveMember(int metadataToken) => ResolveMember(metadataToken, null, null); + public virtual MemberInfo ResolveMember(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) { throw NotImplemented.ByDesign; } + + public MethodBase ResolveMethod(int metadataToken) => ResolveMethod(metadataToken, null, null); + public virtual MethodBase ResolveMethod(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) { throw NotImplemented.ByDesign; } + + public virtual byte[] ResolveSignature(int metadataToken) { throw NotImplemented.ByDesign; } + public virtual string ResolveString(int metadataToken) { throw NotImplemented.ByDesign; } + + public Type ResolveType(int metadataToken) => ResolveType(metadataToken, null, null); + public virtual Type ResolveType(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) { throw NotImplemented.ByDesign; } + + public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { throw NotImplemented.ByDesign; } + + public override bool Equals(object o) => base.Equals(o); + public override int GetHashCode() => base.GetHashCode(); + + public static bool operator ==(Module left, Module right) + { + if (object.ReferenceEquals(left, right)) + return true; + + if ((object)left == null || (object)right == null) + return false; + + return left.Equals(right); + } + + public static bool operator !=(Module left, Module right) => !(left == right); + + public override string ToString() => ScopeName; + + public static readonly TypeFilter FilterTypeName = FilterTypeNameImpl; + public static readonly TypeFilter FilterTypeNameIgnoreCase = FilterTypeNameIgnoreCaseImpl; + + private const BindingFlags DefaultLookup = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public; + + // FilterTypeName + // This method will filter the class based upon the name. It supports + // a trailing wild card. + private static bool FilterTypeNameImpl(Type cls, object filterCriteria) + { + // Check that the criteria object is a String object + if (filterCriteria == null || !(filterCriteria is string)) + throw new InvalidFilterCriteriaException(SR.InvalidFilterCriteriaException_CritString); + + string str = (string)filterCriteria; + + // Check to see if this is a prefix or exact match requirement + if (str.Length > 0 && str[str.Length - 1] == '*') + { + str = str.Substring(0, str.Length - 1); + return cls.Name.StartsWith(str, StringComparison.Ordinal); + } + + return cls.Name.Equals(str); + } + + // FilterFieldNameIgnoreCase + // This method filter the Type based upon name, it ignores case. + private static bool FilterTypeNameIgnoreCaseImpl(Type cls, object filterCriteria) + { + // Check that the criteria object is a String object + if (filterCriteria == null || !(filterCriteria is string)) + throw new InvalidFilterCriteriaException(SR.InvalidFilterCriteriaException_CritString); + + string str = (string)filterCriteria; + + // Check to see if this is a prefix or exact match requirement + if (str.Length > 0 && str[str.Length - 1] == '*') + { + str = str.Substring(0, str.Length - 1); + string name = cls.Name; + if (name.Length >= str.Length) + return (string.Compare(name, 0, str, 0, str.Length, StringComparison.OrdinalIgnoreCase) == 0); + else + return false; + } + return (string.Compare(str, cls.Name, StringComparison.OrdinalIgnoreCase) == 0); + } + } +} diff --git a/src/mscorlib/shared/System/Reflection/ModuleResolveEventHandler.cs b/src/mscorlib/shared/System/Reflection/ModuleResolveEventHandler.cs new file mode 100644 index 0000000000..eb8926b5db --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/ModuleResolveEventHandler.cs @@ -0,0 +1,9 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + public delegate Module ModuleResolveEventHandler(object sender, ResolveEventArgs e); +} + diff --git a/src/mscorlib/shared/System/Reflection/ObfuscateAssemblyAttribute.cs b/src/mscorlib/shared/System/Reflection/ObfuscateAssemblyAttribute.cs new file mode 100644 index 0000000000..f8f765ced2 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/ObfuscateAssemblyAttribute.cs @@ -0,0 +1,19 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false, Inherited = false)] + public sealed class ObfuscateAssemblyAttribute : Attribute + { + public ObfuscateAssemblyAttribute(bool assemblyIsPrivate) + { + AssemblyIsPrivate = assemblyIsPrivate; + } + + public bool AssemblyIsPrivate { get; } + public bool StripAfterObfuscation { get; set; } = true; + } +} + diff --git a/src/mscorlib/shared/System/Reflection/ObfuscationAttribute.cs b/src/mscorlib/shared/System/Reflection/ObfuscationAttribute.cs new file mode 100644 index 0000000000..11d93b6313 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/ObfuscationAttribute.cs @@ -0,0 +1,21 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Event | AttributeTargets.Interface | AttributeTargets.Enum | AttributeTargets.Delegate, + AllowMultiple = true, Inherited = false)] + public sealed class ObfuscationAttribute : Attribute + { + public ObfuscationAttribute() + { + } + + public bool StripAfterObfuscation { get; set; } = true; + public bool Exclude { get; set; } = true; + public bool ApplyToMembers { get; set; } = true; + public string Feature { get; set; } = "all"; + } +} + diff --git a/src/mscorlib/shared/System/Reflection/ParameterAttributes.cs b/src/mscorlib/shared/System/Reflection/ParameterAttributes.cs new file mode 100644 index 0000000000..ce195897c2 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/ParameterAttributes.cs @@ -0,0 +1,29 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +// ParameterAttributes is an enum defining the attributes that may be +// associated with a Parameter. These are defined in CorHdr.h. + +namespace System.Reflection +{ + // This Enum matchs the CorParamAttr defined in CorHdr.h + [Flags] + public enum ParameterAttributes + { + None = 0x0000, // no flag is specified + In = 0x0001, // Param is [In] + Out = 0x0002, // Param is [Out] + Lcid = 0x0004, // Param is [lcid] + + Retval = 0x0008, // Param is [Retval] + Optional = 0x0010, // Param is optional + + HasDefault = 0x1000, // Param has default value. + HasFieldMarshal = 0x2000, // Param has FieldMarshal. + + Reserved3 = 0x4000, + Reserved4 = 0x8000, + ReservedMask = 0xf000, + } +} diff --git a/src/mscorlib/shared/System/Reflection/ParameterInfo.cs b/src/mscorlib/shared/System/Reflection/ParameterInfo.cs new file mode 100644 index 0000000000..94bfffaa53 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/ParameterInfo.cs @@ -0,0 +1,110 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using System.Runtime.Serialization; + +namespace System.Reflection +{ + public class ParameterInfo : ICustomAttributeProvider, IObjectReference + { + protected ParameterInfo() { } + + public virtual ParameterAttributes Attributes => AttrsImpl; + public virtual MemberInfo Member => MemberImpl; + public virtual string Name => NameImpl; + public virtual Type ParameterType => ClassImpl; + public virtual int Position => PositionImpl; + + public bool IsIn => (Attributes & ParameterAttributes.In) != 0; + public bool IsLcid => (Attributes & ParameterAttributes.Lcid) != 0; + public bool IsOptional => (Attributes & ParameterAttributes.Optional) != 0; + public bool IsOut => (Attributes & ParameterAttributes.Out) != 0; + public bool IsRetval => (Attributes & ParameterAttributes.Retval) != 0; + + public virtual object DefaultValue { get { throw NotImplemented.ByDesign; } } + public virtual object RawDefaultValue { get { throw NotImplemented.ByDesign; } } + public virtual bool HasDefaultValue { get { throw NotImplemented.ByDesign; } } + + public virtual bool IsDefined(Type attributeType, bool inherit) + { + if (attributeType == null) + throw new ArgumentNullException(nameof(attributeType)); + + return false; + } + + public virtual IEnumerable<CustomAttributeData> CustomAttributes => GetCustomAttributesData(); + public virtual IList<CustomAttributeData> GetCustomAttributesData() { throw NotImplemented.ByDesign; } + + public virtual object[] GetCustomAttributes(bool inherit) => Array.Empty<object>(); + public virtual object[] GetCustomAttributes(Type attributeType, bool inherit) + { + if (attributeType == null) + throw new ArgumentNullException(nameof(attributeType)); + + return Array.Empty<object>(); + } + + public virtual Type[] GetOptionalCustomModifiers() => Array.Empty<Type>(); + public virtual Type[] GetRequiredCustomModifiers() => Array.Empty<Type>(); + + public virtual int MetadataToken => MetadataToken_ParamDef; + + public object GetRealObject(StreamingContext context) + { + // Once all the serializable fields have come in we can set up the real + // instance based on just two of them (MemberImpl and PositionImpl). + + if (MemberImpl == null) + throw new SerializationException(SR.Serialization_InsufficientState); + + ParameterInfo[] args = null; + + switch (MemberImpl.MemberType) + { + case MemberTypes.Constructor: + case MemberTypes.Method: + if (PositionImpl == -1) + { + if (MemberImpl.MemberType == MemberTypes.Method) + return ((MethodInfo)MemberImpl).ReturnParameter; + else + throw new SerializationException(SR.Serialization_BadParameterInfo); + } + else + { + args = ((MethodBase)MemberImpl).GetParametersNoCopy(); + + if (args != null && PositionImpl < args.Length) + return args[PositionImpl]; + else + throw new SerializationException(SR.Serialization_BadParameterInfo); + } + + case MemberTypes.Property: + args = ((PropertyInfo)MemberImpl).GetIndexParameters(); + + if (args != null && PositionImpl > -1 && PositionImpl < args.Length) + return args[PositionImpl]; + else + throw new SerializationException(SR.Serialization_BadParameterInfo); + + default: + throw new SerializationException(SR.Serialization_NoParameterInfo); + } + } + + public override string ToString() => ParameterType.FormatTypeName() + " " + Name; + + protected ParameterAttributes AttrsImpl; + protected Type ClassImpl; + protected object DefaultValueImpl; + protected MemberInfo MemberImpl; + protected string NameImpl; + protected int PositionImpl; + + private const int MetadataToken_ParamDef = 0x08000000; + } +} diff --git a/src/mscorlib/shared/System/Reflection/ParameterModifier.cs b/src/mscorlib/shared/System/Reflection/ParameterModifier.cs new file mode 100644 index 0000000000..18d6cf669d --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/ParameterModifier.cs @@ -0,0 +1,36 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [Serializable] + public struct ParameterModifier + { + private readonly bool[] _byRef; + + public ParameterModifier(int parameterCount) + { + if (parameterCount <= 0) + throw new ArgumentException(SR.Arg_ParmArraySize); + + _byRef = new bool[parameterCount]; + } + + public bool this[int index] + { + get + { + return _byRef[index]; + } + set + { + _byRef[index] = value; + } + } + +#if CORECLR + internal bool[] IsByRefArray => _byRef; +#endif + } +} diff --git a/src/mscorlib/shared/System/Reflection/Pointer.cs b/src/mscorlib/shared/System/Reflection/Pointer.cs new file mode 100644 index 0000000000..13a5efff46 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/Pointer.cs @@ -0,0 +1,61 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Diagnostics; +using System.Runtime.Serialization; + +namespace System.Reflection +{ + [Serializable] + [CLSCompliant(false)] + public sealed unsafe class Pointer : ISerializable + { + // CoreCLR: Do not add or remove fields without updating the ReflectionPointer class in runtimehandles.h + private readonly void* _ptr; + private readonly Type _ptrType; + + private Pointer(void* ptr, Type ptrType) + { + Debug.Assert(ptrType.IsRuntimeImplemented()); // CoreCLR: For CoreRT's sake, _ptrType has to be declared as "Type", but in fact, it is always a RuntimeType. Code on CoreCLR expects this. + _ptr = ptr; + _ptrType = ptrType; + } + + private Pointer(SerializationInfo info, StreamingContext context) + { + _ptr = ((IntPtr)(info.GetValue("_ptr", typeof(IntPtr)))).ToPointer(); + _ptrType = (Type)info.GetValue("_ptrType", typeof(Type)); + if (!_ptrType.IsRuntimeImplemented()) + throw new SerializationException(SR.Arg_MustBeType); + } + + public static object Box(void* ptr, Type type) + { + if (type == null) + throw new ArgumentNullException(nameof(type)); + if (!type.IsPointer) + throw new ArgumentException(SR.Arg_MustBePointer, nameof(ptr)); + if (!type.IsRuntimeImplemented()) + throw new ArgumentException(SR.Arg_MustBeType, nameof(ptr)); + + return new Pointer(ptr, type); + } + + public static void* Unbox(object ptr) + { + if (!(ptr is Pointer)) + throw new ArgumentException(SR.Arg_MustBePointer, nameof(ptr)); + return ((Pointer)ptr)._ptr; + } + + void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) + { + info.AddValue("_ptr", new IntPtr(_ptr)); + info.AddValue("_ptrType", _ptrType); + } + + internal Type GetPointerType() => _ptrType; + internal object GetPointerValue() => (IntPtr)_ptr; + } +} diff --git a/src/mscorlib/shared/System/Reflection/PortableExecutableKinds.cs b/src/mscorlib/shared/System/Reflection/PortableExecutableKinds.cs new file mode 100644 index 0000000000..79be338685 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/PortableExecutableKinds.cs @@ -0,0 +1,18 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [Flags] + public enum PortableExecutableKinds + { + NotAPortableExecutableImage = 0x0, + ILOnly = 0x1, + Required32Bit = 0x2, + PE32Plus = 0x4, + Unmanaged32Bit = 0x8, + Preferred32Bit = 0x10, + } +} + diff --git a/src/mscorlib/shared/System/Reflection/ProcessorArchitecture.cs b/src/mscorlib/shared/System/Reflection/ProcessorArchitecture.cs new file mode 100644 index 0000000000..becb346c4f --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/ProcessorArchitecture.cs @@ -0,0 +1,16 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + public enum ProcessorArchitecture + { + None = 0x0000, + MSIL = 0x0001, + X86 = 0x0002, + IA64 = 0x0003, + Amd64 = 0x0004, + Arm = 0x0005 + } +} diff --git a/src/mscorlib/shared/System/Reflection/PropertyAttributes.cs b/src/mscorlib/shared/System/Reflection/PropertyAttributes.cs new file mode 100644 index 0000000000..31e7a653bb --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/PropertyAttributes.cs @@ -0,0 +1,25 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +// PropertyAttributes is an enum which defines the attributes that may be associated +// with a property. The values here are defined in Corhdr.h. + +namespace System.Reflection +{ + // This Enum matchs the CorPropertyAttr defined in CorHdr.h + [Flags] + public enum PropertyAttributes + { + None = 0x0000, + SpecialName = 0x0200, // property is special. Name describes how. + + RTSpecialName = 0x0400, // Runtime(metadata internal APIs) should check name encoding. + HasDefault = 0x1000, // Property has default + + Reserved2 = 0x2000, + Reserved3 = 0x4000, + Reserved4 = 0x8000, + ReservedMask = 0xf400, + } +} diff --git a/src/mscorlib/shared/System/Reflection/PropertyInfo.cs b/src/mscorlib/shared/System/Reflection/PropertyInfo.cs new file mode 100644 index 0000000000..ff8a02e96d --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/PropertyInfo.cs @@ -0,0 +1,74 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Diagnostics; +using System.Globalization; + +namespace System.Reflection +{ + public abstract class PropertyInfo : MemberInfo + { + protected PropertyInfo() { } + + public override MemberTypes MemberType => MemberTypes.Property; + + public abstract Type PropertyType { get; } + public abstract ParameterInfo[] GetIndexParameters(); + + public abstract PropertyAttributes Attributes { get; } + public bool IsSpecialName => (Attributes & PropertyAttributes.SpecialName) != 0; + + public abstract bool CanRead { get; } + public abstract bool CanWrite { get; } + + public MethodInfo[] GetAccessors() => GetAccessors(nonPublic: false); + public abstract MethodInfo[] GetAccessors(bool nonPublic); + + public virtual MethodInfo GetMethod => GetGetMethod(nonPublic: true); + public MethodInfo GetGetMethod() => GetGetMethod(nonPublic: false); + public abstract MethodInfo GetGetMethod(bool nonPublic); + + public virtual MethodInfo SetMethod => GetSetMethod(nonPublic: true); + public MethodInfo GetSetMethod() => GetSetMethod(nonPublic: false); + public abstract MethodInfo GetSetMethod(bool nonPublic); + + public virtual Type[] GetOptionalCustomModifiers() => Array.Empty<Type>(); + public virtual Type[] GetRequiredCustomModifiers() => Array.Empty<Type>(); + + [DebuggerHidden] + [DebuggerStepThrough] + public object GetValue(object obj) => GetValue(obj, index: null); + [DebuggerHidden] + [DebuggerStepThrough] + public virtual object GetValue(object obj, object[] index) => GetValue(obj, BindingFlags.Default, binder: null, index: index, culture: null); + public abstract object GetValue(object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture); + + public virtual object GetConstantValue() { throw NotImplemented.ByDesign; } + public virtual object GetRawConstantValue() { throw NotImplemented.ByDesign; } + + [DebuggerHidden] + [DebuggerStepThrough] + public void SetValue(object obj, object value) => SetValue(obj, value, index: null); + [DebuggerHidden] + [DebuggerStepThrough] + public virtual void SetValue(object obj, object value, object[] index) => SetValue(obj, value, BindingFlags.Default, binder: null, index: index, culture: null); + public abstract void SetValue(object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture); + + public override bool Equals(object obj) => base.Equals(obj); + public override int GetHashCode() => base.GetHashCode(); + + public static bool operator ==(PropertyInfo left, PropertyInfo right) + { + if (object.ReferenceEquals(left, right)) + return true; + + if ((object)left == null || (object)right == null) + return false; + + return left.Equals(right); + } + + public static bool operator !=(PropertyInfo left, PropertyInfo right) => !(left == right); + } +} diff --git a/src/mscorlib/shared/System/Reflection/ReflectionContext.cs b/src/mscorlib/shared/System/Reflection/ReflectionContext.cs new file mode 100644 index 0000000000..e9e93dab81 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/ReflectionContext.cs @@ -0,0 +1,24 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + public abstract class ReflectionContext + { + protected ReflectionContext() { } + + public abstract Assembly MapAssembly(Assembly assembly); + + public abstract TypeInfo MapType(TypeInfo type); + + public virtual TypeInfo GetTypeForObject(object value) + { + if (value == null) + throw new ArgumentNullException(nameof(value)); + + return MapType(value.GetType().GetTypeInfo()); + } + } +} + diff --git a/src/mscorlib/shared/System/Reflection/ReflectionTypeLoadException.cs b/src/mscorlib/shared/System/Reflection/ReflectionTypeLoadException.cs new file mode 100644 index 0000000000..772620cf84 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/ReflectionTypeLoadException.cs @@ -0,0 +1,46 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.Serialization; + +namespace System.Reflection +{ + [Serializable] + public sealed class ReflectionTypeLoadException : SystemException, ISerializable + { + public ReflectionTypeLoadException(Type[] classes, Exception[] exceptions) + : base(null) + { + Types = classes; + LoaderExceptions = exceptions; + HResult = __HResults.COR_E_REFLECTIONTYPELOAD; + } + + public ReflectionTypeLoadException(Type[] classes, Exception[] exceptions, string message) + : base(message) + { + Types = classes; + LoaderExceptions = exceptions; + HResult = __HResults.COR_E_REFLECTIONTYPELOAD; + } + + internal ReflectionTypeLoadException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + Types = (Type[])(info.GetValue("Types", typeof(Type[]))); + LoaderExceptions = (Exception[])(info.GetValue("Exceptions", typeof(Exception[]))); + } + + public override void GetObjectData(SerializationInfo info, StreamingContext context) + { + base.GetObjectData(info, context); + info.AddValue("Types", Types, typeof(Type[])); + info.AddValue("Exceptions", LoaderExceptions, typeof(Exception[])); + } + + public Type[] Types { get; } + + public Exception[] LoaderExceptions { get; } + } +} diff --git a/src/mscorlib/shared/System/Reflection/ResourceAttributes.cs b/src/mscorlib/shared/System/Reflection/ResourceAttributes.cs new file mode 100644 index 0000000000..2d03f42ba0 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/ResourceAttributes.cs @@ -0,0 +1,14 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [Flags] + public enum ResourceAttributes + { + Public = 0x0001, + Private = 0x0002, + } +} + diff --git a/src/mscorlib/shared/System/Reflection/ResourceLocation.cs b/src/mscorlib/shared/System/Reflection/ResourceLocation.cs new file mode 100644 index 0000000000..4902333ac0 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/ResourceLocation.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + [Flags] + public enum ResourceLocation + { + ContainedInAnotherAssembly = 2, + ContainedInManifestFile = 4, + Embedded = 1, + } +} + diff --git a/src/mscorlib/shared/System/Reflection/StrongNameKeyPair.cs b/src/mscorlib/shared/System/Reflection/StrongNameKeyPair.cs new file mode 100644 index 0000000000..c04ddd6d1a --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/StrongNameKeyPair.cs @@ -0,0 +1,74 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.IO; +using System.Runtime.Serialization; + +namespace System.Reflection +{ + [Serializable] + public class StrongNameKeyPair : IDeserializationCallback, ISerializable + { + private bool _keyPairExported; + private byte[] _keyPairArray; + private string _keyPairContainer; + private byte[] _publicKey; + + // Build key pair from file. + public StrongNameKeyPair(FileStream keyPairFile) + { + if (keyPairFile == null) + throw new ArgumentNullException(nameof(keyPairFile)); + + int length = (int)keyPairFile.Length; + _keyPairArray = new byte[length]; + keyPairFile.Read(_keyPairArray, 0, length); + + _keyPairExported = true; + } + + // Build key pair from byte array in memory. + public StrongNameKeyPair(byte[] keyPairArray) + { + if (keyPairArray == null) + throw new ArgumentNullException(nameof(keyPairArray)); + + _keyPairArray = new byte[keyPairArray.Length]; + Array.Copy(keyPairArray, _keyPairArray, keyPairArray.Length); + + _keyPairExported = true; + } + + protected StrongNameKeyPair(SerializationInfo info, StreamingContext context) + { + _keyPairExported = (bool)info.GetValue("_keyPairExported", typeof(bool)); + _keyPairArray = (byte[])info.GetValue("_keyPairArray", typeof(byte[])); + _keyPairContainer = (string)info.GetValue("_keyPairContainer", typeof(string)); + _publicKey = (byte[])info.GetValue("_publicKey", typeof(byte[])); + } + + public StrongNameKeyPair(string keyPairContainer) + { + throw new PlatformNotSupportedException(SR.PlatformNotSupported_StrongNameSigning); + } + + public byte[] PublicKey + { + get + { + throw new PlatformNotSupportedException(SR.PlatformNotSupported_StrongNameSigning); + } + } + + void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) + { + info.AddValue("_keyPairExported", _keyPairExported); + info.AddValue("_keyPairArray", _keyPairArray); + info.AddValue("_keyPairContainer", _keyPairContainer); + info.AddValue("_publicKey", _publicKey); + } + + void IDeserializationCallback.OnDeserialization(object sender) { } + } +} diff --git a/src/mscorlib/shared/System/Reflection/TargetException.cs b/src/mscorlib/shared/System/Reflection/TargetException.cs new file mode 100644 index 0000000000..03f8730cdd --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/TargetException.cs @@ -0,0 +1,33 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.Serialization; + +namespace System.Reflection +{ + [Serializable] + public class TargetException : ApplicationException + { + public TargetException() + : this(null) + { + } + + public TargetException(string message) + : this(message, null) + { + } + + public TargetException(string message, Exception inner) + : base(message, inner) + { + HResult = __HResults.COR_E_TARGET; + } + + protected TargetException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } + } +} diff --git a/src/mscorlib/shared/System/Reflection/TargetInvocationException.cs b/src/mscorlib/shared/System/Reflection/TargetInvocationException.cs new file mode 100644 index 0000000000..e934e5bde7 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/TargetInvocationException.cs @@ -0,0 +1,29 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.Serialization; + +namespace System.Reflection +{ + [Serializable] + public sealed class TargetInvocationException : ApplicationException + { + public TargetInvocationException(Exception inner) + : base(SR.Arg_TargetInvocationException, inner) + { + HResult = __HResults.COR_E_TARGETINVOCATION; + } + + public TargetInvocationException(string message, Exception inner) + : base(message, inner) + { + HResult = __HResults.COR_E_TARGETINVOCATION; + } + + internal TargetInvocationException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } + } +} diff --git a/src/mscorlib/shared/System/Reflection/TargetParameterCountException.cs b/src/mscorlib/shared/System/Reflection/TargetParameterCountException.cs new file mode 100644 index 0000000000..c3604548e6 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/TargetParameterCountException.cs @@ -0,0 +1,35 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.Serialization; + +namespace System.Reflection +{ + [Serializable] + public sealed class TargetParameterCountException : ApplicationException + { + public TargetParameterCountException() + : base(SR.Arg_TargetParameterCountException) + { + HResult = __HResults.COR_E_TARGETPARAMCOUNT; + } + + public TargetParameterCountException(string message) + : base(message) + { + HResult = __HResults.COR_E_TARGETPARAMCOUNT; + } + + public TargetParameterCountException(string message, Exception inner) + : base(message, inner) + { + HResult = __HResults.COR_E_TARGETPARAMCOUNT; + } + + internal TargetParameterCountException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } + } +} diff --git a/src/mscorlib/shared/System/Reflection/TypeAttributes.cs b/src/mscorlib/shared/System/Reflection/TypeAttributes.cs new file mode 100644 index 0000000000..aa30331856 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/TypeAttributes.cs @@ -0,0 +1,63 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.InteropServices; + +namespace System.Reflection +{ + // This Enum matchs the CorTypeAttr defined in CorHdr.h + [Flags] + public enum TypeAttributes + { + VisibilityMask = 0x00000007, + NotPublic = 0x00000000, // Class is not public scope. + Public = 0x00000001, // Class is public scope. + NestedPublic = 0x00000002, // Class is nested with public visibility. + NestedPrivate = 0x00000003, // Class is nested with private visibility. + NestedFamily = 0x00000004, // Class is nested with family visibility. + NestedAssembly = 0x00000005, // Class is nested with assembly visibility. + NestedFamANDAssem = 0x00000006, // Class is nested with family and assembly visibility. + NestedFamORAssem = 0x00000007, // Class is nested with family or assembly visibility. + + // Use this mask to retrieve class layout informaiton + // 0 is AutoLayout, 0x2 is SequentialLayout, 4 is ExplicitLayout + LayoutMask = 0x00000018, + AutoLayout = 0x00000000, // Class fields are auto-laid out + SequentialLayout = 0x00000008, // Class fields are laid out sequentially + ExplicitLayout = 0x00000010, // Layout is supplied explicitly + // end layout mask + + // Use this mask to distinguish whether a type declaration is an interface. (Class vs. ValueType done based on whether it subclasses S.ValueType) + ClassSemanticsMask = 0x00000020, + Class = 0x00000000, // Type is a class (or a value type). + Interface = 0x00000020, // Type is an interface. + + // Special semantics in addition to class semantics. + Abstract = 0x00000080, // Class is abstract + Sealed = 0x00000100, // Class is concrete and may not be extended + SpecialName = 0x00000400, // Class name is special. Name describes how. + + // Implementation attributes. + Import = 0x00001000, // Class / interface is imported + Serializable = 0x00002000, // The class is Serializable. + WindowsRuntime = 0x00004000, // Type is a Windows Runtime type. + + // Use tdStringFormatMask to retrieve string information for native interop + StringFormatMask = 0x00030000, + AnsiClass = 0x00000000, // LPTSTR is interpreted as ANSI in this class + UnicodeClass = 0x00010000, // LPTSTR is interpreted as UNICODE + AutoClass = 0x00020000, // LPTSTR is interpreted automatically + CustomFormatClass = 0x00030000, // A non-standard encoding specified by CustomFormatMask + CustomFormatMask = 0x00C00000, // Use this mask to retrieve non-standard encoding information for native interop. The meaning of the values of these 2 bits is unspecified. + + // end string format mask + + BeforeFieldInit = 0x00100000, // Initialize the class any time before first static field access. + + RTSpecialName = 0x00000800, // Runtime should check name encoding. + HasSecurity = 0x00040000, // Class has security associate with it. + + ReservedMask = 0x00040800, + } +} diff --git a/src/mscorlib/shared/System/Reflection/TypeDelegator.cs b/src/mscorlib/shared/System/Reflection/TypeDelegator.cs new file mode 100644 index 0000000000..7f928d2486 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/TypeDelegator.cs @@ -0,0 +1,124 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +// TypeDelegator +// +// This class wraps a Type object and delegates all methods to that Type. + +using CultureInfo = System.Globalization.CultureInfo; + +namespace System.Reflection +{ + [Serializable] + public class TypeDelegator : TypeInfo + { + public override bool IsAssignableFrom(TypeInfo typeInfo) + { + if (typeInfo == null) + return false; + return IsAssignableFrom(typeInfo.AsType()); + } + + protected Type typeImpl; + + protected TypeDelegator() { } + + public TypeDelegator(Type delegatingType) + { + if (delegatingType == null) + throw new ArgumentNullException(nameof(delegatingType)); + + typeImpl = delegatingType; + } + + public override Guid GUID => typeImpl.GUID; + public override int MetadataToken => typeImpl.MetadataToken; + + public override object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, + object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) + { + return typeImpl.InvokeMember(name, invokeAttr, binder, target, args, modifiers, culture, namedParameters); + } + + public override Module Module => typeImpl.Module; + public override Assembly Assembly => typeImpl.Assembly; + public override RuntimeTypeHandle TypeHandle => typeImpl.TypeHandle; + public override string Name => typeImpl.Name; + public override string FullName => typeImpl.FullName; + public override string Namespace => typeImpl.Namespace; + public override string AssemblyQualifiedName => typeImpl.AssemblyQualifiedName; + public override Type BaseType => typeImpl.BaseType; + + protected override ConstructorInfo GetConstructorImpl(BindingFlags bindingAttr, Binder binder, + CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) + { + return typeImpl.GetConstructor(bindingAttr, binder, callConvention, types, modifiers); + } + + public override ConstructorInfo[] GetConstructors(BindingFlags bindingAttr) => typeImpl.GetConstructors(bindingAttr); + + protected override MethodInfo GetMethodImpl(string name, BindingFlags bindingAttr, Binder binder, + CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) + { + // This is interesting there are two paths into the impl. One that validates + // type as non-null and one where type may be null. + if (types == null) + return typeImpl.GetMethod(name, bindingAttr); + else + return typeImpl.GetMethod(name, bindingAttr, binder, callConvention, types, modifiers); + } + + public override MethodInfo[] GetMethods(BindingFlags bindingAttr) => typeImpl.GetMethods(bindingAttr); + + public override FieldInfo GetField(string name, BindingFlags bindingAttr) => typeImpl.GetField(name, bindingAttr); + public override FieldInfo[] GetFields(BindingFlags bindingAttr) => typeImpl.GetFields(bindingAttr); + + public override Type GetInterface(string name, bool ignoreCase) => typeImpl.GetInterface(name, ignoreCase); + + public override Type[] GetInterfaces() => typeImpl.GetInterfaces(); + + public override EventInfo GetEvent(string name, BindingFlags bindingAttr) => typeImpl.GetEvent(name, bindingAttr); + + public override EventInfo[] GetEvents() => typeImpl.GetEvents(); + + protected override PropertyInfo GetPropertyImpl(string name, BindingFlags bindingAttr, Binder binder, + Type returnType, Type[] types, ParameterModifier[] modifiers) + { + if (returnType == null && types == null) + return typeImpl.GetProperty(name, bindingAttr); + else + return typeImpl.GetProperty(name, bindingAttr, binder, returnType, types, modifiers); + } + + public override PropertyInfo[] GetProperties(BindingFlags bindingAttr) => typeImpl.GetProperties(bindingAttr); + public override EventInfo[] GetEvents(BindingFlags bindingAttr) => typeImpl.GetEvents(bindingAttr); + public override Type[] GetNestedTypes(BindingFlags bindingAttr) => typeImpl.GetNestedTypes(bindingAttr); + public override Type GetNestedType(string name, BindingFlags bindingAttr) => typeImpl.GetNestedType(name, bindingAttr); + public override MemberInfo[] GetMember(string name, MemberTypes type, BindingFlags bindingAttr) => typeImpl.GetMember(name, type, bindingAttr); + public override MemberInfo[] GetMembers(BindingFlags bindingAttr) => typeImpl.GetMembers(bindingAttr); + + protected override TypeAttributes GetAttributeFlagsImpl() => typeImpl.Attributes; + + public override bool IsSZArray => typeImpl.IsSZArray; + + protected override bool IsArrayImpl() => typeImpl.IsArray; + protected override bool IsPrimitiveImpl() => typeImpl.IsPrimitive; + protected override bool IsByRefImpl() => typeImpl.IsByRef; + protected override bool IsPointerImpl() => typeImpl.IsPointer; + protected override bool IsValueTypeImpl() => typeImpl.IsValueType; + protected override bool IsCOMObjectImpl() => typeImpl.IsCOMObject; + public override bool IsConstructedGenericType => typeImpl.IsConstructedGenericType; + public override Type GetElementType() => typeImpl.GetElementType(); + protected override bool HasElementTypeImpl() => typeImpl.HasElementType; + + public override Type UnderlyingSystemType => typeImpl.UnderlyingSystemType; + + // ICustomAttributeProvider + public override object[] GetCustomAttributes(bool inherit) => typeImpl.GetCustomAttributes(inherit); + public override object[] GetCustomAttributes(Type attributeType, bool inherit) => typeImpl.GetCustomAttributes(attributeType, inherit); + + public override bool IsDefined(Type attributeType, bool inherit) => typeImpl.IsDefined(attributeType, inherit); + public override InterfaceMapping GetInterfaceMap(Type interfaceType) => typeImpl.GetInterfaceMap(interfaceType); + } +} diff --git a/src/mscorlib/shared/System/Reflection/TypeFilter.cs b/src/mscorlib/shared/System/Reflection/TypeFilter.cs new file mode 100644 index 0000000000..eb049f81f9 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/TypeFilter.cs @@ -0,0 +1,8 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Reflection +{ + public delegate bool TypeFilter(Type m, object filterCriteria); +} diff --git a/src/mscorlib/shared/System/Reflection/TypeInfo.cs b/src/mscorlib/shared/System/Reflection/TypeInfo.cs new file mode 100644 index 0000000000..f4add736f4 --- /dev/null +++ b/src/mscorlib/shared/System/Reflection/TypeInfo.cs @@ -0,0 +1,84 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; + +namespace System.Reflection +{ + public abstract partial class TypeInfo : Type, IReflectableType + { + protected TypeInfo() { } + + TypeInfo IReflectableType.GetTypeInfo() => this; + public virtual Type AsType() => this; + + public virtual Type[] GenericTypeParameters => IsGenericTypeDefinition ? GetGenericArguments() : Type.EmptyTypes; + + public virtual EventInfo GetDeclaredEvent(string name) => GetEvent(name, TypeInfo.DeclaredOnlyLookup); + public virtual FieldInfo GetDeclaredField(string name) => GetField(name, TypeInfo.DeclaredOnlyLookup); + public virtual MethodInfo GetDeclaredMethod(string name) => GetMethod(name, TypeInfo.DeclaredOnlyLookup); + public virtual TypeInfo GetDeclaredNestedType(string name) => GetNestedType(name, TypeInfo.DeclaredOnlyLookup)?.GetTypeInfo(); + public virtual PropertyInfo GetDeclaredProperty(string name) => GetProperty(name, TypeInfo.DeclaredOnlyLookup); + + public virtual IEnumerable<MethodInfo> GetDeclaredMethods(string name) + { + foreach (MethodInfo method in GetMethods(TypeInfo.DeclaredOnlyLookup)) + { + if (method.Name == name) + yield return method; + } + } + + public virtual IEnumerable<ConstructorInfo> DeclaredConstructors => GetConstructors(TypeInfo.DeclaredOnlyLookup); + public virtual IEnumerable<EventInfo> DeclaredEvents => GetEvents(TypeInfo.DeclaredOnlyLookup); + public virtual IEnumerable<FieldInfo> DeclaredFields => GetFields(TypeInfo.DeclaredOnlyLookup); + public virtual IEnumerable<MemberInfo> DeclaredMembers => GetMembers(TypeInfo.DeclaredOnlyLookup); + public virtual IEnumerable<MethodInfo> DeclaredMethods => GetMethods(TypeInfo.DeclaredOnlyLookup); + public virtual IEnumerable<System.Reflection.TypeInfo> DeclaredNestedTypes + { + get + { + foreach (Type t in GetNestedTypes(TypeInfo.DeclaredOnlyLookup)) + { + yield return t.GetTypeInfo(); + } + } + } + public virtual IEnumerable<PropertyInfo> DeclaredProperties => GetProperties(TypeInfo.DeclaredOnlyLookup); + + public virtual IEnumerable<Type> ImplementedInterfaces => GetInterfaces(); + + //a re-implementation of ISAF from Type, skipping the use of UnderlyingType + public virtual bool IsAssignableFrom(TypeInfo typeInfo) + { + if (typeInfo == null) + return false; + + if (this == typeInfo) + return true; + + // If c is a subclass of this class, then c can be cast to this type. + if (typeInfo.IsSubclassOf(this)) + return true; + + if (this.IsInterface) + { + return typeInfo.ImplementInterface(this); + } + else if (IsGenericParameter) + { + Type[] constraints = GetGenericParameterConstraints(); + for (int i = 0; i < constraints.Length; i++) + if (!constraints[i].IsAssignableFrom(typeInfo)) + return false; + + return true; + } + + return false; + } + + private const BindingFlags DeclaredOnlyLookup = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly; + } +} |