IList C#运行时检查对象是否为IList [英] IList C# Runtime checking the object is IList or not

查看:188
本文介绍了IList C#运行时检查对象是否为IList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我的查询是,如何检查运行时对象变量是否为IList对象.如果为IList,则如何检查IList中包含的Class对象并读取该class对象的值.

我有两个类对象

Hi
My query is , how to check runtime the Object Variable is IList object or not . If IList then how to check the Class object contained in the IList and read the values of the the class object .

I have two class objects

public Class Employee
{
  int _EmpID;
  string _Empname
public String Empname
  {
    get {return _EmpName;}
    set {_EmpName=value;}
  }

  public int EmpID
  {
    get {return _EmpID;}
    set {_EmpID=value;}
  }

}

public Class Department
{
  int _DeptID
  string _DeptName

  public String DeptName
  {
    get {return _DeptName;}
    set {_DeptName=value;}
  }

  public int DeptID
  {
    get {return _DeptID;}
    set {_DeptID=value;}
  }

}

////Both class object is in IList .

public Object ClassObejcttoString(Object objvalue)
{
    // Converting it into String
}



现在在运行时,我想知道Object在IList的位置.如果是,则它包含哪个类对象,并在运行时读取该对象的值.



Now at runtime I want to know where Object is IList .. If yes which class object it contains and at run time and read the values of the object .

推荐答案

此是这样的:

This is how:

static Type GetGenericListElementType(Type candidateListType) {
    Type[] interfaces = candidateListType.GetInterfaces();
    foreach (Type interfaceType in interfaces)
        if (interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == typeof(System.Collections.Generic.IList<>)) {
            Type[] genericArguments = interfaceType.GetGenericArguments();
            if (genericArguments.Length != 1) return null;
            return genericArguments[0];
        } //if
    return null;
} //GetGenericListElementType

static bool IsIList(Type candidateListType) { return GetGenericListElementType(candidateListType) != null); }

//...
object someObject = //...
if (someObject != null) {
    bool isIList = IsIList(someObject.GetType());
    //...
}



该代码不仅可以回答问题,而且还可以确定泛型列表的元素类型.在以下意义上,这段代码是不平凡的:当您仅知道未实例化的泛型类型(IList<>)时,它将显示如何使用未完全定义的类型.

不用说,在新的开发中,您应该只使用强类型的泛型列表类型和接口,而不要使用弱类型的非泛型集合.如果仍然出于任何原因(旧版?)使用它,只需检查以下内容即可轻松简化上述代码:



Not only this code answers the question, it also determines the element type of the generic list. This code is non-trivial in the following sense: it shows how to work with the type which is not fully defined, when the you know only the non-instantiated generic type (IList<>).

Needless to say, in new development you should only use strongly-typed generic list types and interfaces, never the weak-typed non-generic collections. If you still need it by whatever reason (legacy?), you could easily simplify the above code simply checking:

Type typeInQuestion = //...
bool isIList = typeof(System.Collections.IList).IsAssignableFrom(typeInQuestion);





解决方案1甚至显示了更简单,最有可能更快的方法.考虑到我的回答集中在泛型收集和过大的问题上:-).我只想强调一下,非通用集合没有用.没有2.0之前的.NET Framework之类的东西;我认为这些旧版本根本没有意义.

[END EDIT]

但是同样,切勿在任何新开发中使用非泛型System.Collections.IList. 完全没有意义,仅支持旧版代码.非通用(非专用)集合早在.NET Framework v.2.0中就已过时.这些类型没有被正式标记为obsolete属性,只是因为如果在遗留代码中引入了这些类型,就不会有任何错误.

该问题已完全解决.享受. :-)

-SA





Solution 1 shows even simper and, most likely, faster method. Consider my answer as focused on the problem of generic collection and the overkill :-). I just want to emphasize that non-generic collections are not useful. There is no such thing as .NET Framework prior to 2.0; I think those old versions makes little sense if any at all.

[END EDIT]

But again, never use non-generic System.Collections.IList in any new development. It makes no sense at all, only good to support legacy code. The non-generic (non-specialized) collections was rendered obsolete as early as of .NET Framework v.2.0. These types were not formally marked with obsolete attribute only because there is nothing wrong in these types if there were introduced in legacy code.

The problem is fully solved. Enjoy. :-)

—SA


IList il = myObject as IList;

if (il != null)
{
// It's an iList

}


这篇关于IList C#运行时检查对象是否为IList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆