-
Notifications
You must be signed in to change notification settings - Fork 3
/
ExtensionMethods.cs
43 lines (36 loc) · 1.38 KB
/
ExtensionMethods.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
using System;
using System.Linq.Expressions;
namespace TinyMCERTE {
public static class ExtensionMethods {
public static String nameof<T, TT>(this Expression<Func<T, TT>> accessor) {
return nameof(accessor.Body);
}
public static String nameof<T>(this Expression<Func<T>> accessor) {
return nameof(accessor.Body);
}
public static String nameof<T, TT>(this T obj, Expression<Func<T, TT>> propertyAccessor) {
return nameof(propertyAccessor.Body);
}
public static string Left(this string value, int maxLength) {
if (string.IsNullOrEmpty(value)) return value;
maxLength = Math.Abs(maxLength);
return (value.Length <= maxLength
? value
: value.Substring(0, maxLength)
);
}
public static string FormatWith(this string value, params object[] args) {
return String.Format(value, args);
}
private static String nameof(Expression expression) {
if (expression.NodeType == ExpressionType.MemberAccess)
{
var memberExpression = expression as MemberExpression;
if (memberExpression == null)
return null;
return memberExpression.Member.Name;
}
return null;
}
}
}