Пример #1
0
 /// <summary>
 /// Creates a new instance of PropertyEqualityComparer.
 /// </summary>
 ///
 /// <param name="propertyName">
 /// The name of the property on type T to perform the comparison on.
 /// </param>
 public PropertyEqualityComparer(string propertyName)
 {
     _propertyInfo = typeof(T).GetProperty(propertyName, BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public);
     if (_propertyInfo == null)
     {
         ThrowException.ThrowArgumentException(
             string.Format("{0} is not a property of type {1}.", propertyName, typeof(T)));
     }
 }
Пример #2
0
        /// <summary>
        /// Adds an object instance in the multiton stored instances.
        /// </summary>
        ///
        /// <param name="key">
        /// Instance key.
        /// </param>
        ///
        /// <param name="instance">
        /// Object instance.
        /// </param>
        public void Add(string key, object instance)
        {
            lock (_locker)
            {
                if (_container.ContainsKey(key))
                {
                    ThrowException.ThrowArgumentException("There is already an instance registered using key = '{0}'", key);
                }

                _container.TryAdd(key, instance);
            }
        }
Пример #3
0
        /// <summary>
        /// Gets the fullname of the queue (".\private$\nameQueue").
        /// </summary>
        ///
        /// <param name="queueName">
        /// Name of the queue (".\private$\nameQueue" or "nameQueue").
        /// </param>
        ///
        /// <returns>
        /// The fullname of the queue.
        /// </returns>
        public static string GetQueueFullName(string queueName)
        {
            if (string.IsNullOrEmpty(queueName))
            {
                ThrowException.ThrowArgumentException("queueName");
            }

            if (!queueName.ToLowerInvariant().Contains(@"\private$\"))
            {
                queueName = string.Format(@".\private$\{0}", queueName);
            }

            return(queueName);
        }
Пример #4
0
        /// <summary>
        /// Creates the new queue if not exists.
        /// </summary>
        ///
        /// <param name="queueName">
        /// Name of the queue (".\private$\mailQueue" or "nameQueue").
        /// </param>
        ///
        /// <param name="isTransactional">
        /// Value indicating whether the queue is transactional.
        /// </param>
        ///
        /// <returns>
        /// The MessageQueue instance.
        /// </returns>
        public static MessageQueue CreateQueue(string queueName, bool isTransactional = false)
        {
            if (string.IsNullOrEmpty(queueName))
            {
                ThrowException.ThrowArgumentException("queueName");
            }

            queueName = GetQueueFullName(queueName);

            if (MessageQueue.Exists(queueName))
            {
                return(new MessageQueue(queueName));
            }

            return(MessageQueue.Create(queueName, isTransactional));
        }
Пример #5
0
        /// <summary>
        /// Serialize an object to a Xml representation using SerializerType.Xml (DOES NOT SUPPORT CIRCULAR REFERENCES) or SerializerType.DataContract (supports circular references).
        /// </summary>
        ///
        /// <param name="serializerType">
        /// Serializer to use.
        /// </param>
        ///
        /// <param name="source">
        /// Object to serialize.
        /// </param>
        ///
        /// <returns>
        /// The Xml representation.
        /// </returns>
        public static string ToXml(SerializerType serializerType, object source)
        {
            if (source == null)
            {
                ThrowException.ThrowArgumentNullException("source");
            }

            if (serializerType != SerializerType.Xml &&
                serializerType != SerializerType.DataContract)
            {
                ThrowException.ThrowArgumentException(string.Format("The '{0}' serializer is not supported in this context", serializerType));
            }

            if (serializerType == SerializerType.DataContract)
            {
                var mStream = new MemoryStream();                 // no using -> CA2202 Do not dispose objects multiple times Object 'mStream' can be disposed more than once in method 'SerializerHelper.ToXml(XmlSerializerType, object)'.

                using (var reader = new StreamReader(mStream))
                {
                    var dcSerializer = new DataContractSerializer(source.GetType());
                    dcSerializer.WriteObject(mStream, source);
                    mStream.Position = 0;

                    return(reader.ReadToEnd());
                }
            }
            else
            {
                var xmlSerializer = new XmlSerializer(source.GetType());
                using (var writer = new StringWriter())
                {
                    // NOTE: if you get "A circular reference was detected while serializing an object of type 'Entity'"
                    //       then use XmlSerializerType.DataContract instead.

                    xmlSerializer.Serialize(writer, source);
                    return(writer.ToString());
                }
            }
        }
Пример #6
0
        /// <summary>
        /// Returns the mime type of the specified file.
        /// </summary>
        ///
        /// <param name="file">
        /// File path.
        /// </param>
        ///
        /// <returns>
        /// The mime type of the file path (the return value can be null).
        /// </returns>
        public static string GetMimeType(string file)
        {
            if (string.IsNullOrWhiteSpace(file))
            {
                ThrowException.ThrowArgumentNullException("file");
            }

            string filePath  = Path.GetFullPath(file);
            string extension = Path.GetExtension(filePath);

            if (extension == null || extension.Length == 0)
            {
                ThrowException.ThrowArgumentException(
                    "file",
                    string.Format("The '{0}' file must have an extension", filePath));
            }

            string registryPath = string.Format(@"HKEY_CLASSES_ROOT\{0}", extension);

            string mimeType = null;

            try
            {
                object value = Registry.GetValue(registryPath, "Content Type", null);

                if (value != null)
                {
                    mimeType = value.ToString();
                }
            }
            catch
            {
            }

            return(mimeType);
        }