在C#中进行适当的可空类型检查?

时间:2008-10-04 02:05:02

标签: c# casting nullable

好的,我的实际问题是:我正在实施IList<T>。当我到达CopyTo(Array array, int index)时,这是我的解决方案:

void ICollection.CopyTo(Array array, int index)
{
    // Bounds checking, etc here.
    if (!(array.GetValue(0) is T))
        throw new ArgumentException("Cannot cast to this type of Array.");
    // Handle copying here.
}

这在我的原始代码中有效,并且仍然有效。但它有一个小缺陷,直到我开始为它构建测试时才暴露,特别是这个:

public void CopyToObjectArray()
{
    ICollection coll = (ICollection)_list;
    string[] testArray = new string[6];

    coll.CopyTo(testArray, 2);
}

现在,这个测试应该通过。它抛出ArgumentException无法施放。为什么? array[0] == null。检查设置为is的变量时,null关键字始终返回false。现在,由于各种原因,这很方便,包括避免空取消引用等。我最终想出的类型检查是:

try
{
    T test = (T)array.GetValue(0);
}
catch (InvalidCastException ex)
{
    throw new ArgumentException("Cannot cast to this type of Array.", ex);
}

这不是很优雅,但它的确有效......有没有更好的方法呢?

4 个答案:

答案 0 :(得分:4)

有一种专门用于此类型的方法,请尝试:

if(!typeof(T).IsAssignableFrom(array.GetElementType()))

答案 1 :(得分:3)

确保唯一的方法是使用反射,但有90%的时间可以通过使用array is T[]来避免成本。大多数人都会传入一个正确类型的数组,所以这样做。但是,您应该始终提供代码来进行反射检查,以防万一。这就是我的通用锅板看起来的样子(注意:我在这里写的,从内存中写的,所以这可能无法编译,但它应该给出基本的想法):

class MyCollection : ICollection<T> {
   void ICollection<T>.CopyTo(T[] array, int index) {
       // Bounds checking, etc here.
       CopyToImpl(array, index);
   }
   void ICollection.CopyTo(Array array, int index) {
       // Bounds checking, etc here.
       if (array is T[]) { // quick, avoids reflection, but only works if array is typed as exactly T[]
           CopyToImpl((T[])localArray, index);
       } else {
           Type elementType = array.GetType().GetElementType();
           if (!elementType.IsAssignableFrom(typeof(T)) && !typeof(T).IsAssignableFrom(elementType)) {
               throw new Exception();
           }
           CopyToImpl((object[])array, index);
       }
   }
   private void CopyToImpl(object[] array, int index) {
       // array will always have a valid type by this point, and the bounds will be checked
       // Handle the copying here
   }
}

编辑:好的,忘了指出一些事情。一对夫妇回答天真地使用了什么,在这段代码中,只读取element.IsAssignableFrom(typeof(T))。如果开发人员知道此特定typeof(T).IsAssignableFrom(elementType)中的所有值实际上属于ICollection类型,那么 也应该允许S,就像BCL一样派生自T,并传递类型S[]

的数组

答案 2 :(得分:1)

List<T>使用此:

try
{
    Array.Copy(this._items, 0, array, index, this.Count);
}
catch (ArrayTypeMismatchException)
{
  //throw exception...
}

答案 3 :(得分:0)

这是对try / catch与反射的一点测试:

object[] obj = new object[] { };
DateTime start = DateTime.Now;

for (int x = 0; x < 1000; x++)
{
    try
    {
        throw new Exception();
    }
    catch (Exception ex) { }
}
DateTime end = DateTime.Now;
Console.WriteLine("Try/Catch: " + (end - start).TotalSeconds.ToString());

start = DateTime.Now;

for (int x = 0; x < 1000; x++)
{
    bool assignable = typeof(int).IsAssignableFrom(obj.GetType().GetElementType());
}
end = DateTime.Now;
Console.WriteLine("IsAssignableFrom: " + (end - start).TotalSeconds.ToString());

发布模式下的结果输出为:

Try/Catch: 1.7501001
IsAssignableFrom: 0

在调试模式下:

Try/Catch: 1.8171039
IsAssignableFrom: 0.0010001

结论,只需进行反思检查。这是值得的。

相关问题