C# how detect is some some type is some type

Максим Глуховский 21 Reputation points
2022-03-29T17:59:54.433+00:00

I have some data (is a inherited entity and some realize some interface)
then i have filter it

var needTypes = new List<Type>();
needTypes.Add(typeof( Dlinnomer ));//Some class
needTypes.Add(typeof( IBarometr ));//Some interface

data = data.Where(d => needTypes.Any(s => d is s));

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,922 questions
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 65,211 Reputation points
    2022-03-29T21:01:51.787+00:00

    the is operator only works at compile time, you must use a runtime compare. also GetType() return the runtime type, not inherited types or interfaces. so you need to use the Type method .IsAssignableFrom which is the runtime version of the is operator.

    var list = data.Where(d => needTypes.Any(s => s.IsAssignableFrom(d.GetType()))).ToList();
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,421 Reputation points
    2022-03-29T20:34:46.427+00:00

    By Interface

    using System;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                object[] items = { new Person(), new Human() };
    
                foreach (var item in items)
                {
                    if (item is IBase)
                    {
                        Console.WriteLine(item.GetType());
                    }
                }
    
                Console.ReadLine();
            }
        }
    
        public interface IBase
        {
            public int Id { get;  }
    
        }
    
        public class Person : IBase
        {
            public int Identifier { get; set; }
            public int Id => Identifier;
        }
    
        public class Human
        {
            public int Identifier { get; set; }
        }
    }
    

    C#9 or higher

    class Program
    {
        static void Main(string[] args)
        {
            object[] items = { new Person() {Identifier = 3}, new Human() };
    
            foreach (var item in items)
            {
    
                if (item as IBase is { } casted)
                {
                    Console.WriteLine(casted.Id);
                }
    
            }
    
            Console.ReadLine();
        }
    }
    

    And for types

    static class Extensions
    {
        public static bool Implements<I>(this Type source) where I : class 
            => typeof(I).IsAssignableFrom(source);
    }
    

    Usage

    static void Main(string[] args)
    {
        IEnumerable<Type> result = TypesList().Where(type => type.Implements<IBase>());
    }
    
    private static object[] TypesArray()
    {
        object[] items = { new Person() { Identifier = 3 }, new Human() { Identifier = 2 } };
        return items;
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.