Esempio n. 1
0
 //
 // This entry is targeted by the ILTransformer to implement Type.GetType()'s ability to detect the calling assembly and use it as
 // a default assembly name.
 //
 public static Type ExtensibleGetType(string typeName, string callingAssemblyName, Func<AssemblyName, Assembly> assemblyResolver, Func<Assembly, string, bool, Type> typeResolver, bool throwOnError, bool ignoreCase)
 {
     LowLevelListWithIList<String> defaultAssemblies = new LowLevelListWithIList<String>();
     defaultAssemblies.Add(callingAssemblyName);
     defaultAssemblies.AddRange(DefaultAssemblyNamesForGetType);
     return ReflectionCoreExecution.ExecutionDomain.GetType(typeName, assemblyResolver, typeResolver, throwOnError, ignoreCase, defaultAssemblies);
 }
Esempio n. 2
0
        //
        // If throwIfMissingMetadata is false, returns null rather than throwing a MissingMetadataException.
        //
        internal sealed override IList <CustomAttributeNamedArgument> GetNamedArguments(bool throwIfMissingMetadata)
        {
            LowLevelListWithIList <CustomAttributeNamedArgument> customAttributeNamedArguments = new LowLevelListWithIList <CustomAttributeNamedArgument>();

            foreach (NamedArgumentHandle namedArgumentHandle in _customAttribute.NamedArguments)
            {
                NamedArgument namedArgument             = namedArgumentHandle.GetNamedArgument(_reader);
                String        memberName                = namedArgument.Name.GetString(_reader);
                bool          isField                   = (namedArgument.Flags == NamedArgumentMemberKind.Field);
                CustomAttributeTypedArgument typedValue =
                    ParseFixedArgument(
                        _reader,
                        namedArgument.Value,
                        throwIfMissingMetadata,
                        delegate()
                {
                    // We got here because the custom attribute blob did not inclue type information. For named arguments, this is considered illegal metadata
                    // (ECMA always includes type info for named arguments.)
                    throw new BadImageFormatException();
                }
                        );
                if (typedValue.ArgumentType == null)
                {
                    Debug.Assert(!throwIfMissingMetadata);
                    return(null);
                }
                customAttributeNamedArguments.Add(ReflectionAugments.CreateCustomAttributeNamedArgument(this.AttributeType, memberName, isField, typedValue));
            }
            return(customAttributeNamedArguments);
        }
Esempio n. 3
0
        /// <summary>
        /// This callback gets called whenever a module gets registered. It adds the metadata reader
        /// for the new module to the available scopes. The lock in ExecutionEnvironmentImplementation ensures
        /// that this function may never be called concurrently so that we can assume that two threads
        /// never update the reader and scope list at the same time.
        /// </summary>
        /// <param name="moduleHandle">Module handle to register</param>
        private void RegisterModule(IntPtr moduleHandle)
        {
            MetadataReader newReader = _executionEnvironment.GetMetadataReaderForModule(moduleHandle);

            if (newReader == null)
            {
                return;
            }

            // Build new reader list
            LowLevelListWithIList <MetadataReader> readers = new LowLevelListWithIList <MetadataReader>(_readers.Count + 1);

            foreach (MetadataReader oldReader in _readers)
            {
                readers.Add(oldReader);
            }
            readers.Add(newReader);

            LowLevelDictionaryWithIEnumerable <string, ScopeDefinitionGroup> scopeGroups = new LowLevelDictionaryWithIEnumerable <string, ScopeDefinitionGroup>();

            foreach (KeyValuePair <string, ScopeDefinitionGroup> oldGroup in _scopeGroups)
            {
                scopeGroups.Add(oldGroup.Key, oldGroup.Value);
            }
            AddScopesFromReaderToGroups(scopeGroups, newReader);

            // Update reader and scope list
            _readers     = readers;
            _scopeGroups = scopeGroups;
        }
Esempio n. 4
0
 //
 // This entry is targeted by the ILTransformer to implement Type.GetType()'s ability to detect the calling assembly and use it as
 // a default assembly name.
 //
 public static Type GetType(String typeName, String callingAssemblyName, bool throwOnError, bool ignoreCase)
 {
     LowLevelListWithIList<String> defaultAssemblies = new LowLevelListWithIList<String>();
     defaultAssemblies.Add(callingAssemblyName);
     defaultAssemblies.AddRange(DefaultAssemblyNamesForGetType);
     return ReflectionCoreExecution.ExecutionDomain.GetType(typeName, throwOnError, ignoreCase, defaultAssemblies);
 }
Esempio n. 5
0
        //
        // Wrap a custom attribute argument (or an element of an array-typed custom attribute argument) in a CustomAttributeTypeArgument structure
        // for insertion into a CustomAttributeData value.
        //
        protected CustomAttributeTypedArgument WrapInCustomAttributeTypedArgument(Object value, Type argumentType)
        {
            if (argumentType.Equals(CommonRuntimeTypes.Object))
            {
                // If the declared attribute type is System.Object, we must report the type based on the runtime value.
                if (value == null)
                {
                    argumentType = CommonRuntimeTypes.String;  // Why is null reported as System.String? Because that's what the desktop CLR does.
                }
                else if (value is Type)
                {
                    argumentType = CommonRuntimeTypes.Type;    // value.GetType() will not actually be System.Type - rather it will be some internal implementation type. We only want to report it as System.Type.
                }
                else
                {
                    argumentType = value.GetType();
                }
            }

            // Handle the array case
            if (value is IEnumerable enumerableValue && !(value is string))
            {
                if (!argumentType.IsArray)
                {
                    throw new BadImageFormatException();
                }
                Type reportedElementType = argumentType.GetElementType();
                LowLevelListWithIList <CustomAttributeTypedArgument> elementTypedArguments = new LowLevelListWithIList <CustomAttributeTypedArgument>();
                foreach (Object elementValue in enumerableValue)
                {
                    CustomAttributeTypedArgument elementTypedArgument = WrapInCustomAttributeTypedArgument(elementValue, reportedElementType);
                    elementTypedArguments.Add(elementTypedArgument);
                }
                return(new CustomAttributeTypedArgument(argumentType, new ReadOnlyCollection <CustomAttributeTypedArgument>(elementTypedArguments)));
            }
Esempio n. 6
0
        /// <summary>
        /// Invokes a handler on each <see cref="T:System.Exception"/> contained by this <see
        /// cref="AggregateException"/>.
        /// </summary>
        /// <param name="predicate">The predicate to execute for each exception. The predicate accepts as an
        /// argument the <see cref="T:System.Exception"/> to be processed and returns a Boolean to indicate
        /// whether the exception was handled.</param>
        /// <remarks>
        /// Each invocation of the <paramref name="predicate"/> returns true or false to indicate whether the
        /// <see cref="T:System.Exception"/> was handled. After all invocations, if any exceptions went
        /// unhandled, all unhandled exceptions will be put into a new <see cref="AggregateException"/>
        /// which will be thrown. Otherwise, the <see cref="Handle"/> method simply returns. If any
        /// invocations of the <paramref name="predicate"/> throws an exception, it will halt the processing
        /// of any more exceptions and immediately propagate the thrown exception as-is.
        /// </remarks>
        /// <exception cref="AggregateException">An exception contained by this <see
        /// cref="AggregateException"/> was not handled.</exception>
        /// <exception cref="T:System.ArgumentNullException">The <paramref name="predicate"/> argument is
        /// null.</exception>
        public void Handle(Func <Exception, bool> predicate)
        {
            if (predicate == null)
            {
                throw new ArgumentNullException(nameof(predicate));
            }

            LowLevelListWithIList <Exception> unhandledExceptions = null;

            for (int i = 0; i < _innerExceptions.Count; i++)
            {
                // If the exception was not handled, lazily allocate a list of unhandled
                // exceptions (to be rethrown later) and add it.
                if (!predicate(_innerExceptions[i]))
                {
                    if (unhandledExceptions == null)
                    {
                        unhandledExceptions = new LowLevelListWithIList <Exception>();
                    }

                    unhandledExceptions.Add(_innerExceptions[i]);
                }
            }

            // If there are unhandled exceptions remaining, throw them.
            if (unhandledExceptions != null)
            {
                throw new AggregateException(Message, unhandledExceptions);
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Attempts to transition the underlying
        /// <see cref="T:System.Threading.Tasks.Task{TResult}"/> into the
        /// <see cref="System.Threading.Tasks.TaskStatus.Faulted">Faulted</see>
        /// state.
        /// </summary>
        /// <param name="exceptions">The collection of exceptions to bind to this <see
        /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
        /// <returns>True if the operation was successful; otherwise, false.</returns>
        /// <remarks>This operation will return false if the
        /// <see cref="T:System.Threading.Tasks.Task{TResult}"/> is already in one
        /// of the three final states:
        /// <see cref="System.Threading.Tasks.TaskStatus.RanToCompletion">RanToCompletion</see>,
        /// <see cref="System.Threading.Tasks.TaskStatus.Faulted">Faulted</see>, or
        /// <see cref="System.Threading.Tasks.TaskStatus.Canceled">Canceled</see>.
        /// </remarks>
        /// <exception cref="T:System.ArgumentNullException">The <paramref name="exceptions"/> argument is null.</exception>
        /// <exception cref="T:System.ArgumentException">There are one or more null elements in <paramref name="exceptions"/>.</exception>
        /// <exception cref="T:System.ArgumentException">The <paramref name="exceptions"/> collection is empty.</exception>
        /// <exception cref="T:System.ObjectDisposedException">The <see cref="Task"/> was disposed.</exception>
        public bool TrySetException(IEnumerable <Exception> exceptions)
        {
            if (exceptions == null)
            {
                throw new ArgumentNullException(nameof(exceptions));
            }

            LowLevelListWithIList <Exception> defensiveCopy = new LowLevelListWithIList <Exception>();

            foreach (Exception e in exceptions)
            {
                if (e == null)
                {
                    throw new ArgumentException(SR.TaskCompletionSourceT_TrySetException_NullException, nameof(exceptions));
                }
                defensiveCopy.Add(e);
            }

            if (defensiveCopy.Count == 0)
            {
                throw new ArgumentException(SR.TaskCompletionSourceT_TrySetException_NoExceptions, nameof(exceptions));
            }

            bool rval = m_task.TrySetException(defensiveCopy);

            if (!rval && !m_task.IsCompleted)
            {
                SpinUntilCompleted();
            }
            return(rval);
        }
        //
        // Foo or Foo+Inner or Foo[String] or Foo+Inner[String]
        //
        private NonQualifiedTypeName ParseNamedOrConstructedGenericTypeName()
        {
            NamedTypeName namedType = ParseNamedTypeName();

            // Because "[" is used both for generic arguments and array indexes, we must peek two characters deep.
            if (!(_lexer.Peek == TokenType.OpenSqBracket && (_lexer.PeekSecond == TokenType.Other || _lexer.PeekSecond == TokenType.OpenSqBracket)))
            {
                return(namedType);
            }
            else
            {
                _lexer.Skip();
                LowLevelListWithIList <TypeName> genericTypeArguments = new LowLevelListWithIList <TypeName>();
                for (;;)
                {
                    TypeName genericTypeArgument = ParseGenericTypeArgument();
                    genericTypeArguments.Add(genericTypeArgument);
                    TokenType token = _lexer.GetNextToken();
                    if (token == TokenType.CloseSqBracket)
                    {
                        break;
                    }
                    if (token != TokenType.Comma)
                    {
                        throw new ArgumentException();
                    }
                }

                return(new ConstructedGenericTypeName(namedType, genericTypeArguments));
            }
        }
Esempio n. 9
0
        //
        // This entry is targeted by the ILTransformer to implement Type.GetType()'s ability to detect the calling assembly and use it as
        // a default assembly name.
        //
        public static Type GetType(String typeName, String callingAssemblyName, bool throwOnError, bool ignoreCase)
        {
            LowLevelListWithIList <String> defaultAssemblies = new LowLevelListWithIList <String>();

            defaultAssemblies.Add(callingAssemblyName);
            defaultAssemblies.AddRange(DefaultAssemblyNamesForGetType);
            return(ReflectionCoreExecution.ExecutionDomain.GetType(typeName, throwOnError, ignoreCase, defaultAssemblies));
        }
Esempio n. 10
0
        //
        // This entry is targeted by the ILTransformer to implement Type.GetType()'s ability to detect the calling assembly and use it as
        // a default assembly name.
        //
        public static Type ExtensibleGetType(string typeName, string callingAssemblyName, Func <AssemblyName, Assembly> assemblyResolver, Func <Assembly, string, bool, Type> typeResolver, bool throwOnError, bool ignoreCase)
        {
            LowLevelListWithIList <string> defaultAssemblies = new LowLevelListWithIList <string>();

            defaultAssemblies.Add(callingAssemblyName);
            defaultAssemblies.Add(AssemblyBinder.DefaultAssemblyNameForGetType);
            return(ReflectionCoreExecution.ExecutionDomain.GetType(typeName, assemblyResolver, typeResolver, throwOnError, ignoreCase, defaultAssemblies));
        }
Esempio n. 11
0
            public void AddOverflowScope(QScopeDefinition overflowScope)
            {
                if (_overflowScopes == null)
                {
                    _overflowScopes = new LowLevelListWithIList <QScopeDefinition>();
                }

                _overflowScopes.Add(overflowScope);
            }
Esempio n. 12
0
        private ExecutionContext(ExecutionContext other)
        {
            m_localValues = new LowLevelDictionaryWithIEnumerable <IAsyncLocal, object>();
            foreach (KeyValuePair <IAsyncLocal, object> kvp in other.m_localValues)
            {
                m_localValues.Add(kvp.Key, kvp.Value);
            }

            m_localChangeNotifications = new LowLevelListWithIList <IAsyncLocal>(other.m_localChangeNotifications);
        }
Esempio n. 13
0
        private ExecutionContext(ExecutionContext other)
        {
            m_localValues = new LowLevelDictionaryWithIEnumerable<IAsyncLocal, object>();
            foreach (KeyValuePair<IAsyncLocal, object> kvp in other.m_localValues)
            {
                m_localValues.Add(kvp.Key, kvp.Value);
            }

            m_localChangeNotifications = new LowLevelListWithIList<IAsyncLocal>(other.m_localChangeNotifications);
        }
Esempio n. 14
0
        public sealed override Type GetType(string typeName, Func <AssemblyName, Assembly> assemblyResolver, Func <Assembly, string, bool, Type> typeResolver, bool throwOnError, bool ignoreCase, string defaultAssemblyName)
        {
            LowLevelListWithIList <string> defaultAssemblies = new LowLevelListWithIList <string>();

            if (defaultAssemblyName != null)
            {
                defaultAssemblies.Add(defaultAssemblyName);
            }
            defaultAssemblies.Add(AssemblyBinder.DefaultAssemblyNameForGetType);
            return(_executionDomain.GetType(typeName, assemblyResolver, typeResolver, throwOnError, ignoreCase, defaultAssemblies));
        }
 public sealed override Type GetType(string typeName, Func <AssemblyName, Assembly> assemblyResolver, Func <Assembly, string, bool, Type> typeResolver, bool throwOnError, bool ignoreCase, string defaultAssemblyName)
 {
     if (defaultAssemblyName == null)
     {
         return(_executionDomain.GetType(typeName, assemblyResolver, typeResolver, throwOnError, ignoreCase, ReflectionExecution.DefaultAssemblyNamesForGetType));
     }
     else
     {
         LowLevelListWithIList <String> defaultAssemblies = new LowLevelListWithIList <String>();
         defaultAssemblies.Add(defaultAssemblyName);
         defaultAssemblies.AddRange(ReflectionExecution.DefaultAssemblyNamesForGetType);
         return(_executionDomain.GetType(typeName, assemblyResolver, typeResolver, throwOnError, ignoreCase, defaultAssemblies));
     }
 }
Esempio n. 16
0
        /// <summary>
        /// Convert each Oid's value to an ASCII string, then create an unmanaged array of "numOids" LPSTR pointers, one for each Oid.
        /// "numOids" is the number of LPSTR pointers. This is normally the same as oids.Count, except in the case where a malicious caller
        /// appends to the OidCollection while this method is in progress. In such a case, this method guarantees only that this won't create
        /// an unmanaged buffer overflow condition.
        /// </summary>
        public static SafeHandle ToLpstrArray(this OidCollection oids, out int numOids)
        {
            if (oids == null || oids.Count == 0)
            {
                numOids = 0;
                return(SafeLocalAllocHandle.InvalidHandle);
            }

            // Copy the oid strings to a local list to prevent a security race condition where
            // the OidCollection or individual oids can be modified by another thread and
            // potentially cause a buffer overflow
            LowLevelListWithIList <byte[]> oidStrings = new LowLevelListWithIList <byte[]>();

            foreach (Oid oid in oids)
            {
                byte[] oidString = oid.ValueAsAscii();
                oidStrings.Add(oidString);
            }

            numOids = oidStrings.Count;
            unsafe
            {
                int allocationSize = checked (numOids * sizeof(void *));
                foreach (byte[] oidString in oidStrings)
                {
                    checked
                    {
                        allocationSize += oidString.Length + 1;
                    }
                }

                SafeLocalAllocHandle safeLocalAllocHandle = SafeLocalAllocHandle.Create(allocationSize);
                byte **pOidPointers = (byte **)(safeLocalAllocHandle.DangerousGetHandle());
                byte * pOidContents = (byte *)(pOidPointers + numOids);

                for (int i = 0; i < numOids; i++)
                {
                    pOidPointers[i] = pOidContents;
                    byte[] oidString = oidStrings[i];
                    Marshal.Copy(oidString, 0, new IntPtr(pOidContents), oidString.Length);
                    pOidContents[oidString.Length] = 0;
                    pOidContents += oidString.Length + 1;
                }

                return(safeLocalAllocHandle);
            }
        }
Esempio n. 17
0
        /// <summary>
        /// Convert each Oid's value to an ASCII string, then create an unmanaged array of "numOids" LPSTR pointers, one for each Oid.
        /// "numOids" is the number of LPSTR pointers. This is normally the same as oids.Count, except in the case where a malicious caller
        /// appends to the OidCollection while this method is in progress. In such a case, this method guarantees only that this won't create
        /// an unmanaged buffer overflow condition.
        /// </summary>
        public static SafeHandle ToLpstrArray(this OidCollection oids, out int numOids)
        {
            if (oids == null || oids.Count == 0)
            {
                numOids = 0;
                return SafeLocalAllocHandle.InvalidHandle;
            }

            // Copy the oid strings to a local list to prevent a security race condition where
            // the OidCollection or individual oids can be modified by another thread and
            // potentially cause a buffer overflow
            LowLevelListWithIList<byte[]> oidStrings = new LowLevelListWithIList<byte[]>();
            foreach (Oid oid in oids)
            {
                byte[] oidString = oid.ValueAsAscii();
                oidStrings.Add(oidString);
            }

            numOids = oidStrings.Count;
            unsafe
            {
                int allocationSize = checked(numOids * sizeof(void*));
                foreach (byte[] oidString in oidStrings)
                {
                    checked
                    {
                        allocationSize += oidString.Length + 1;
                    }
                }

                SafeLocalAllocHandle safeLocalAllocHandle = SafeLocalAllocHandle.Create(allocationSize);
                byte** pOidPointers = (byte**)(safeLocalAllocHandle.DangerousGetHandle());
                byte* pOidContents = (byte*)(pOidPointers + numOids);

                for (int i = 0; i < numOids; i++)
                {
                    pOidPointers[i] = pOidContents;
                    byte[] oidString = oidStrings[i];
                    Marshal.Copy(oidString, 0, new IntPtr(pOidContents), oidString.Length);
                    pOidContents[oidString.Length] = 0;
                    pOidContents += oidString.Length + 1;
                }

                return safeLocalAllocHandle;
            }
        }
        //
        // Wrap a custom attribute argument (or an element of an array-typed custom attribute argument) in a CustomAttributeTypeArgument structure
        // for insertion into a CustomAttributeData value.
        //
        private CustomAttributeTypedArgument WrapInCustomAttributeTypedArgument(Object value, Type argumentType)
        {
            // To support reflection domains other than the execution domain, we'll have to translate argumentType to one of the values off
            // _reflectionDomain.FoundationTypes rather than using the direct value of value.GetType(). It's unclear how to do this for
            // enum types. Cross that bridge if ever get to it.
            Debug.Assert(_reflectionDomain is ExecutionDomain);

            if (argumentType.Equals(typeof(Object)))
            {
                // If the declared attribute type is System.Object, we must report the type based on the runtime value.
                if (value == null)
                {
                    argumentType = typeof(String);  // Why is null reported as System.String? Because that's what the desktop CLR does.
                }
                else if (value is Type)
                {
                    argumentType = typeof(Type);    // value.GetType() will not actually be System.Type - rather it will be some internal implementation type. We only want to report it as System.Type.
                }
                else
                {
                    argumentType = value.GetType();
                }
            }

            Array arrayValue = value as Array;

            if (arrayValue != null)
            {
                if (!argumentType.IsArray)
                {
                    throw new BadImageFormatException();
                }
                Type reportedElementType = argumentType.GetElementType();
                LowLevelListWithIList <CustomAttributeTypedArgument> elementTypedArguments = new LowLevelListWithIList <CustomAttributeTypedArgument>();
                foreach (Object elementValue in arrayValue)
                {
                    CustomAttributeTypedArgument elementTypedArgument = WrapInCustomAttributeTypedArgument(elementValue, reportedElementType);
                    elementTypedArguments.Add(elementTypedArgument);
                }
                return(ExtensibleCustomAttributeData.CreateCustomAttributeTypedArgument(argumentType, new ReadOnlyCollection <CustomAttributeTypedArgument>(elementTypedArguments)));
            }
            else
            {
                return(ExtensibleCustomAttributeData.CreateCustomAttributeTypedArgument(argumentType, value));
            }
        }
Esempio n. 19
0
        /// <summary>
        /// Flattens an <see cref="AggregateException"/> instances into a single, new instance.
        /// </summary>
        /// <returns>A new, flattened <see cref="AggregateException"/>.</returns>
        /// <remarks>
        /// If any inner exceptions are themselves instances of
        /// <see cref="AggregateException"/>, this method will recursively flatten all of them. The
        /// inner exceptions returned in the new <see cref="AggregateException"/>
        /// will be the union of all of the the inner exceptions from exception tree rooted at the provided
        /// <see cref="AggregateException"/> instance.
        /// </remarks>
        public AggregateException Flatten()
        {
            // Initialize a collection to contain the flattened exceptions.
            LowLevelListWithIList <Exception> flattenedExceptions = new LowLevelListWithIList <Exception>();

            // Create a list to remember all aggregates to be flattened, this will be accessed like a FIFO queue
            LowLevelList <AggregateException> exceptionsToFlatten = new LowLevelList <AggregateException>();

            exceptionsToFlatten.Add(this);
            int nDequeueIndex = 0;

            // Continue removing and recursively flattening exceptions, until there are no more.
            while (exceptionsToFlatten.Count > nDequeueIndex)
            {
                // dequeue one from exceptionsToFlatten
                IList <Exception> currentInnerExceptions = exceptionsToFlatten[nDequeueIndex++].InnerExceptions;

                for (int i = 0; i < currentInnerExceptions.Count; i++)
                {
                    Exception currentInnerException = currentInnerExceptions[i];

                    if (currentInnerException == null)
                    {
                        continue;
                    }

                    AggregateException currentInnerAsAggregate = currentInnerException as AggregateException;

                    // If this exception is an aggregate, keep it around for later.  Otherwise,
                    // simply add it to the list of flattened exceptions to be returned.
                    if (currentInnerAsAggregate != null)
                    {
                        exceptionsToFlatten.Add(currentInnerAsAggregate);
                    }
                    else
                    {
                        flattenedExceptions.Add(currentInnerException);
                    }
                }
            }


            return(new AggregateException(Message, flattenedExceptions));
        }
        //
        // If throwIfMissingMetadata is false, returns null rather than throwing a MissingMetadataException.
        //
        internal sealed override IList <CustomAttributeNamedArgument> GetNamedArguments(bool throwIfMissingMetadata)
        {
            LowLevelListWithIList <CustomAttributeNamedArgument> customAttributeNamedArguments = new LowLevelListWithIList <CustomAttributeNamedArgument>();

            foreach (NamedArgumentHandle namedArgumentHandle in _customAttribute.NamedArguments)
            {
                NamedArgument namedArgument = namedArgumentHandle.GetNamedArgument(_reader);
                string        memberName    = namedArgument.Name.GetString(_reader);
                bool          isField       = (namedArgument.Flags == NamedArgumentMemberKind.Field);

                Exception?      exception    = null;
                RuntimeTypeInfo?argumentType = namedArgument.Type.TryResolve(_reader, new TypeContext(null, null), ref exception);
                if (argumentType == null)
                {
                    if (throwIfMissingMetadata)
                    {
                        throw exception !;
                    }
                    else
                    {
                        return(null);
                    }
                }

                object?   value;
                Exception e = namedArgument.Value.TryParseConstantValue(_reader, out value);
                if (e != null)
                {
                    if (throwIfMissingMetadata)
                    {
                        throw e;
                    }
                    else
                    {
                        return(null);
                    }
                }
                CustomAttributeTypedArgument typedValue = WrapInCustomAttributeTypedArgument(value, argumentType);

                customAttributeNamedArguments.Add(CreateCustomAttributeNamedArgument(this.AttributeType, memberName, isField, typedValue));
            }
            return(customAttributeNamedArguments);
        }
Esempio n. 21
0
        /// <summary>Gets all of the threads' values in a list.</summary>
        private LowLevelListWithIList <T> GetValuesAsList()
        {
            LowLevelListWithIList <T> valueList = new LowLevelListWithIList <T>();
            int id = ~m_idComplement;

            if (id == -1)
            {
                return(null);
            }

            // Walk over the linked list of slots and gather the values associated with this ThreadLocal instance.
            for (LinkedSlot linkedSlot = m_linkedSlot.Next; linkedSlot != null; linkedSlot = linkedSlot.Next)
            {
                // We can safely read linkedSlot.Value. Even if this ThreadLocal has been disposed in the meantime, the LinkedSlot
                // objects will never be assigned to another ThreadLocal instance.
                valueList.Add(linkedSlot.Value);
            }

            return(valueList);
        }
        // Equals/GetHashCode no need to override (they just implement reference equality but desktop never unified these things.)

        private void LoadArgumentInfo(bool throwIfMissingMetadata, out IList <CustomAttributeNamedArgument> namedArguments, out IList <CustomAttributeTypedArgument> fixedArguments, out bool metadataWasMissing)
        {
            LowLevelListWithIList <CustomAttributeNamedArgument> newNamedArguments = new LowLevelListWithIList <CustomAttributeNamedArgument>();
            LowLevelListWithIList <CustomAttributeTypedArgument> newFixedArguments = new LowLevelListWithIList <CustomAttributeTypedArgument>();
            ReflectionTypeProvider typeProvider = new ReflectionTypeProvider(throwIfMissingMetadata);

            CustomAttributeValue <RuntimeTypeInfo> customAttributeValue = _customAttribute.DecodeValue(typeProvider);

            foreach (CustomAttributeTypedArgument <RuntimeTypeInfo> fixedArgument in customAttributeValue.FixedArguments)
            {
                newFixedArguments.Add(WrapInCustomAttributeTypedArgument(fixedArgument.Value, fixedArgument.Type));
            }

            foreach (CustomAttributeNamedArgument <RuntimeTypeInfo> ecmaNamedArgument in customAttributeValue.NamedArguments)
            {
                bool isField = ecmaNamedArgument.Kind == CustomAttributeNamedArgumentKind.Field;
                CustomAttributeTypedArgument typedArgument = WrapInCustomAttributeTypedArgument(ecmaNamedArgument.Value, ecmaNamedArgument.Type);
                newNamedArguments.Add(ReflectionAugments.CreateCustomAttributeNamedArgument(this.AttributeType, ecmaNamedArgument.Name, isField, typedArgument));
            }

            if (newFixedArguments.Count == 0)
            {
                fixedArguments = Array.Empty <CustomAttributeTypedArgument>();
            }
            else
            {
                fixedArguments = newFixedArguments;
            }

            if (newNamedArguments.Count == 0)
            {
                namedArguments = Array.Empty <CustomAttributeNamedArgument>();
            }
            else
            {
                namedArguments = newNamedArguments;
            }

            metadataWasMissing = typeProvider.ExceptionOccurred;
        }
        //
        // If throwIfMissingMetadata is false, returns null rather than throwing a MissingMetadataException.
        //
        internal sealed override IList<CustomAttributeTypedArgument> GetConstructorArguments(bool throwIfMissingMetadata)
        {
            int index = 0;
            LowLevelList<Handle> lazyCtorTypeHandles = null;
            LowLevelListWithIList<CustomAttributeTypedArgument> customAttributeTypedArguments = new LowLevelListWithIList<CustomAttributeTypedArgument>();

            foreach (FixedArgumentHandle fixedArgumentHandle in _customAttribute.FixedArguments)
            {
                CustomAttributeTypedArgument customAttributeTypedArgument =
                    ParseFixedArgument(
                        _reader,
                        fixedArgumentHandle,
                        throwIfMissingMetadata,
                        delegate ()
                        {
                            // If we got here, the custom attribute blob lacked type information (this is actually the typical case.) We must fallback to
                            // parsing the constructor's signature to get the type info. 
                            if (lazyCtorTypeHandles == null)
                            {
                                IEnumerable<ParameterTypeSignatureHandle> parameterTypeSignatureHandles;
                                HandleType handleType = _customAttribute.Constructor.HandleType;
                                switch (handleType)
                                {
                                    case HandleType.QualifiedMethod:
                                        parameterTypeSignatureHandles = _customAttribute.Constructor.ToQualifiedMethodHandle(_reader).GetQualifiedMethod(_reader).Method.GetMethod(_reader).Signature.GetMethodSignature(_reader).Parameters;
                                        break;

                                    case HandleType.MemberReference:
                                        parameterTypeSignatureHandles = _customAttribute.Constructor.ToMemberReferenceHandle(_reader).GetMemberReference(_reader).Signature.ToMethodSignatureHandle(_reader).GetMethodSignature(_reader).Parameters;
                                        break;
                                    default:
                                        throw new BadImageFormatException();
                                }
                                LowLevelList<Handle> ctorTypeHandles = new LowLevelList<Handle>();
                                foreach (ParameterTypeSignatureHandle parameterTypeSignatureHandle in parameterTypeSignatureHandles)
                                {
                                    ctorTypeHandles.Add(parameterTypeSignatureHandle.GetParameterTypeSignature(_reader).Type);
                                }
                                lazyCtorTypeHandles = ctorTypeHandles;
                            }
                            Handle typeHandle = lazyCtorTypeHandles[index];
                            Exception exception = null;
                            RuntimeType argumentType = _reflectionDomain.TryResolve(_reader, typeHandle, new TypeContext(null, null), ref exception);
                            if (argumentType == null)
                            {
                                if (throwIfMissingMetadata)
                                    throw exception;
                                return null;
                            }
                            return argumentType;
                        }
                );

                if (customAttributeTypedArgument.ArgumentType == null)
                {
                    Debug.Assert(!throwIfMissingMetadata);
                    return null;
                }

                customAttributeTypedArguments.Add(customAttributeTypedArgument);
                index++;
            }

            return customAttributeTypedArguments;
        }
        public sealed override Object InvokeMember(
            String name, BindingFlags bindingFlags, Binder binder, Object target,
            Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
        {
            const BindingFlags MemberBindingMask       = (BindingFlags)0x000000FF;
            const BindingFlags InvocationMask          = (BindingFlags)0x0000FF00;
            const BindingFlags BinderGetSetProperty    = BindingFlags.GetProperty | BindingFlags.SetProperty;
            const BindingFlags BinderGetSetField       = BindingFlags.GetField | BindingFlags.SetField;
            const BindingFlags BinderNonFieldGetSet    = (BindingFlags)0x00FFF300;
            const BindingFlags BinderNonCreateInstance = BindingFlags.InvokeMethod | BinderGetSetField | BinderGetSetProperty;

            if (IsGenericParameter)
            {
                throw new InvalidOperationException(SR.Arg_GenericParameter);
            }

            #region Preconditions
            if ((bindingFlags & InvocationMask) == 0)
            {
                // "Must specify binding flags describing the invoke operation required."
                throw new ArgumentException(SR.Arg_NoAccessSpec, nameof(bindingFlags));
            }

            // Provide a default binding mask if none is provided
            if ((bindingFlags & MemberBindingMask) == 0)
            {
                bindingFlags |= BindingFlags.Instance | BindingFlags.Public;

                if ((bindingFlags & BindingFlags.CreateInstance) == 0)
                {
                    bindingFlags |= BindingFlags.Static;
                }
            }

            // There must not be more named parameters than provided arguments
            if (namedParams != null)
            {
                if (providedArgs != null)
                {
                    if (namedParams.Length > providedArgs.Length)
                    {
                        // "Named parameter array can not be bigger than argument array."
                        throw new ArgumentException(SR.Arg_NamedParamTooBig, nameof(namedParams));
                    }
                }
                else
                {
                    if (namedParams.Length != 0)
                    {
                        // "Named parameter array can not be bigger than argument array."
                        throw new ArgumentException(SR.Arg_NamedParamTooBig, nameof(namedParams));
                    }
                }
            }
            #endregion

            #region COM Interop
            if (target != null && target.GetType().IsCOMObject)
            {
                throw new PlatformNotSupportedException(SR.Arg_PlatformNotSupportedInvokeMemberCom);
            }
            #endregion

            #region Check that any named paramters are not null
            if (namedParams != null && Array.IndexOf(namedParams, null) != -1)
            {
                // "Named parameter value must not be null."
                throw new ArgumentException(SR.Arg_NamedParamNull, nameof(namedParams));
            }
            #endregion

            int argCnt = (providedArgs != null) ? providedArgs.Length : 0;

            #region Get a Binder
            if (binder == null)
            {
                binder = DefaultBinder;
            }

            bool bDefaultBinder = (binder == DefaultBinder);
            #endregion

            #region Delegate to Activator.CreateInstance
            if ((bindingFlags & BindingFlags.CreateInstance) != 0)
            {
                if ((bindingFlags & BindingFlags.CreateInstance) != 0 && (bindingFlags & BinderNonCreateInstance) != 0)
                {
                    // "Can not specify both CreateInstance and another access type."
                    throw new ArgumentException(SR.Arg_CreatInstAccess, nameof(bindingFlags));
                }

                return(Activator.CreateInstance(this, bindingFlags, binder, providedArgs, culture));
            }
            #endregion

            // PutDispProperty and\or PutRefDispProperty ==> SetProperty.
            if ((bindingFlags & (BindingFlags.PutDispProperty | BindingFlags.PutRefDispProperty)) != 0)
            {
                bindingFlags |= BindingFlags.SetProperty;
            }

            #region Name
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (name.Length == 0 || name.Equals(@"[DISPID=0]"))
            {
                name = GetDefaultMemberName();

                if (name == null)
                {
                    // in InvokeMember we always pretend there is a default member if none is provided and we make it ToString
                    name = "ToString";
                }
            }
            #endregion

            #region GetField or SetField
            bool IsGetField = (bindingFlags & BindingFlags.GetField) != 0;
            bool IsSetField = (bindingFlags & BindingFlags.SetField) != 0;

            if (IsGetField || IsSetField)
            {
                #region Preconditions
                if (IsGetField)
                {
                    if (IsSetField)
                    {
                        // "Can not specify both Get and Set on a field."
                        throw new ArgumentException(SR.Arg_FldSetGet, nameof(bindingFlags));
                    }

                    if ((bindingFlags & BindingFlags.SetProperty) != 0)
                    {
                        // "Can not specify both GetField and SetProperty."
                        throw new ArgumentException(SR.Arg_FldGetPropSet, nameof(bindingFlags));
                    }
                }
                else
                {
                    Debug.Assert(IsSetField);

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

                    if ((bindingFlags & BindingFlags.GetProperty) != 0)
                    {
                        // "Can not specify both SetField and GetProperty."
                        throw new ArgumentException(SR.Arg_FldSetPropGet, nameof(bindingFlags));
                    }

                    if ((bindingFlags & BindingFlags.InvokeMethod) != 0)
                    {
                        // "Can not specify Set on a Field and Invoke on a method."
                        throw new ArgumentException(SR.Arg_FldSetInvoke, nameof(bindingFlags));
                    }
                }
                #endregion

                #region Lookup Field
                FieldInfo   selFld = null;
                FieldInfo[] flds   = GetMember(name, MemberTypes.Field, bindingFlags) as FieldInfo[];

                Debug.Assert(flds != null);

                if (flds.Length == 1)
                {
                    selFld = flds[0];
                }
                else if (flds.Length > 0)
                {
                    selFld = binder.BindToField(bindingFlags, flds, IsGetField ? Empty.Value : providedArgs[0], culture);
                }
                #endregion

                if (selFld != null)
                {
                    #region Invocation on a field
                    if (selFld.FieldType.IsArray || Object.ReferenceEquals(selFld.FieldType, CommonRuntimeTypes.Array))
                    {
                        #region Invocation of an array Field
                        int idxCnt;

                        if ((bindingFlags & BindingFlags.GetField) != 0)
                        {
                            idxCnt = argCnt;
                        }
                        else
                        {
                            idxCnt = argCnt - 1;
                        }

                        if (idxCnt > 0)
                        {
                            // Verify that all of the index values are ints
                            int[] idx = new int[idxCnt];
                            for (int i = 0; i < idxCnt; i++)
                            {
                                try
                                {
                                    idx[i] = ((IConvertible)providedArgs[i]).ToInt32(null);
                                }
                                catch (InvalidCastException)
                                {
                                    throw new ArgumentException(SR.Arg_IndexMustBeInt);
                                }
                            }

                            // Set or get the value...
                            Array a = (Array)selFld.GetValue(target);

                            // Set or get the value in the array
                            if ((bindingFlags & BindingFlags.GetField) != 0)
                            {
                                return(a.GetValue(idx));
                            }
                            else
                            {
                                a.SetValue(providedArgs[idxCnt], idx);
                                return(null);
                            }
                        }
                        #endregion
                    }

                    if (IsGetField)
                    {
                        #region Get the field value
                        if (argCnt != 0)
                        {
                            throw new ArgumentException(SR.Arg_FldGetArgErr, nameof(bindingFlags));
                        }

                        return(selFld.GetValue(target));

                        #endregion
                    }
                    else
                    {
                        #region Set the field Value
                        if (argCnt != 1)
                        {
                            throw new ArgumentException(SR.Arg_FldSetArgErr, nameof(bindingFlags));
                        }

                        selFld.SetValue(target, providedArgs[0], bindingFlags, binder, culture);

                        return(null);

                        #endregion
                    }
                    #endregion
                }

                if ((bindingFlags & BinderNonFieldGetSet) == 0)
                {
                    throw new MissingFieldException(FullName, name);
                }
            }
            #endregion

            #region Property PreConditions
            // @Legacy - This is RTM behavior
            bool isGetProperty = (bindingFlags & BindingFlags.GetProperty) != 0;
            bool isSetProperty = (bindingFlags & BindingFlags.SetProperty) != 0;

            if (isGetProperty || isSetProperty)
            {
                #region Preconditions
                if (isGetProperty)
                {
                    Debug.Assert(!IsSetField);

                    if (isSetProperty)
                    {
                        throw new ArgumentException(SR.Arg_PropSetGet, nameof(bindingFlags));
                    }
                }
                else
                {
                    Debug.Assert(isSetProperty);

                    Debug.Assert(!IsGetField);

                    if ((bindingFlags & BindingFlags.InvokeMethod) != 0)
                    {
                        throw new ArgumentException(SR.Arg_PropSetInvoke, nameof(bindingFlags));
                    }
                }
                #endregion
            }
            #endregion

            MethodInfo[] finalists = null;
            MethodInfo   finalist  = null;

            #region BindingFlags.InvokeMethod
            if ((bindingFlags & BindingFlags.InvokeMethod) != 0)
            {
                #region Lookup Methods
                MethodInfo[] semiFinalists = GetMember(name, MemberTypes.Method, bindingFlags) as MethodInfo[];
                LowLevelListWithIList <MethodInfo> results = null;

                for (int i = 0; i < semiFinalists.Length; i++)
                {
                    MethodInfo semiFinalist = semiFinalists[i];
                    Debug.Assert(semiFinalist != null);

                    if (!semiFinalist.QualifiesBasedOnParameterCount(bindingFlags, CallingConventions.Any, new Type[argCnt]))
                    {
                        continue;
                    }

                    if (finalist == null)
                    {
                        finalist = semiFinalist;
                    }
                    else
                    {
                        if (results == null)
                        {
                            results = new LowLevelListWithIList <MethodInfo>(semiFinalists.Length);
                            results.Add(finalist);
                        }

                        results.Add(semiFinalist);
                    }
                }

                if (results != null)
                {
                    Debug.Assert(results.Count > 1);
                    finalists = new MethodInfo[results.Count];
                    results.CopyTo(finalists, 0);
                }
                #endregion
            }
            #endregion

            Debug.Assert(finalists == null || finalist != null);

            #region BindingFlags.GetProperty or BindingFlags.SetProperty
            if (finalist == null && isGetProperty || isSetProperty)
            {
                #region Lookup Property
                PropertyInfo[] semiFinalists = GetMember(name, MemberTypes.Property, bindingFlags) as PropertyInfo[];
                LowLevelListWithIList <MethodInfo> results = null;

                for (int i = 0; i < semiFinalists.Length; i++)
                {
                    MethodInfo semiFinalist = null;

                    if (isSetProperty)
                    {
                        semiFinalist = semiFinalists[i].GetSetMethod(true);
                    }
                    else
                    {
                        semiFinalist = semiFinalists[i].GetGetMethod(true);
                    }

                    if (semiFinalist == null)
                    {
                        continue;
                    }

                    BindingFlags expectedBindingFlags = semiFinalist.IsPublic ? BindingFlags.Public : BindingFlags.NonPublic;
                    if ((bindingFlags & expectedBindingFlags) != expectedBindingFlags)
                    {
                        continue;
                    }

                    if (!semiFinalist.QualifiesBasedOnParameterCount(bindingFlags, CallingConventions.Any, new Type[argCnt]))
                    {
                        continue;
                    }

                    if (finalist == null)
                    {
                        finalist = semiFinalist;
                    }
                    else
                    {
                        if (results == null)
                        {
                            results = new LowLevelListWithIList <MethodInfo>(semiFinalists.Length);
                            results.Add(finalist);
                        }

                        results.Add(semiFinalist);
                    }
                }

                if (results != null)
                {
                    Debug.Assert(results.Count > 1);
                    finalists = new MethodInfo[results.Count];
                    results.CopyTo(finalists, 0);
                }
                #endregion
            }
            #endregion

            if (finalist != null)
            {
                #region Invoke
                if (finalists == null &&
                    argCnt == 0 &&
                    finalist.GetParametersNoCopy().Length == 0 &&
                    (bindingFlags & BindingFlags.OptionalParamBinding) == 0)
                {
                    //if (useCache && argCnt == props[0].GetParameters().Length)
                    //    AddMethodToCache(name, bindingFlags, argCnt, providedArgs, props[0]);

                    return(finalist.Invoke(target, bindingFlags, binder, providedArgs, culture));
                }

                if (finalists == null)
                {
                    finalists = new MethodInfo[] { finalist }
                }
                ;

                if (providedArgs == null)
                {
                    providedArgs = Array.Empty <object>();
                }

                Object state = null;


                MethodBase invokeMethod = null;

                try { invokeMethod = binder.BindToMethod(bindingFlags, finalists, ref providedArgs, modifiers, culture, namedParams, out state); }
                catch (MissingMethodException) { }

                if (invokeMethod == null)
                {
                    throw new MissingMethodException(FullName, name);
                }

                //if (useCache && argCnt == invokeMethod.GetParameters().Length)
                //    AddMethodToCache(name, bindingFlags, argCnt, providedArgs, invokeMethod);

                Object result = ((MethodInfo)invokeMethod).Invoke(target, bindingFlags, binder, providedArgs, culture);

                if (state != null)
                {
                    binder.ReorderArgumentArray(ref providedArgs, state);
                }

                return(result);

                #endregion
            }

            throw new MissingMethodException(FullName, name);
        }
    }
 //
 // If throwIfMissingMetadata is false, returns null rather than throwing a MissingMetadataException.
 //
 internal sealed override IList<CustomAttributeNamedArgument> GetNamedArguments(bool throwIfMissingMetadata)
 {
     LowLevelListWithIList<CustomAttributeNamedArgument> customAttributeNamedArguments = new LowLevelListWithIList<CustomAttributeNamedArgument>();
     foreach (NamedArgumentHandle namedArgumentHandle in _customAttribute.NamedArguments)
     {
         NamedArgument namedArgument = namedArgumentHandle.GetNamedArgument(_reader);
         String memberName = namedArgument.Name.GetString(_reader);
         bool isField = (namedArgument.Flags == NamedArgumentMemberKind.Field);
         CustomAttributeTypedArgument typedValue =
             ParseFixedArgument(
                 _reader,
                 namedArgument.Value,
                 throwIfMissingMetadata,
                 delegate ()
                 {
                     // We got here because the custom attribute blob did not inclue type information. For named arguments, this is considered illegal metadata
                     // (ECMA always includes type info for named arguments.)
                     throw new BadImageFormatException();
                 }
         );
         if (typedValue.ArgumentType == null)
         {
             Debug.Assert(!throwIfMissingMetadata);
             return null;
         }
         customAttributeNamedArguments.Add(ExtensibleCustomAttributeData.CreateCustomAttributeNamedArgument(this.AttributeType, memberName, isField, typedValue));
     }
     return customAttributeNamedArguments;
 }
        //
        // Wrap a custom attribute argument (or an element of an array-typed custom attribute argument) in a CustomAttributeTypeArgument structure
        // for insertion into a CustomAttributeData value.
        //
        private CustomAttributeTypedArgument WrapInCustomAttributeTypedArgument(Object value, Type argumentType)
        {
            // To support reflection domains other than the execution domain, we'll have to translate argumentType to one of the values off
            // _reflectionDomain.FoundationTypes rather than using the direct value of value.GetType(). It's unclear how to do this for 
            // enum types. Cross that bridge if ever get to it. 
            Debug.Assert(_reflectionDomain is ExecutionDomain);

            if (argumentType.Equals(typeof(Object)))
            {
                // If the declared attribute type is System.Object, we must report the type based on the runtime value.
                if (value == null)
                    argumentType = typeof(String);  // Why is null reported as System.String? Because that's what the desktop CLR does.
                else if (value is Type)
                    argumentType = typeof(Type);    // value.GetType() will not actually be System.Type - rather it will be some internal implementation type. We only want to report it as System.Type.
                else
                    argumentType = value.GetType();
            }

            Array arrayValue = value as Array;
            if (arrayValue != null)
            {
                if (!argumentType.IsArray)
                    throw new BadImageFormatException();
                Type reportedElementType = argumentType.GetElementType();
                LowLevelListWithIList<CustomAttributeTypedArgument> elementTypedArguments = new LowLevelListWithIList<CustomAttributeTypedArgument>();
                foreach (Object elementValue in arrayValue)
                {
                    CustomAttributeTypedArgument elementTypedArgument = WrapInCustomAttributeTypedArgument(elementValue, reportedElementType);
                    elementTypedArguments.Add(elementTypedArgument);
                }
                return ExtensibleCustomAttributeData.CreateCustomAttributeTypedArgument(argumentType, new ReadOnlyCollection<CustomAttributeTypedArgument>(elementTypedArguments));
            }
            else
            {
                return ExtensibleCustomAttributeData.CreateCustomAttributeTypedArgument(argumentType, value);
            }
        }
Esempio n. 27
0
 private ExecutionContext()
 {
     m_localValues = new LowLevelDictionaryWithIEnumerable<IAsyncLocal, object>();
     m_localChangeNotifications = new LowLevelListWithIList<IAsyncLocal>();
 }
Esempio n. 28
0
        /// <summary>Adds the exception to the fault list.</summary>
        /// <param name="exceptionObject">The exception to store.</param>
        /// <remarks>
        /// Must be called under lock.
        /// </remarks>
        private void AddFaultException(object exceptionObject)
        {
            Debug.Assert(exceptionObject != null, "AddFaultException(): Expected a non-null exceptionObject");

            // Initialize the exceptions list if necessary.  The list should be non-null iff it contains exceptions.
            var exceptions = m_faultExceptions;

            if (exceptions == null)
            {
                m_faultExceptions = exceptions = new LowLevelListWithIList <ExceptionDispatchInfo>(1);
            }
            else
            {
                Debug.Assert(exceptions.Count > 0, "Expected existing exceptions list to have > 0 exceptions.");
            }

            // Handle Exception by capturing it into an ExceptionDispatchInfo and storing that
            var exception = exceptionObject as Exception;

            if (exception != null)
            {
                exceptions.Add(ExceptionDispatchInfo.Capture(exception));
            }
            else
            {
                // Handle ExceptionDispatchInfo by storing it into the list
                var edi = exceptionObject as ExceptionDispatchInfo;
                if (edi != null)
                {
                    exceptions.Add(edi);
                }
                else
                {
                    // Handle enumerables of exceptions by capturing each of the contained exceptions into an EDI and storing it
                    var exColl = exceptionObject as IEnumerable <Exception>;
                    if (exColl != null)
                    {
#if DEBUG
                        int numExceptions = 0;
#endif
                        foreach (var exc in exColl)
                        {
#if DEBUG
                            Debug.Assert(exc != null, "No exceptions should be null");
                            numExceptions++;
#endif
                            exceptions.Add(ExceptionDispatchInfo.Capture(exc));
                        }
#if DEBUG
                        Debug.Assert(numExceptions > 0, "Collection should contain at least one exception.");
#endif
                    }
                    else
                    {
                        // Handle enumerables of EDIs by storing them directly
                        var ediColl = exceptionObject as IEnumerable <ExceptionDispatchInfo>;
                        if (ediColl != null)
                        {
                            exceptions.AddRange(ediColl);
#if DEBUG
                            Debug.Assert(exceptions.Count > 0, "There should be at least one dispatch info.");
                            foreach (var tmp in exceptions)
                            {
                                Debug.Assert(tmp != null, "No dispatch infos should be null");
                            }
#endif
                        }
                        // Anything else is a programming error
                        else
                        {
                            throw new ArgumentException(SR.TaskExceptionHolder_UnknownExceptionType, "exceptionObject");
                        }
                    }
                }
            }

            if (exceptions.Count > 0)
            {
                MarkAsUnhandled();
            }
        }
Esempio n. 29
0
 private ExecutionContext()
 {
     m_localValues = new LowLevelDictionaryWithIEnumerable <IAsyncLocal, object>();
     m_localChangeNotifications = new LowLevelListWithIList <IAsyncLocal>();
 }
        //
        // If throwIfMissingMetadata is false, returns null rather than throwing a MissingMetadataException.
        //
        internal sealed override IList <CustomAttributeTypedArgument> GetConstructorArguments(bool throwIfMissingMetadata)
        {
            int index = 0;

            HandleCollection parameterTypeSignatureHandles;
            HandleType       handleType = _customAttribute.Constructor.HandleType;

            switch (handleType)
            {
            case HandleType.QualifiedMethod:
                parameterTypeSignatureHandles = _customAttribute.Constructor.ToQualifiedMethodHandle(_reader).GetQualifiedMethod(_reader).Method.GetMethod(_reader).Signature.GetMethodSignature(_reader).Parameters;
                break;

            case HandleType.MemberReference:
                parameterTypeSignatureHandles = _customAttribute.Constructor.ToMemberReferenceHandle(_reader).GetMemberReference(_reader).Signature.ToMethodSignatureHandle(_reader).GetMethodSignature(_reader).Parameters;
                break;

            default:
                throw new BadImageFormatException();
            }
            Handle[] ctorTypeHandles = parameterTypeSignatureHandles.ToArray();

            LowLevelListWithIList <CustomAttributeTypedArgument> customAttributeTypedArguments = new LowLevelListWithIList <CustomAttributeTypedArgument>();

            foreach (Handle fixedArgumentHandle in _customAttribute.FixedArguments)
            {
                Handle          typeHandle   = ctorTypeHandles[index];
                Exception?      exception    = null;
                RuntimeTypeInfo?argumentType = typeHandle.TryResolve(_reader, new TypeContext(null, null), ref exception);
                if (argumentType == null)
                {
                    if (throwIfMissingMetadata)
                    {
                        throw exception !;
                    }
                    return(null);
                }

                Exception e = fixedArgumentHandle.TryParseConstantValue(_reader, out object?value);
                CustomAttributeTypedArgument customAttributeTypedArgument;
                if (e != null)
                {
                    if (throwIfMissingMetadata)
                    {
                        throw e;
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    customAttributeTypedArgument = WrapInCustomAttributeTypedArgument(value, argumentType);
                }

                customAttributeTypedArguments.Add(customAttributeTypedArgument);
                index++;
            }

            return(customAttributeTypedArguments);
        }
        public override Object InvokeMember(
            String name, BindingFlags bindingFlags, Binder binder, Object target,
            Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
        {
            const BindingFlags MemberBindingMask = (BindingFlags)0x000000FF;
            const BindingFlags InvocationMask = (BindingFlags)0x0000FF00;
            const BindingFlags BinderGetSetProperty = BindingFlags.GetProperty | BindingFlags.SetProperty;
            const BindingFlags BinderGetSetField = BindingFlags.GetField | BindingFlags.SetField;
            const BindingFlags BinderNonFieldGetSet = (BindingFlags)0x00FFF300;
            const BindingFlags BinderNonCreateInstance = BindingFlags.InvokeMethod | BinderGetSetField | BinderGetSetProperty;

            if (IsGenericParameter)
                throw new InvalidOperationException(SR.Arg_GenericParameter);

            #region Preconditions
            if ((bindingFlags & InvocationMask) == 0)
                // "Must specify binding flags describing the invoke operation required."
                throw new ArgumentException(SR.Arg_NoAccessSpec, nameof(bindingFlags));

            // Provide a default binding mask if none is provided 
            if ((bindingFlags & MemberBindingMask) == 0)
            {
                bindingFlags |= BindingFlags.Instance | BindingFlags.Public;

                if ((bindingFlags & BindingFlags.CreateInstance) == 0)
                    bindingFlags |= BindingFlags.Static;
            }

            // There must not be more named parameters than provided arguments
            if (namedParams != null)
            {
                if (providedArgs != null)
                {
                    if (namedParams.Length > providedArgs.Length)
                        // "Named parameter array can not be bigger than argument array."
                        throw new ArgumentException(SR.Arg_NamedParamTooBig, nameof(namedParams));
                }
                else
                {
                    if (namedParams.Length != 0)
                        // "Named parameter array can not be bigger than argument array."
                        throw new ArgumentException(SR.Arg_NamedParamTooBig, nameof(namedParams));
                }
            }
            #endregion

            #region COM Interop
            if (target != null && target.GetType().IsCOMObject)
                throw new PlatformNotSupportedException(SR.Arg_PlatformNotSupportedInvokeMemberCom);
            #endregion

            #region Check that any named paramters are not null
            if (namedParams != null && Array.IndexOf(namedParams, null) != -1)
                // "Named parameter value must not be null."
                throw new ArgumentException(SR.Arg_NamedParamNull, nameof(namedParams));
            #endregion

            int argCnt = (providedArgs != null) ? providedArgs.Length : 0;

            #region Get a Binder
            if (binder == null)
                binder = DefaultBinder;

            bool bDefaultBinder = (binder == DefaultBinder);
            #endregion

            #region Delegate to Activator.CreateInstance
            if ((bindingFlags & BindingFlags.CreateInstance) != 0)
            {
                if ((bindingFlags & BindingFlags.CreateInstance) != 0 && (bindingFlags & BinderNonCreateInstance) != 0)
                    // "Can not specify both CreateInstance and another access type."
                    throw new ArgumentException(SR.Arg_CreatInstAccess, nameof(bindingFlags));

                return Activator.CreateInstance(this, bindingFlags, binder, providedArgs, culture);
            }
            #endregion

            // PutDispProperty and\or PutRefDispProperty ==> SetProperty.
            if ((bindingFlags & (BindingFlags.PutDispProperty | BindingFlags.PutRefDispProperty)) != 0)
                bindingFlags |= BindingFlags.SetProperty;

            #region Name
            if (name == null)
                throw new ArgumentNullException(nameof(name));

            if (name.Length == 0 || name.Equals(@"[DISPID=0]"))
            {
                name = GetDefaultMemberName();

                if (name == null)
                {
                    // in InvokeMember we always pretend there is a default member if none is provided and we make it ToString
                    name = "ToString";
                }
            }
            #endregion

            #region GetField or SetField
            bool IsGetField = (bindingFlags & BindingFlags.GetField) != 0;
            bool IsSetField = (bindingFlags & BindingFlags.SetField) != 0;

            if (IsGetField || IsSetField)
            {
                #region Preconditions
                if (IsGetField)
                {
                    if (IsSetField)
                        // "Can not specify both Get and Set on a field."
                        throw new ArgumentException(SR.Arg_FldSetGet, nameof(bindingFlags));

                    if ((bindingFlags & BindingFlags.SetProperty) != 0)
                        // "Can not specify both GetField and SetProperty."
                        throw new ArgumentException(SR.Arg_FldGetPropSet, nameof(bindingFlags));
                }
                else
                {
                    Debug.Assert(IsSetField);

                    if (providedArgs == null)
                        throw new ArgumentNullException(nameof(providedArgs));

                    if ((bindingFlags & BindingFlags.GetProperty) != 0)
                        // "Can not specify both SetField and GetProperty."
                        throw new ArgumentException(SR.Arg_FldSetPropGet, nameof(bindingFlags));

                    if ((bindingFlags & BindingFlags.InvokeMethod) != 0)
                        // "Can not specify Set on a Field and Invoke on a method."
                        throw new ArgumentException(SR.Arg_FldSetInvoke, nameof(bindingFlags));
                }
                #endregion

                #region Lookup Field
                FieldInfo selFld = null;
                FieldInfo[] flds = GetMember(name, MemberTypes.Field, bindingFlags) as FieldInfo[];

                Debug.Assert(flds != null);

                if (flds.Length == 1)
                {
                    selFld = flds[0];
                }
                else if (flds.Length > 0)
                {
                    selFld = binder.BindToField(bindingFlags, flds, IsGetField ? Empty.Value : providedArgs[0], culture);
                }
                #endregion

                if (selFld != null)
                {
                    #region Invocation on a field
                    if (selFld.FieldType.IsArray || Object.ReferenceEquals(selFld.FieldType, CommonRuntimeTypes.Array))
                    {
                        #region Invocation of an array Field
                        int idxCnt;

                        if ((bindingFlags & BindingFlags.GetField) != 0)
                        {
                            idxCnt = argCnt;
                        }
                        else
                        {
                            idxCnt = argCnt - 1;
                        }

                        if (idxCnt > 0)
                        {
                            // Verify that all of the index values are ints
                            int[] idx = new int[idxCnt];
                            for (int i = 0; i < idxCnt; i++)
                            {
                                try
                                {
                                    idx[i] = ((IConvertible)providedArgs[i]).ToInt32(null);
                                }
                                catch (InvalidCastException)
                                {
                                    throw new ArgumentException(SR.Arg_IndexMustBeInt);
                                }
                            }

                            // Set or get the value...
                            Array a = (Array)selFld.GetValue(target);

                            // Set or get the value in the array
                            if ((bindingFlags & BindingFlags.GetField) != 0)
                            {
                                return a.GetValue(idx);
                            }
                            else
                            {
                                a.SetValue(providedArgs[idxCnt], idx);
                                return null;
                            }
                        }
                        #endregion
                    }

                    if (IsGetField)
                    {
                        #region Get the field value
                        if (argCnt != 0)
                            throw new ArgumentException(SR.Arg_FldGetArgErr, nameof(bindingFlags));

                        return selFld.GetValue(target);
                        #endregion
                    }
                    else
                    {
                        #region Set the field Value
                        if (argCnt != 1)
                            throw new ArgumentException(SR.Arg_FldSetArgErr, nameof(bindingFlags));

                        selFld.SetValue(target, providedArgs[0], bindingFlags, binder, culture);

                        return null;
                        #endregion
                    }
                    #endregion
                }

                if ((bindingFlags & BinderNonFieldGetSet) == 0)
                    throw new MissingFieldException(FullName, name);
            }
            #endregion                    

            #region Property PreConditions
            // @Legacy - This is RTM behavior
            bool isGetProperty = (bindingFlags & BindingFlags.GetProperty) != 0;
            bool isSetProperty = (bindingFlags & BindingFlags.SetProperty) != 0;

            if (isGetProperty || isSetProperty)
            {
                #region Preconditions
                if (isGetProperty)
                {
                    Debug.Assert(!IsSetField);

                    if (isSetProperty)
                        throw new ArgumentException(SR.Arg_PropSetGet, nameof(bindingFlags));
                }
                else
                {
                    Debug.Assert(isSetProperty);

                    Debug.Assert(!IsGetField);

                    if ((bindingFlags & BindingFlags.InvokeMethod) != 0)
                        throw new ArgumentException(SR.Arg_PropSetInvoke, nameof(bindingFlags));
                }
                #endregion
            }
            #endregion

            MethodInfo[] finalists = null;
            MethodInfo finalist = null;

            #region BindingFlags.InvokeMethod
            if ((bindingFlags & BindingFlags.InvokeMethod) != 0)
            {
                #region Lookup Methods
                MethodInfo[] semiFinalists = GetMember(name, MemberTypes.Method, bindingFlags) as MethodInfo[];
                LowLevelListWithIList<MethodInfo> results = null;

                for (int i = 0; i < semiFinalists.Length; i++)
                {
                    MethodInfo semiFinalist = semiFinalists[i];
                    Debug.Assert(semiFinalist != null);

                    if (!semiFinalist.QualifiesBasedOnParameterCount(bindingFlags, CallingConventions.Any, new Type[argCnt]))
                        continue;

                    if (finalist == null)
                    {
                        finalist = semiFinalist;
                    }
                    else
                    {
                        if (results == null)
                        {
                            results = new LowLevelListWithIList<MethodInfo>(semiFinalists.Length);
                            results.Add(finalist);
                        }

                        results.Add(semiFinalist);
                    }
                }

                if (results != null)
                {
                    Debug.Assert(results.Count > 1);
                    finalists = new MethodInfo[results.Count];
                    results.CopyTo(finalists, 0);
                }
                #endregion
            }
            #endregion

            Debug.Assert(finalists == null || finalist != null);

            #region BindingFlags.GetProperty or BindingFlags.SetProperty
            if (finalist == null && isGetProperty || isSetProperty)
            {
                #region Lookup Property
                PropertyInfo[] semiFinalists = GetMember(name, MemberTypes.Property, bindingFlags) as PropertyInfo[];
                LowLevelListWithIList<MethodInfo> results = null;

                for (int i = 0; i < semiFinalists.Length; i++)
                {
                    MethodInfo semiFinalist = null;

                    if (isSetProperty)
                    {
                        semiFinalist = semiFinalists[i].GetSetMethod(true);
                    }
                    else
                    {
                        semiFinalist = semiFinalists[i].GetGetMethod(true);
                    }

                    if (semiFinalist == null)
                        continue;

                    BindingFlags expectedBindingFlags = semiFinalist.IsPublic ? BindingFlags.Public : BindingFlags.NonPublic;
                    if ((bindingFlags & expectedBindingFlags) != expectedBindingFlags)
                        continue;

                    if (!semiFinalist.QualifiesBasedOnParameterCount(bindingFlags, CallingConventions.Any, new Type[argCnt]))
                        continue;

                    if (finalist == null)
                    {
                        finalist = semiFinalist;
                    }
                    else
                    {
                        if (results == null)
                        {
                            results = new LowLevelListWithIList<MethodInfo>(semiFinalists.Length);
                            results.Add(finalist);
                        }

                        results.Add(semiFinalist);
                    }
                }

                if (results != null)
                {
                    Debug.Assert(results.Count > 1);
                    finalists = new MethodInfo[results.Count];
                    results.CopyTo(finalists, 0);
                }
                #endregion            
            }
            #endregion

            if (finalist != null)
            {
                #region Invoke
                if (finalists == null &&
                    argCnt == 0 &&
                    finalist.GetParametersNoCopy().Length == 0 &&
                    (bindingFlags & BindingFlags.OptionalParamBinding) == 0)
                {
                    //if (useCache && argCnt == props[0].GetParameters().Length)
                    //    AddMethodToCache(name, bindingFlags, argCnt, providedArgs, props[0]);

                    return finalist.Invoke(target, bindingFlags, binder, providedArgs, culture);
                }

                if (finalists == null)
                    finalists = new MethodInfo[] { finalist };

                if (providedArgs == null)
                    providedArgs = Array.Empty<object>();

                Object state = null;


                MethodBase invokeMethod = null;

                try { invokeMethod = binder.BindToMethod(bindingFlags, finalists, ref providedArgs, modifiers, culture, namedParams, out state); }
                catch (MissingMethodException) { }

                if (invokeMethod == null)
                    throw new MissingMethodException(FullName, name);

                //if (useCache && argCnt == invokeMethod.GetParameters().Length)
                //    AddMethodToCache(name, bindingFlags, argCnt, providedArgs, invokeMethod);

                Object result = ((MethodInfo)invokeMethod).Invoke(target, bindingFlags, binder, providedArgs, culture);

                if (state != null)
                    binder.ReorderArgumentArray(ref providedArgs, state);

                return result;
                #endregion
            }

            throw new MissingMethodException(FullName, name);
        }
Esempio n. 32
0
 public X509CertificateCollection()
 {
     _list = new LowLevelListWithIList <Object>();
 }
Esempio n. 33
0
 public OidCollection()
 {
     _list = new LowLevelListWithIList <Oid>();
 }
Esempio n. 34
0
        //
        // If throwIfMissingMetadata is false, returns null rather than throwing a MissingMetadataException.
        //
        internal sealed override IList <CustomAttributeTypedArgument> GetConstructorArguments(bool throwIfMissingMetadata)
        {
            int index = 0;
            LowLevelList <Handle> lazyCtorTypeHandles = null;
            LowLevelListWithIList <CustomAttributeTypedArgument> customAttributeTypedArguments = new LowLevelListWithIList <CustomAttributeTypedArgument>();

            foreach (FixedArgumentHandle fixedArgumentHandle in _customAttribute.FixedArguments)
            {
                CustomAttributeTypedArgument customAttributeTypedArgument =
                    ParseFixedArgument(
                        _reader,
                        fixedArgumentHandle,
                        throwIfMissingMetadata,
                        delegate()
                {
                    // If we got here, the custom attribute blob lacked type information (this is actually the typical case.) We must fallback to
                    // parsing the constructor's signature to get the type info.
                    if (lazyCtorTypeHandles == null)
                    {
                        IEnumerable <Handle> parameterTypeSignatureHandles;
                        HandleType handleType = _customAttribute.Constructor.HandleType;
                        switch (handleType)
                        {
                        case HandleType.QualifiedMethod:
                            parameterTypeSignatureHandles = _customAttribute.Constructor.ToQualifiedMethodHandle(_reader).GetQualifiedMethod(_reader).Method.GetMethod(_reader).Signature.GetMethodSignature(_reader).Parameters;
                            break;

                        case HandleType.MemberReference:
                            parameterTypeSignatureHandles = _customAttribute.Constructor.ToMemberReferenceHandle(_reader).GetMemberReference(_reader).Signature.ToMethodSignatureHandle(_reader).GetMethodSignature(_reader).Parameters;
                            break;

                        default:
                            throw new BadImageFormatException();
                        }
                        LowLevelList <Handle> ctorTypeHandles = new LowLevelList <Handle>(parameterTypeSignatureHandles);
                        lazyCtorTypeHandles = ctorTypeHandles;
                    }
                    Handle typeHandle            = lazyCtorTypeHandles[index];
                    Exception exception          = null;
                    RuntimeTypeInfo argumentType = typeHandle.TryResolve(_reader, new TypeContext(null, null), ref exception);
                    if (argumentType == null)
                    {
                        if (throwIfMissingMetadata)
                        {
                            throw exception;
                        }
                        return(null);
                    }
                    return(argumentType);
                }
                        );

                if (customAttributeTypedArgument.ArgumentType == null)
                {
                    Debug.Assert(!throwIfMissingMetadata);
                    return(null);
                }

                customAttributeTypedArguments.Add(customAttributeTypedArgument);
                index++;
            }

            return(customAttributeTypedArguments);
        }
Esempio n. 35
0
 public OidCollection()
 {
     _list = new LowLevelListWithIList<Oid>();
 }
            public void AddOverflowScope(QScopeDefinition overflowScope)
            {
                if (_overflowScopes == null)
                {
                    _overflowScopes = new LowLevelListWithIList<QScopeDefinition>();
                }

                _overflowScopes.Add(overflowScope);
            }
Esempio n. 37
0
        /// <summary>
        /// Invokes a handler on each <see cref="T:System.Exception"/> contained by this <see
        /// cref="AggregateException"/>.
        /// </summary>
        /// <param name="predicate">The predicate to execute for each exception. The predicate accepts as an
        /// argument the <see cref="T:System.Exception"/> to be processed and returns a Boolean to indicate
        /// whether the exception was handled.</param>
        /// <remarks>
        /// Each invocation of the <paramref name="predicate"/> returns true or false to indicate whether the
        /// <see cref="T:System.Exception"/> was handled. After all invocations, if any exceptions went
        /// unhandled, all unhandled exceptions will be put into a new <see cref="AggregateException"/>
        /// which will be thrown. Otherwise, the <see cref="Handle"/> method simply returns. If any
        /// invocations of the <paramref name="predicate"/> throws an exception, it will halt the processing
        /// of any more exceptions and immediately propagate the thrown exception as-is.
        /// </remarks>
        /// <exception cref="AggregateException">An exception contained by this <see
        /// cref="AggregateException"/> was not handled.</exception>
        /// <exception cref="T:System.ArgumentNullException">The <paramref name="predicate"/> argument is
        /// null.</exception>
        public void Handle(Func<Exception, bool> predicate)
        {
            if (predicate == null)
            {
                throw new ArgumentNullException(nameof(predicate));
            }

            LowLevelListWithIList<Exception> unhandledExceptions = null;
            for (int i = 0; i < m_innerExceptions.Count; i++)
            {
                // If the exception was not handled, lazily allocate a list of unhandled
                // exceptions (to be rethrown later) and add it.
                if (!predicate(m_innerExceptions[i]))
                {
                    if (unhandledExceptions == null)
                    {
                        unhandledExceptions = new LowLevelListWithIList<Exception>();
                    }

                    unhandledExceptions.Add(m_innerExceptions[i]);
                }
            }

            // If there are unhandled exceptions remaining, throw them.
            if (unhandledExceptions != null)
            {
                throw new AggregateException(Message, unhandledExceptions);
            }
        }
        /// <summary>
        /// This callback gets called whenever a module gets registered. It adds the metadata reader
        /// for the new module to the available scopes. The lock in ExecutionEnvironmentImplementation ensures
        /// that this function may never be called concurrently so that we can assume that two threads
        /// never update the reader and scope list at the same time.
        /// </summary>
        /// <param name="moduleHandle">Module handle to register</param>
        private void RegisterModule(IntPtr moduleHandle)
        {
            MetadataReader newReader = _executionEnvironment.GetMetadataReaderForModule(moduleHandle);
            if (newReader == null)
            {
                return;
            }

            // Build new reader list
            LowLevelListWithIList<MetadataReader> readers = new LowLevelListWithIList<MetadataReader>(_readers.Count + 1);
            foreach (MetadataReader oldReader in _readers)
            {
                readers.Add(oldReader);
            }
            readers.Add(newReader);

            LowLevelDictionaryWithIEnumerable<string, ScopeDefinitionGroup> scopeGroups = new LowLevelDictionaryWithIEnumerable<string, ScopeDefinitionGroup>();
            foreach (KeyValuePair<string, ScopeDefinitionGroup> oldGroup in _scopeGroups)
            {
                scopeGroups.Add(oldGroup.Key, oldGroup.Value);
            }
            AddScopesFromReaderToGroups(scopeGroups, newReader);

            // Update reader and scope list
            _readers = readers;
            _scopeGroups = scopeGroups;
        }
Esempio n. 39
0
        /// <summary>
        /// Flattens an <see cref="AggregateException"/> instances into a single, new instance.
        /// </summary>
        /// <returns>A new, flattened <see cref="AggregateException"/>.</returns>
        /// <remarks>
        /// If any inner exceptions are themselves instances of
        /// <see cref="AggregateException"/>, this method will recursively flatten all of them. The
        /// inner exceptions returned in the new <see cref="AggregateException"/>
        /// will be the union of all of the the inner exceptions from exception tree rooted at the provided
        /// <see cref="AggregateException"/> instance.
        /// </remarks>
        public AggregateException Flatten()
        {
            // Initialize a collection to contain the flattened exceptions.
            LowLevelListWithIList<Exception> flattenedExceptions = new LowLevelListWithIList<Exception>();

            // Create a list to remember all aggregates to be flattened, this will be accessed like a FIFO queue
            LowLevelList<AggregateException> exceptionsToFlatten = new LowLevelList<AggregateException>();
            exceptionsToFlatten.Add(this);
            int nDequeueIndex = 0;

            // Continue removing and recursively flattening exceptions, until there are no more.
            while (exceptionsToFlatten.Count > nDequeueIndex)
            {
                // dequeue one from exceptionsToFlatten
                IList<Exception> currentInnerExceptions = exceptionsToFlatten[nDequeueIndex++].InnerExceptions;

                for (int i = 0; i < currentInnerExceptions.Count; i++)
                {
                    Exception currentInnerException = currentInnerExceptions[i];

                    if (currentInnerException == null)
                    {
                        continue;
                    }

                    AggregateException currentInnerAsAggregate = currentInnerException as AggregateException;

                    // If this exception is an aggregate, keep it around for later.  Otherwise,
                    // simply add it to the list of flattened exceptions to be returned.
                    if (currentInnerAsAggregate != null)
                    {
                        exceptionsToFlatten.Add(currentInnerAsAggregate);
                    }
                    else
                    {
                        flattenedExceptions.Add(currentInnerException);
                    }
                }
            }


            return new AggregateException(Message, flattenedExceptions);
        }
Esempio n. 40
0
 public AssemblyBinderImplementation(ExecutionEnvironmentImplementation executionEnvironment)
 {
     _executionEnvironment = executionEnvironment;
     _readers     = new LowLevelListWithIList <MetadataReader>();
     _scopeGroups = new LowLevelDictionaryWithIEnumerable <string, ScopeDefinitionGroup>();
 }
 public AssemblyBinderImplementation(ExecutionEnvironmentImplementation executionEnvironment)
 {
     _executionEnvironment = executionEnvironment;
     _readers = new LowLevelListWithIList<MetadataReader>();
     _scopeGroups = new LowLevelDictionaryWithIEnumerable<string, ScopeDefinitionGroup>();
 }
Esempio n. 42
0
        //
        // Foo or Foo+Inner or Foo[String] or Foo+Inner[String]
        //
        private NonQualifiedTypeName ParseNamedOrConstructedGenericTypeName()
        {
            NamedTypeName namedType = ParseNamedTypeName();
            // Because "[" is used both for generic arguments and array indexes, we must peek two characters deep.
            if (!(_lexer.Peek == TokenType.OpenSqBracket && (_lexer.PeekSecond == TokenType.Other || _lexer.PeekSecond == TokenType.OpenSqBracket)))
                return namedType;
            else
            {
                _lexer.Skip();
                LowLevelListWithIList<TypeName> genericTypeArguments = new LowLevelListWithIList<TypeName>();
                for (;;)
                {
                    TypeName genericTypeArgument = ParseGenericTypeArgument();
                    genericTypeArguments.Add(genericTypeArgument);
                    TokenType token = _lexer.GetNextToken();
                    if (token == TokenType.CloseSqBracket)
                        break;
                    if (token != TokenType.Comma)
                        throw new ArgumentException();
                }

                return new ConstructedGenericTypeName(namedType, genericTypeArguments);
            }
        }
Esempio n. 43
0
 public X509CertificateCollection()
 {
     _list = new LowLevelListWithIList<Object>();
 }
        /// <summary>Adds the exception to the fault list.</summary>
        /// <param name="exceptionObject">The exception to store.</param>
        /// <remarks>
        /// Must be called under lock.
        /// </remarks>
        private void AddFaultException(object exceptionObject)
        {
            Contract.Requires(exceptionObject != null, "AddFaultException(): Expected a non-null exceptionObject");

            // Initialize the exceptions list if necessary.  The list should be non-null iff it contains exceptions.
            var exceptions = m_faultExceptions;
            if (exceptions == null) m_faultExceptions = exceptions = new LowLevelListWithIList<ExceptionDispatchInfo>(1);
            else Debug.Assert(exceptions.Count > 0, "Expected existing exceptions list to have > 0 exceptions.");

            // Handle Exception by capturing it into an ExceptionDispatchInfo and storing that
            var exception = exceptionObject as Exception;
            if (exception != null)
            {
                exceptions.Add(ExceptionDispatchInfo.Capture(exception));
            }
            else
            {
                // Handle ExceptionDispatchInfo by storing it into the list
                var edi = exceptionObject as ExceptionDispatchInfo;
                if (edi != null)
                {
                    exceptions.Add(edi);
                }
                else
                {
                    // Handle enumerables of exceptions by capturing each of the contained exceptions into an EDI and storing it
                    var exColl = exceptionObject as IEnumerable<Exception>;
                    if (exColl != null)
                    {
#if DEBUG
                        int numExceptions = 0;
#endif
                        foreach (var exc in exColl)
                        {
#if DEBUG
                            Debug.Assert(exc != null, "No exceptions should be null");
                            numExceptions++;
#endif
                            exceptions.Add(ExceptionDispatchInfo.Capture(exc));
                        }
#if DEBUG
                        Debug.Assert(numExceptions > 0, "Collection should contain at least one exception.");
#endif
                    }
                    else
                    {
                        // Handle enumerables of EDIs by storing them directly
                        var ediColl = exceptionObject as IEnumerable<ExceptionDispatchInfo>;
                        if (ediColl != null)
                        {
                            exceptions.AddRange(ediColl);
#if DEBUG
                            Debug.Assert(exceptions.Count > 0, "There should be at least one dispatch info.");
                            foreach (var tmp in exceptions)
                            {
                                Debug.Assert(tmp != null, "No dispatch infos should be null");
                            }
#endif
                        }
                        // Anything else is a programming error
                        else
                        {
                            throw new ArgumentException(SR.TaskExceptionHolder_UnknownExceptionType, "exceptionObject");
                        }
                    }
                }
            }

            if (exceptions.Count > 0)
                MarkAsUnhandled();
        }
        //
        // Wrap a custom attribute argument (or an element of an array-typed custom attribute argument) in a CustomAttributeTypeArgument structure
        // for insertion into a CustomAttributeData value.
        //
        private CustomAttributeTypedArgument WrapInCustomAttributeTypedArgument(Object value, Type argumentType)
        {
            if (argumentType.Equals(typeof(Object)))
            {
                // If the declared attribute type is System.Object, we must report the type based on the runtime value.
                if (value == null)
                    argumentType = typeof(String);  // Why is null reported as System.String? Because that's what the desktop CLR does.
                else if (value is Type)
                    argumentType = typeof(Type);    // value.GetType() will not actually be System.Type - rather it will be some internal implementation type. We only want to report it as System.Type.
                else
                    argumentType = value.GetType();
            }

            // Handle the array case
            IEnumerable enumerableValue = value as IEnumerable;
            if (enumerableValue != null && !(value is String))
            {
                if (!argumentType.IsArray)
                    throw new BadImageFormatException();
                Type reportedElementType = argumentType.GetElementType();
                LowLevelListWithIList<CustomAttributeTypedArgument> elementTypedArguments = new LowLevelListWithIList<CustomAttributeTypedArgument>();
                foreach (Object elementValue in enumerableValue)
                {
                    CustomAttributeTypedArgument elementTypedArgument = WrapInCustomAttributeTypedArgument(elementValue, reportedElementType);
                    elementTypedArguments.Add(elementTypedArgument);
                }
                return new CustomAttributeTypedArgument(argumentType, new ReadOnlyCollection<CustomAttributeTypedArgument>(elementTypedArguments));
            }
            else
            {
                return new CustomAttributeTypedArgument(argumentType, value);
            }
        }