Exemple #1
0
        public sealed override AssemblyName GetName()
        {
#if ENABLE_REFLECTION_TRACE
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.Assembly_GetName(this);
            }
#endif
            return(RuntimeAssemblyName.ToAssemblyName());
        }
Exemple #2
0
        public sealed override Type MakeByRefType()
        {
#if ENABLE_REFLECTION_TRACE
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.TypeInfo_MakeByRefType(this);
            }
#endif
            return(this.GetByRefType());
        }
Exemple #3
0
        public sealed override Type MakeGenericType(params Type[] typeArguments)
        {
#if ENABLE_REFLECTION_TRACE
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.TypeInfo_MakeGenericType(this, typeArguments);
            }
#endif

            if (typeArguments == null)
            {
                throw new ArgumentNullException(nameof(typeArguments));
            }

            if (!IsGenericTypeDefinition)
            {
                throw new InvalidOperationException(SR.Format(SR.Arg_NotGenericTypeDefinition, this));
            }

            // We intentionally don't validate the number of arguments or their suitability to the generic type's constraints.
            // In a pay-for-play world, this can cause needless MissingMetadataExceptions. There is no harm in creating
            // the Type object for an inconsistent generic type - no EEType will ever match it so any attempt to "invoke" it
            // will throw an exception.
            RuntimeTypeInfo[] runtimeTypeArguments = new RuntimeTypeInfo[typeArguments.Length];
            for (int i = 0; i < typeArguments.Length; i++)
            {
                RuntimeTypeInfo runtimeTypeArgument = typeArguments[i] as RuntimeTypeInfo;
                if (runtimeTypeArgument == null)
                {
                    if (typeArguments[i] == null)
                    {
                        throw new ArgumentNullException();
                    }
                    else
                    {
                        throw new PlatformNotSupportedException(SR.PlatformNotSupported_MakeGenericType); // "PlatformNotSupported" because on desktop, passing in a foreign type is allowed and creates a RefEmit.TypeBuilder
                    }
                }

                // Desktop compatibility: Treat generic type definitions as a constructed generic type using the generic parameters as type arguments.
                if (runtimeTypeArgument.IsGenericTypeDefinition)
                {
                    runtimeTypeArgument = runtimeTypeArgument.GetConstructedGenericType(runtimeTypeArgument.RuntimeGenericTypeParameters);
                }

                if (runtimeTypeArgument.IsByRefLike)
                {
                    throw new TypeLoadException(SR.CannotUseByRefLikeTypeInInstantiation);
                }

                runtimeTypeArguments[i] = runtimeTypeArgument;
            }
            return(this.GetConstructedGenericType(runtimeTypeArguments));
        }
Exemple #4
0
        public sealed override Type MakePointerType()
        {
#if ENABLE_REFLECTION_TRACE
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.TypeInfo_MakePointerType(this);
            }
#endif

            return(AsType().MakePointerType());
        }
Exemple #5
0
        public sealed override Type MakeGenericType(params Type[] typeArguments)
        {
#if ENABLE_REFLECTION_TRACE
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.TypeInfo_MakeGenericType(this, typeArguments);
            }
#endif

            return(AsType().MakeGenericType(typeArguments));
        }
Exemple #6
0
        public sealed override Type MakeArrayType(int rank)
        {
#if ENABLE_REFLECTION_TRACE
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.TypeInfo_MakeArrayType(this, rank);
            }
#endif

            return(AsType().MakeArrayType(rank));
        }
Exemple #7
0
        public sealed override void SetValue(Object obj, Object value)
        {
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.FieldInfo_SetValue(this, obj, value);
            }

            FieldAccessor fieldAccessor = this.FieldAccessor;

            fieldAccessor.SetField(obj, value);
        }
Exemple #8
0
        public sealed override Object GetValue(Object obj)
        {
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.FieldInfo_GetValue(this, obj);
            }

            FieldAccessor fieldAccessor = this.FieldAccessor;

            return(fieldAccessor.GetField(obj));
        }
Exemple #9
0
        public sealed override Delegate CreateDelegate(Type delegateType)
        {
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.MethodInfo_CreateDelegate(this, delegateType);
            }

            // Legacy: The only difference between calling CreateDelegate(type) and CreateDelegate(type, null) is that the former
            // disallows closed instance delegates for V1.1 backward compatibility.
            return(CreateDelegate(delegateType, null, allowClosedInstanceDelegates: false));
        }
        // V4.5 api - Creates open or closed delegates over static or instance methods.
        public sealed override Delegate CreateDelegate(Type delegateType, Object target)
        {
#if ENABLE_REFLECTION_TRACE
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.MethodInfo_CreateDelegate(this, delegateType, target);
            }
#endif

            return(CreateDelegateWorker(delegateType, target, allowClosed: true));
        }
Exemple #11
0
        public sealed override void SetValue(Object obj, Object value)
        {
#if ENABLE_REFLECTION_TRACE
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.FieldInfo_SetValue(this, obj, value);
            }
#endif

            FieldAccessor fieldAccessor = this.FieldAccessor;
            fieldAccessor.SetField(obj, value);
        }
Exemple #12
0
        public sealed override Object GetValue(Object obj)
        {
#if ENABLE_REFLECTION_TRACE
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.FieldInfo_GetValue(this, obj);
            }
#endif

            FieldAccessor fieldAccessor = this.FieldAccessor;
            return(fieldAccessor.GetField(obj));
        }
Exemple #13
0
        public sealed override Type MakeArrayType()
        {
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.Type_MakeArrayType(this);
            }

            // Do not implement this as a call to MakeArrayType(1) - they are not interchangable. MakeArrayType() returns a
            // vector type ("SZArray") while MakeArrayType(1) returns a multidim array of rank 1. These are distinct types
            // in the ECMA model and in CLR Reflection.
            return(ReflectionCoreNonPortable.GetArrayType(this));
        }
Exemple #14
0
        public sealed override Type GetType(String name, bool throwOnError, bool ignoreCase)
        {
#if ENABLE_REFLECTION_TRACE
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.Assembly_GetType(this, name);
            }
#endif

            if (name == null)
            {
                throw new ArgumentNullException();
            }
            if (name.Length == 0)
            {
                throw new ArgumentException();
            }

            TypeName typeName = TypeParser.ParseAssemblyQualifiedTypeName(name, throwOnError: throwOnError);
            if (typeName == null)
            {
                return(null);
            }
            if (typeName is AssemblyQualifiedTypeName)
            {
                if (throwOnError)
                {
                    throw new ArgumentException(SR.Argument_AssemblyGetTypeCannotSpecifyAssembly);  // Cannot specify an assembly qualifier in a typename passed to Assembly.GetType()
                }
                else
                {
                    return(null);
                }
            }

            CoreAssemblyResolver coreAssemblyResolver = RuntimeAssembly.GetRuntimeAssemblyIfExists;
            CoreTypeResolver     coreTypeResolver     =
                delegate(Assembly containingAssemblyIfAny, string coreTypeName)
            {
                if (containingAssemblyIfAny == null)
                {
                    return(GetTypeCore(coreTypeName, ignoreCase: ignoreCase));
                }
                else
                {
                    return(containingAssemblyIfAny.GetTypeCore(coreTypeName, ignoreCase: ignoreCase));
                }
            };
            GetTypeOptions getTypeOptions = new GetTypeOptions(coreAssemblyResolver, coreTypeResolver, throwOnError: throwOnError, ignoreCase: ignoreCase);

            return(typeName.ResolveType(this, getTypeOptions));
        }
Exemple #15
0
        public sealed override Type MakeArrayType(int rank)
        {
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.Type_MakeArrayType(this);
            }

            if (rank <= 0)
            {
                throw new IndexOutOfRangeException();
            }
            return(ReflectionCoreNonPortable.GetMultiDimArrayType(this, rank));
        }
Exemple #16
0
        public sealed override void SetValue(object obj, object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
        {
#if ENABLE_REFLECTION_TRACE
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.FieldInfo_SetValue(this, obj, value);
            }
#endif

            FieldAccessor fieldAccessor = this.FieldAccessor;
            BinderBundle  binderBundle  = binder.ToBinderBundle(invokeAttr, culture);
            fieldAccessor.SetField(obj, value, binderBundle);
        }
Exemple #17
0
        public sealed override Type MakeArrayType()
        {
#if ENABLE_REFLECTION_TRACE
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.TypeInfo_MakeArrayType(this);
            }
#endif

            // Do not implement this as a call to MakeArrayType(1) - they are not interchangable. MakeArrayType() returns a
            // vector type ("SZArray") while MakeArrayType(1) returns a multidim array of rank 1. These are distinct types
            // in the ECMA model and in CLR Reflection.
            return(this.GetArrayType());
        }
Exemple #18
0
        public sealed override void SetValue(object obj, object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
        {
#if ENABLE_REFLECTION_TRACE
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.FieldInfo_SetValue(this, obj, value);
            }
#endif

            binder.EnsureNotCustomBinder();

            FieldAccessor fieldAccessor = this.FieldAccessor;
            fieldAccessor.SetField(obj, value);
        }
Exemple #19
0
        public sealed override Object GetConstantValue()
        {
#if ENABLE_REFLECTION_TRACE
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.PropertyInfo_GetConstantValue(this);
            }
#endif
            Object defaultValue;
            if (!DefaultValueProcessing.GetDefaultValueIfAny(_reader, ref _property, this, out defaultValue))
            {
                throw new InvalidOperationException();
            }
            return(defaultValue);
        }
Exemple #20
0
        public sealed override Type MakeArrayType(int rank)
        {
#if ENABLE_REFLECTION_TRACE
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.TypeInfo_MakeArrayType(this, rank);
            }
#endif

            if (rank <= 0)
            {
                throw new IndexOutOfRangeException();
            }
            return(this.GetMultiDimArrayType(rank));
        }
Exemple #21
0
        public sealed override Object Invoke(Object obj, Object[] parameters)
        {
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.MethodBase_Invoke(this, obj, parameters);
            }

            if (parameters == null)
            {
                parameters = Array.Empty <Object>();
            }
            MethodInvoker methodInvoker = this.MethodInvoker;

            return(methodInvoker.Invoke(obj, parameters));
        }
        public sealed override Type GetType(String name, bool throwOnError, bool ignoreCase)
        {
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.Assembly_GetType(this, name);
            }

            if (name == null)
            {
                throw new ArgumentNullException();
            }
            if (name.Length == 0)
            {
                throw new ArgumentException();
            }

            AssemblyQualifiedTypeName assemblyQualifiedTypeName;

            try
            {
                assemblyQualifiedTypeName = TypeParser.ParseAssemblyQualifiedTypeName(name);
                if (assemblyQualifiedTypeName.AssemblyName != null)
                {
                    throw new ArgumentException(SR.Argument_AssemblyGetTypeCannotSpecifyAssembly);  // Cannot specify an assembly qualifier in a typename passed to Assembly.GetType()
                }
            }
            catch (ArgumentException)
            {
                if (throwOnError)
                {
                    throw;
                }
                return(null);
            }

            RuntimeType result;
            Exception   typeLoadException = assemblyQualifiedTypeName.TypeName.TryResolve(this.ReflectionDomain, this, ignoreCase, out result);

            if (typeLoadException != null)
            {
                if (throwOnError)
                {
                    throw typeLoadException;
                }
                return(null);
            }
            return(result);
        }
Exemple #23
0
        private object GetConstantValue(bool raw)
        {
#if ENABLE_REFLECTION_TRACE
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.PropertyInfo_GetConstantValue(this);
            }
#endif

            object defaultValue;
            if (!GetDefaultValueIfAny(raw, out defaultValue))
            {
                throw new InvalidOperationException(SR.Arg_EnumLitValueNotFound);
            }
            return(defaultValue);
        }
Exemple #24
0
        public sealed override PropertyInfo GetDeclaredProperty(String name)
        {
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.TypeInfo_GetDeclaredProperty(this, name);
            }

            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            TypeInfoCachedData cachedData = this.TypeInfoCachedData;

            return(cachedData.GetDeclaredProperty(name));
        }
Exemple #25
0
        public sealed override object Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
        {
#if ENABLE_REFLECTION_TRACE
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.MethodBase_Invoke(this, obj, parameters);
            }
#endif

            if (invokeAttr != BindingFlags.Default || binder != null || culture != null)
            {
                throw new NotImplementedException();
            }

            if (parameters == null)
            {
                parameters = Array.Empty <Object>();
            }
            MethodInvoker methodInvoker;
            try
            {
                methodInvoker = this.MethodInvoker;
            }
            catch (Exception)
            {
                //
                // Project N compat note: On the desktop, ConstructorInfo.Invoke(Object[]) specifically forbids invoking static constructors (and
                // for us, that check is embedded inside the MethodInvoker property call.) Howver, MethodBase.Invoke(Object, Object[]) allows it. This was
                // probably an oversight on the desktop. We choose not to support this loophole on Project N for the following reasons:
                //
                //  1. The Project N toolchain aggressively replaces static constructors with static initialization data whenever possible.
                //     So the static constructor may no longer exist.
                //
                //  2. Invoking the static constructor through Reflection is not very useful as it invokes the static constructor whether or not
                //     it was already run. Since static constructors are specifically one-shot deals, this will almost certainly mess up the
                //     type's internal assumptions.
                //

                if (this.IsStatic)
                {
                    throw new PlatformNotSupportedException(SR.Acc_NotClassInit);
                }
                throw;
            }

            return(methodInvoker.Invoke(obj, parameters));
        }
        public sealed override object Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
        {
#if ENABLE_REFLECTION_TRACE
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.MethodBase_Invoke(this, obj, parameters);
            }
#endif
            if (parameters == null)
            {
                parameters = Array.Empty <Object>();
            }
            MethodInvoker methodInvoker = this.MethodInvoker;
            object        result        = methodInvoker.Invoke(obj, parameters, binder, invokeAttr, culture);
            System.Diagnostics.DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
            return(result);
        }
Exemple #27
0
        public sealed override MethodInfo GetDeclaredMethod(String name)
        {
#if ENABLE_REFLECTION_TRACE
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.TypeInfo_GetDeclaredMethod(this, name);
            }
#endif

            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            TypeInfoCachedData cachedData = this.TypeInfoCachedData;
            return(cachedData.GetDeclaredMethod(name));
        }
        public sealed override void SetValue(object obj, object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
        {
#if ENABLE_REFLECTION_TRACE
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.FieldInfo_SetValue(this, obj, value);
            }
#endif

            if (invokeAttr != BindingFlags.Default || !(binder is DefaultBinder) || culture != null)
            {
                throw new NotImplementedException();
            }

            FieldAccessor fieldAccessor = this.FieldAccessor;
            fieldAccessor.SetField(obj, value);
        }
        public sealed override ParameterInfo[] GetParameters()
        {
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.MethodBase_GetParameters(this);
            }

            RuntimeParameterInfo[] runtimeParametersAndReturn = this.RuntimeParametersAndReturn;
            if (runtimeParametersAndReturn.Length == 1)
            {
                return(Array.Empty <ParameterInfo>());
            }
            ParameterInfo[] result = new ParameterInfo[runtimeParametersAndReturn.Length - 1];
            for (int i = 0; i < result.Length; i++)
            {
                result[i] = runtimeParametersAndReturn[i + 1];
            }
            return(result);
        }
Exemple #30
0
        public sealed override Type MakeGenericType(params Type[] instantiation)
        {
#if ENABLE_REFLECTION_TRACE
            if (ReflectionTrace.Enabled)
            {
                ReflectionTrace.Type_MakeGenericType(this, instantiation);
            }
#endif

            if (instantiation == null)
            {
                throw new ArgumentNullException("instantiation");
            }

            if (!(this.InternalIsGenericTypeDefinition))
            {
                throw new InvalidOperationException(SR.Format(SR.Arg_NotGenericTypeDefinition, this));
            }

            // We intentionally don't validate the number of arguments or their suitability to the generic type's constraints.
            // In a pay-for-play world, this can cause needless MissingMetadataExceptions. There is no harm in creating
            // the Type object for an inconsistent generic type - no EEType will ever match it so any attempt to "invoke" it
            // will throw an exception.
            RuntimeType[] genericTypeArguments = new RuntimeType[instantiation.Length];
            for (int i = 0; i < instantiation.Length; i++)
            {
                genericTypeArguments[i] = instantiation[i] as RuntimeType;
                if (genericTypeArguments[i] == null)
                {
                    if (instantiation[i] == null)
                    {
                        throw new ArgumentNullException();
                    }
                    else
                    {
                        throw new PlatformNotSupportedException(SR.PlatformNotSupported_MakeGenericType); // "PlatformNotSupported" because on desktop, passing in a foreign type is allowed and creates a RefEmit.TypeBuilder
                    }
                }
            }
            return(ReflectionCoreNonPortable.GetConstructedGenericType(this, genericTypeArguments));
        }