summaryrefslogtreecommitdiff
path: root/lib/TypeExtensions.cs
blob: 45ae24409bde6aca6ab465fd040e0411e5803d9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
using System.Linq;
using System.Reflection;

namespace jive
{
  public static class TypeExtensions
  {
    public static Type last_interface(this Type type)
    {
      return type.GetInterfaces()[type.GetInterfaces().Length - 1];
    }

    public static Type first_interface(this Type type)
    {
      return type.GetInterfaces()[0];
    }

    public static bool is_a_generic_type(this Type type)
    {
      //return type.IsGenericType;
      return type.IsGenericTypeDefinition;
    }

    public static object default_value(this Type type)
    {
      return (type.IsValueType ? Activator.CreateInstance(type) : null);
    }

    public static Attribute get_attribute<Attribute>(this ICustomAttributeProvider provider)
      where Attribute : System.Attribute
      {
        return
          provider
          .GetCustomAttributes(typeof (Attribute), false)
          .Select(x => x.downcast_to<Attribute>())
          .First();
      }
  }
}