Find Collection Interface from Type - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:ICollection

Description

Find Collection Interface from Type

Demo Code


using System.Reflection;
using System.Collections.Generic;
using System.Collections;
using System;//from w w w . java 2s . com

public class Main{
        private static Type FindCollectionInterface(Type type)
        {
            var interfaces = type.FindInterfaces((t, o) =>
            {
                if (t.IsGenericType)
                    return t.GetGenericTypeDefinition() == typeof(ICollection<>);
                return false;
            }, null);

            return (interfaces.Length == 1)
                ? interfaces[0]
                : null;
        }
}

Related Tutorials