我有点新的反思,所以原谅我,如果这是一个更基本的问题我正在编写一个程序在C#中,并试图编写一个通用的空或空检查方法 到目前为止代码读取,使用反射投出一个对象
public static class EmptyNull
{
public static bool EmptyNullChecker(Object o)
{
try
{
var ob = (object[]) o;
if (ob == null || !ob.Any())
return true;
}
catch (Exception e)// i could use genercs to figure out if this a array but //figured i just catch the exception
{Console.WriteLine(e);}
try
{
if (o.GetType().GetGenericTypeDefinition().Equals("System.Collections.Generic.List`1[T]"))
//the following line is where the code goes haywire
var ob = (List<o.GetType().GetGenericArguments()[0].ReflectedType>)o;
if (ob == null || !ob.Any())
return true;
}
catch (Exception e)
{ Console.WriteLine(e); }
return o == null || o.ToString().Equals("");//the only thing that can return "" after a toString() is a string that ="", if its null will return objects placeMarker
}
}
现在显然是一个列表,我需要一种方法来弄清楚它是什么类型的泛型列表的,所以我想使用反射来弄明白,再与该反射投正是这种可能
谢谢
不管你做什么,移动测试空顶部的启发。你现在有各种空解除引用。 – 2012-07-09 23:57:30
这些对象来自哪里,你失去了所有类型的信息? – bmm6o 2012-07-10 00:00:27
任何地方我真的不在乎它只是一个通用的方法,我可以用我的程序来快速找出这个对象,即时处理是空的还是空的 - 而不是写出我使用的每个特定项目的支票 – 2012-07-10 00:02:36