-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extension.cs
46 lines (39 loc) · 1.29 KB
/
Extension.cs
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
41
42
43
44
45
46
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Basics
{
public static class Extension
{
public static bool IsNull<T>(this T instance)
{
return instance == null;
}
public static bool IsNotNull<T>(this T instance)
{
return !instance.IsNull();
}
public static bool IsNullOrDefault<T>(this T instance)
{
if (instance == null)
return true;
return instance.Equals(default(T));
}
public static bool IsNotNullOrDefault<T>(this T instance)
{
return !instance.IsNullOrDefault();
}
public static TAttribute GetAttribute<TAttribute, TInstance>(this TInstance instance)
where TAttribute : Attribute
{
return (TAttribute)instance.GetAttributes<TAttribute, TInstance>().First();
}
public static IEnumerable<TAttribute> GetAttributes<TAttribute, TInstance>(this TInstance instance)
where TAttribute : Attribute
{
var memberInfo = typeof(TInstance).GetMember(instance.ToString());
return (IEnumerable<TAttribute>)memberInfo[0].GetCustomAttributes(typeof(TAttribute), false);
}
}
}