jQuery and ASP.NET

March 18, 2009

Determine if an object implements IEnumerable of (T)




I was recently attending a .NET conference when a programmer popped up a question. How do I make sure that my type implements IEnumerable<T>

Here's how:

C#


    protected void CheckInterfaceImpl(Type someType)


    {


        Type[] listInterfaces = someType.GetType().GetInterfaces();


        foreach (Type t in listInterfaces)


        {


            if (t.GetGenericTypeDefinition() == typeof(IEnumerable<>))   


            {       


                // Implements IEnumerable<T>


            }


            else


            {


                // Does not Implement IEnumerable<T>


            }


 


        }


    }




VB.NET


    Protected Sub CheckInterfaceImpl(ByVal someType As Type)


        Dim listInterfaces() As Type = someType.GetType.GetInterfaces()


        For Each t As Type In listInterfaces


            If t.GetGenericTypeDefinition() Is GetType(IEnumerable(Of )) Then


                ' Implements IEnumerable<T>


            Else


                ' Does not Implement IEnumerable<T>


            End If


 


        Next t


    End Sub




Know a better way? Share it here. I am all ears!


Bookmark this link on del.icio.us (saved by 0 users)

Did you like this post?
kick it on DotNetKicks.com
subscribe via rss subscribe via e-mail
print this post follow me on twitter
Others Also Read..

comments

0 Responses to "Determine if an object implements IEnumerable of (T)"
 

Copyright 2010 All Rights Reserved DevCurry.com by Suprotim Agarwal