Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Extensions/TypeExtensions.cs @ 17284

Last change on this file since 17284 was 17284, checked in by dpiringe, 5 years ago

#3026: renamed namespace and project from HeuristicLab.Manufacture to HeuristicLab.JsonInterface

File size: 1.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Core;
7
8namespace HeuristicLab.JsonInterface {
9  public static class TypeExtensions {
10
11    public static bool IsEqualTo(this Type type, Type other) {
12      if (other == null) throw new ArgumentNullException("other");
13      if (type == other) return true;
14      if (other.IsInterface && other.IsGenericType)
15        return
16          type.IsAssignableFrom(other) ||
17          type.GetInterfaces()
18            .Where(i => i.IsGenericType)
19            .Any(i => i.GetGenericTypeDefinition() == other);
20      else if (other.IsInterface) {
21        return type.GetInterfaces().Any(i => i == other);
22      }
23       
24      else if (other.IsGenericType) {
25        Type baseType = type;
26        while (baseType != typeof(object)) {
27          if (baseType.IsGenericType && baseType.GetGenericTypeDefinition() == other)
28            return true;
29          baseType = baseType.BaseType;
30        }
31      }
32      return type.IsAssignableFrom(other);
33    }
34  }
35}
Note: See TracBrowser for help on using the repository browser.