Esempio n. 1
0
 /// <summary>
 /// Adds an item to this collection.
 /// </summary>
 /// <param name="value">
 /// The item to be added.
 /// </param>
 /// <returns>
 /// The index of the added item.
 /// </returns>
 int System.Collections.IList.Add(object value)
 {
     ArgumentHelper.AssertNotNull(value, "value");
     ExceptionHelper.ThrowIf(!(value is TItem), "WrongType", value.GetType().FullName, typeof(TItem).FullName);
     Add((TItem)value);
     return(Count - 1);
 }
Esempio n. 2
0
        /// <summary>
        /// Copies all items in this collection to the specified array.
        /// </summary>
        /// <param name="array">
        /// The array to copy to.
        /// </param>
        /// <param name="arrayIndex">
        /// The starting index for the copy operation.
        /// </param>
        void System.Collections.ICollection.CopyTo(Array array, int index)
        {
            ArgumentHelper.AssertNotNull(array, "array");
            ExceptionHelper.ThrowIf(index < 0, "CopyTo.ArrayIndexNegative");

            for (int i = 0; i < this.Count; ++i)
            {
                array.SetValue(this[i], i + index);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Constructs an instance of <c>DispatchingCollection</c>.
        /// </summary>
        /// <param name="underlyingCollection">
        /// The collection being wrapped by this dispatching collection.
        /// </param>
        /// <param name="dispatcher">
        /// The <see cref="Dispatcher"/> to which <see cref="CollectionChanged"/> notifications will be marshalled.
        /// </param>
        public DispatchingCollection(TCollection underlyingCollection, Dispatcher dispatcher)
        {
            ArgumentHelper.AssertNotNull((object)underlyingCollection, "underlyingCollection");
            ArgumentHelper.AssertNotNull(dispatcher, "dispatcher");
            _underlyingCollection = underlyingCollection;
            _dispatcher           = dispatcher;

            _underlyingCollection.CollectionChanged += (sender, e) =>
            {
                OnCollectionChanged(e);
            };
        }
Esempio n. 4
0
 /// <summary>
 /// Gets or sets an item in this filtered collection.
 /// </summary>
 /// <param name="index">
 /// The index of the item.
 /// </param>
 /// <returns>
 /// The item at the specified index.
 /// </returns>
 object System.Collections.IList.this[int index]
 {
     get
     {
         return(this[index]);
     }
     set
     {
         ArgumentHelper.AssertNotNull(value, "value");
         ExceptionHelper.ThrowIf(!(value is TItem), "WrongType", value.GetType().FullName, typeof(TItem).FullName);
         this[index] = (TItem)value;
     }
 }
Esempio n. 5
0
        public static void Raise <T>(EventHandler <T> handler, object sender, CreateEventArguments <T> createEventArguments)
            where T : EventArgs
        {
            ArgumentHelper.AssertNotNull(createEventArguments, "createEventArguments");

            if (handler != null)
            {
#if DEBUG
                RaiseWithDiagnostics(handler, sender, createEventArguments());
#else
                handler(sender, createEventArguments());
#endif
            }
        }
Esempio n. 6
0
 /// <summary>
 /// Removes the specified item from this filtered collection.
 /// </summary>
 /// <param name="value">
 /// The item to remove.
 /// </param>
 /// <returns>
 /// <see langword="true"/> if the item was removed, otherwise <see langword="false"/>.
 /// </returns>
 void System.Collections.IList.Remove(object value)
 {
     ArgumentHelper.AssertNotNull(value, "value");
     ExceptionHelper.ThrowIf(!(value is TItem), "WrongType", value.GetType().FullName, typeof(TItem).FullName);
     Remove((TItem)value);
 }
Esempio n. 7
0
        public static void Throw(string exceptionKey, object[] constructorArgs, Exception innerException, params object[] messageArgs)
        {
            ArgumentHelper.AssertNotNull(exceptionKey, "exceptionKey");

            //first we need to find the type from which we were invoked - this is used as a grouping mechanism in the XML config file
            Type invokingType = null;
            int  skipFrames   = 1;

            while (true)
            {
                StackFrame stackFrame = new StackFrame(skipFrames);
                Debug.Assert(stackFrame.GetMethod() != null);

                if (stackFrame.GetMethod().DeclaringType != typeof(ExceptionHelper))
                {
                    invokingType = stackFrame.GetMethod().DeclaringType;
                    break;
                }

                ++skipFrames;
            }

            XmlDocument exceptionInfo = GetExceptionInfo(invokingType.Assembly);

            string  xpath         = string.Concat("/exceptionHelper/exceptionGroup[@type=\"", invokingType.FullName, "\"]/exception[@key=\"", exceptionKey, "\"]");
            XmlNode exceptionNode = exceptionInfo.SelectSingleNode(xpath);

            if (exceptionNode == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "The exception details for key '{0}' could not be found (they should be under '{1}')", exceptionKey, xpath));
            }

            XmlAttribute typeAttribute = exceptionNode.Attributes[_typeAttributeName];

            if (typeAttribute == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "The '{0}' attribute could not be found for exception with key '{1}'", _typeAttributeName, exceptionKey));
            }

            Type type = Type.GetType(typeAttribute.Value);

            if (type == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Type '{0}' could not be loaded for exception with key '{1}'", typeAttribute.Value, exceptionKey));
            }

            if (!typeof(Exception).IsAssignableFrom(type))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Type '{0}' for exception with key '{1}' does not inherit from '{2}'", type.FullName, exceptionKey, typeof(Exception).FullName));
            }

            string message = exceptionNode.InnerText.Trim();

            if ((messageArgs != null) && (messageArgs.Length > 0))
            {
                message = string.Format(CultureInfo.InvariantCulture, message, messageArgs);
            }

            List <object> constructorArgsList = new List <object>();

            //message is always first
            constructorArgsList.Add(message);

            //next, any additional constructor args
            if (constructorArgs != null)
            {
                constructorArgsList.AddRange(constructorArgs);
            }

            //finally, the inner exception, if any
            if (innerException != null)
            {
                constructorArgsList.Add(innerException);
            }

            object[]        constructorArgsArr = constructorArgsList.ToArray();
            BindingFlags    bindingFlags       = BindingFlags.Public | BindingFlags.Instance;
            object          state;
            ConstructorInfo constructor = null;

            try
            {
                constructor = (ConstructorInfo)Type.DefaultBinder.BindToMethod(bindingFlags, type.GetConstructors(bindingFlags), ref constructorArgsArr, null, null, null, out state);
            }
            catch (MissingMethodException)
            {
                //swallow and deal with below
            }

            if (constructor == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "An appropriate constructor could not be found for exception type '{0}, for exception with key '{1}'", type.FullName, exceptionKey));
            }

            //create the exception instance
            Exception e = (Exception)constructor.Invoke(constructorArgsArr);

            //finally, throw the exception
            throw e;
        }