Get Container Interface - CSharp System.Reflection

CSharp examples for System.Reflection:Interface

Description

Get Container Interface

Demo Code


using System.Threading.Tasks;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.CompilerServices;
using System.Reflection.Emit;
using System.Reflection;
using System.Linq;
using System.IO;//from ww  w  .j  a v a2s.  co  m
using System.Globalization;
using System.Collections.Generic;
using System;

public class Main{
        public static Type GetContainerInterface(this Type t, Type containerType)
        {
            return
                (t.IsGenericType() && t.GetGenericTypeDefinition() == containerType)
                    ? t
                    : t.GetInterfaces().First(i => i.IsGenericType() && i.GetGenericTypeDefinition() == containerType);
        }
        public static bool IsGenericType(this Type type)
        {
            var info = type.GetTypeInfo();

            return info.IsGenericType;
        }
}

Related Tutorials