//******************************
// Written by Peter Golde
// Copyright (c) 2004-2005, Wintellect
//
// Use and restribution of this code is subject to the license agreement
// contained in the file "License.txt" accompanying this file.
//******************************
using System;
using System.Collections;
using System.Collections.Generic;
namespace Wintellect.PowerCollections
{
///
/// A holder class for various internal utility functions that need to be shared.
///
internal static class Util
{
///
/// Determine if a type is cloneable: either a value type or implementing
/// ICloneable.
///
/// Type to check.
/// Returns if the type is a value type, and does not implement ICloneable.
/// True if the type is cloneable.
public static bool IsCloneableType(Type type, out bool isValue)
{
isValue = false;
if (typeof(ICloneable).IsAssignableFrom(type)) {
return true;
}
else if (type.IsValueType) {
isValue = true;
return true;
}
else
return false;
}
///
/// Returns the simple name of the class, for use in exception messages.
///
/// The simple name of this class.
public static string SimpleClassName(Type type)
{
string name = type.Name;
// Just use the simple name.
int index = name.IndexOfAny(new char[] { '<', '{', '`' });
if (index >= 0)
name = name.Substring(0, index);
return name;
}
///
/// Wrap an enumerable so that clients can't get to the underlying
/// implementation via a down-cast.
///
[Serializable]
class WrapEnumerable : IEnumerable
{
IEnumerable wrapped;
///
/// Create the wrapper around an enumerable.
///
/// IEnumerable to wrap.
public WrapEnumerable(IEnumerable wrapped)
{
this.wrapped = wrapped;
}
public IEnumerator GetEnumerator()
{
return wrapped.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)wrapped).GetEnumerator();
}
}
///
/// Wrap an enumerable so that clients can't get to the underlying
/// implementation via a down-case
///
/// Enumerable to wrap.
/// A wrapper around the enumerable.
public static IEnumerable CreateEnumerableWrapper(IEnumerable wrapped)
{
return new WrapEnumerable(wrapped);
}
///
/// Gets the hash code for an object using a comparer. Correctly handles
/// null.
///
/// Item to get hash code for. Can be null.
/// The comparer to use.
/// The hash code for the item.
public static int GetHashCode(T item, IEqualityComparer equalityComparer)
{
if (item == null)
return 0x1786E23C;
else
return equalityComparer.GetHashCode(item);
}
}
}