Пример #1
0
            internal ActivatorCache(RuntimeType rt)
            {
                Debug.Assert(rt != null);

#if DEBUG
                _originalRuntimeType = rt;
#endif

                // The check below is redundant since these same checks are performed at the
                // unmanaged layer, but this call will throw slightly different exceptions
                // than the unmanaged layer, and callers might be dependent on this.

                rt.CreateInstanceCheckThis();

                try
                {
                    RuntimeTypeHandle.GetActivationInfo(rt,
                                                        out _pfnAllocator !, out _allocatorFirstArg,
                                                        out _pfnCtor !, out _ctorIsPublic);
                }
                catch (Exception ex)
                {
                    // Exception messages coming from the runtime won't include
                    // the type name. Let's include it here to improve the
                    // debugging experience for our callers.

                    string friendlyMessage = SR.Format(SR.Activator_CannotCreateInstance, rt, ex.Message);
                    switch (ex)
                    {
                    case ArgumentException: throw new ArgumentException(friendlyMessage);

                    case PlatformNotSupportedException: throw new PlatformNotSupportedException(friendlyMessage);

                    case NotSupportedException: throw new NotSupportedException(friendlyMessage);

                    case MethodAccessException: throw new MethodAccessException(friendlyMessage);

                    case MissingMethodException: throw new MissingMethodException(friendlyMessage);

                    case MemberAccessException: throw new MemberAccessException(friendlyMessage);
                    }

                    throw; // can't make a friendlier message, rethrow original exception
                }

                // Activator.CreateInstance returns null given typeof(Nullable<T>).

                if (_pfnAllocator == null)
                {
                    Debug.Assert(Nullable.GetUnderlyingType(rt) != null,
                                 "Null allocator should only be returned for Nullable<T>.");