Click here to Skip to main content
15,898,010 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C++
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
}


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 .
Posted
Updated 20-Aug-12 9:43am
v2

This is how:

C#
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());
    //...
}


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:

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


[EDIT]

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
 
Share this answer
 
v3
Comments
Christian Graus 20-Aug-12 13:42pm    
And so I gave you a 5 - your answer goes beyond the question that was asked with all sorts of useful info.
Sergey Alexandrovich Kryukov 20-Aug-12 14:18pm    
Thank you very much; I did not actually expect that. :-)
--SA
Christian Graus 20-Aug-12 14:20pm    
*grin*
RaisKazi 20-Aug-12 15:51pm    
Really useful information Sergey, added to my Bookmarks. 5ed
Sergey Alexandrovich Kryukov 20-Aug-12 16:35pm    
Thank you, Rais.
I only recently figured out those detail about finding collection's (and also array's) element types and incomplete type information from Reflection, glad to share...
--SA
C#
IList il = myObject as IList;

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

}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Aug-12 13:31pm    
This is 100% correct and would be quite good if... non-generic IList would be useful. For generic IList, it's a bit more tricky, but generic IList is million times more useful. I voted 4 even though the answer is perfectly correct.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900