示例#1
0
        public override object GetValue(object key, Type type, object defaultValue, IFormatProvider provider, OnNotFound onNotFound)
        {
            // get it...
            object value = this.GetValue(key);

            // do we have a type?
            if (value != null)
            {
                if (type != null)
                {
                    return(ConversionHelper.ChangeType(value, type, provider));
                }
                else
                {
                    return(value);
                }
            }
            else
            {
                switch (onNotFound)
                {
                case OnNotFound.ReturnNull:
                    return(defaultValue);

                case OnNotFound.ThrowException:
                    throw new InvalidOperationException(string.Format("An item with key '{0}' ({1}) was not found.", key, key.GetType()));

                default:
                    throw new NotSupportedException(string.Format("Cannot handle '{0}' ({1}).", onNotFound, onNotFound.GetType()));
                }
            }
        }
示例#2
0
        /// <summary>
        /// Gets the index of the given entity.
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="onNotFound"></param>
        /// <returns></returns>
        private int IndexOf(object entity, OnNotFound onNotFound)
        {
            if (entity == null)
            {
                // walk...
                for (int index = 0; index < this.InnerComboBox.Items.Count; index++)
                {
                    if (this.InnerComboBox.Items[index] is string)
                    {
                        return(index);
                    }
                }
            }
            else
            {
                // find...
                for (int index = 0; index < this.InnerComboBox.Items.Count; index++)
                {
                    object item = this.InnerComboBox.Items[index];
                    if (item is EntityListItem)
                    {
                        if (Entity.Equals(((EntityListItem)item), entity))
                        {
                            return(index);
                        }
                    }
                }
            }

            // nope...
            switch (onNotFound)
            {
            case OnNotFound.ReturnNull:
                return(-1);

            case OnNotFound.ThrowException:
                if (entity != null)
                {
                    throw new InvalidOperationException(string.Format("The item '{0}' was not found in the collection.", entity));
                }
                else
                {
                    throw new InvalidOperationException("The null item was not found in the collection.");
                }

            default:
                throw new NotSupportedException(string.Format("Cannot handle '{0}' ({1}).", onNotFound, onNotFound.GetType()));
            }
        }
        /// <summary>
        /// Gets the parameter with the given name.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public SqlStatementParameter GetParameter(string name, OnNotFound onNotFound)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (name.Length == 0)
            {
                throw new ArgumentOutOfRangeException("'name' is zero-length.");
            }

            // get...
            int index = this.IndexOf(name);

            if (index != -1)
            {
                return(this[index]);
            }
            else
            {
                switch (onNotFound)
                {
                case OnNotFound.ReturnNull:
                    return(null);

                case OnNotFound.ThrowException:
                    throw new InvalidOperationException(string.Format("A parameter with name '{0}' was not found.", name));

                default:
                    throw new NotSupportedException(string.Format("Cannot handle '{0}' ({1}).", onNotFound, onNotFound.GetType()));
                }
            }
        }
示例#4
0
        /// <summary>
        /// Gets the first found type.
        /// </summary>
        /// <param name="onNotFound"></param>
        /// <returns></returns>
        /// <remarks>This method has a strange name to stop a collision with <see cref="Object.GetType"></see>.</remarks>
        public Type GetFirstFoundType(OnNotFound onNotFound)
        {
            Type[] types = this.GetTypes();
            if (types == null)
            {
                throw new InvalidOperationException("types is null.");
            }

            // check...
            if (types.Length > 0)
            {
                return(types[0]);
            }
            else
            {
                switch (onNotFound)
                {
                case OnNotFound.ThrowException:
                    throw new InvalidOperationException("No types were found.");

                case OnNotFound.ReturnNull:
                    return(null);

                default:
                    throw new NotSupportedException(string.Format("Cannot handle '{0}' ({1}).", onNotFound, onNotFound.GetType()));
                }
            }
        }
示例#5
0
        /// <summary>
        /// Loads an icon from the given assembly.
        /// </summary>
        /// <param name="asm"></param>
        /// <param name="resourceName"></param>
        /// <param name="onNotFound"></param>
        /// <returns></returns>
        public static Icon GetIcon(Assembly asm, string resourceName, OnNotFound onNotFound)
        {
            if (asm == null)
            {
                throw new ArgumentNullException("asm");
            }
            if (resourceName == null)
            {
                throw new ArgumentNullException("resourceName");
            }
            if (resourceName.Length == 0)
            {
                throw new ArgumentOutOfRangeException("'resourceName' is zero-length.");
            }

            // open it...
            Stream stream = asm.GetManifestResourceStream(resourceName);

            if (stream != null)
            {
                return(new Icon(stream));
            }
            else
            {
                switch (onNotFound)
                {
                case OnNotFound.ReturnNull:
                    return(null);

                case OnNotFound.ThrowException:
                    throw new InvalidOperationException(string.Format("Failed to find resource '{0}' in assembly '{1}'.", resourceName, asm));

                default:
                    throw new NotSupportedException(string.Format("Cannot handle '{0}' ({1}).", onNotFound, onNotFound.GetType()));
                }
            }
        }
示例#6
0
        /// <summary>
        /// Gets the given value.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="type"></param>
        /// <param name="defaultValue"></param>
        /// <param name="onNotFound">In this case, <see cref="OnNotFound.ReturnNull"></see> means 'return default value'.</param>
        /// <returns></returns>
        public override object GetValue(string name, Type type, object defaultValue, IFormatProvider provider, OnNotFound onNotFound)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (name.Length == 0)
            {
                throw new ArgumentOutOfRangeException("'name' is zero-length.");
            }

            // mbr - 21-07-2006 - type can be null.
            if (type == null)
            {
//				throw new ArgumentNullException("type");
                type = typeof(object);
            }

            // ensure...
            this.EnsureInitialized();

            // look up the value...
            object key   = this.GetKey(name);
            object value = null;
            bool   found = false;

            if (this.InnerValues.Contains(key) == false)
            {
                // load it...
                value = this.LoadValue(name, ref found);

                // did we find it?
                if (found == false)
                {
                    value = MissingItem.Value;
                }

                // add...
                this.InnerValues[key] = value;
            }
            else
            {
                value = this.InnerValues[key];
            }

            // found?
            if (value is MissingItem)
            {
                // now what?
                switch (onNotFound)
                {
                case OnNotFound.ReturnNull:
                    value = defaultValue;
                    break;

                case OnNotFound.ThrowException:
                    throw new InvalidOperationException(string.Format(Cultures.Exceptions, "A value with name '{0}' was not found.", name));

                default:
                    throw new NotSupportedException(string.Format("Cannot handle '{0}' ({1}).", onNotFound, onNotFound.GetType()));
                }
            }

            // return it...
            if (type != typeof(object))
            {
                // were we asked for a type?
                if (typeof(Type).IsAssignableFrom(type) == true)
                {
                    if (value is Type)
                    {
                        return(value);
                    }

                    // value...
                    value = Convert.ToString(value);
                    if (value == null || ((string)value).Length == 0)
                    {
                        if (onNotFound == OnNotFound.ThrowException)
                        {
                            throw new InvalidOperationException(string.Format(Cultures.System, "The value for '{0}' was an empty string, and an empty string cannot be used to load a type.", name));
                        }
                        else
                        {
                            return(null);
                        }
                    }

                    // try and load it...
                    try
                    {
                        return(Type.GetType((string)value, true, true));
                    }
                    catch (Exception ex)
                    {
                        // mbr - 28-05-2006 - if we can't do it -- perhaps we're in the GAC?  look for the name...
                        string[] parts = ((string)value).Split(',');
                        if (parts.Length == 2)
                        {
                            if (parts[1].Trim().ToLower().StartsWith("BootFX.Common"))
                            {
                                AssemblyName asmName = this.GetType().Assembly.GetName();

                                // append...
                                StringBuilder builder = new StringBuilder();
                                builder.Append(value);
                                builder.Append(", Version=");
                                builder.Append(asmName.Version.ToString());
                                builder.Append(", Culture=neutral, PublicKeyToken=");
                                foreach (byte b in asmName.GetPublicKeyToken())
                                {
                                    builder.Append(b.ToString("x2"));
                                }

                                // try again...
                                try
                                {
                                    return(Type.GetType(builder.ToString(), true, true));
                                }
                                catch (Exception ex2)
                                {
                                    throw new InvalidOperationException(string.Format(Cultures.System, "Neither the value '{0}', nor the value '{1}' could be used to load a type.\r\n(Original exception: {2})",
                                                                                      value, builder, ex.Message), ex2);
                                }
                            }
                        }

                        // still failed?
                        throw new InvalidOperationException(string.Format(Cultures.System, "The value '{0}' cannot be used to load a type.", value), ex);
                    }
                }
                else
                {
                    // convert it...
                    return(ConversionHelper.ChangeType(value, type, provider, ConversionFlags.Safe));
                }
            }
            else
            {
                // just return the value...
                return(value);
            }
        }