-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cs
executable file
·45 lines (38 loc) · 1.53 KB
/
Utils.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
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.CSharp;
namespace Scripting
{
class Utils
{
private static readonly CSharpCodeProvider CodeProvider = new CSharpCodeProvider();
private static Dictionary<Type, string> TypeNameMapping = new Dictionary<Type, string>();
internal static string GetTypeName(Type type)
{
TypeNameMapping.TryGetValue(type, out string name);
if (name == null)
{
// TODO: accept generic types as input, that are bound when finding matching methods
var reference = new CodeTypeReference(type);
if (type.ContainsGenericParameters)
reference.TypeArguments.AddRange(type.GenericTypeArguments.Select(a => new CodeTypeReference(a)).ToArray());
name = CodeProvider.GetTypeOutput(reference);
TypeNameMapping[type] = name = Regex.Replace(name, "`\\d+", "");
}
return name;
}
internal static string GetTypeNameShort(Type type)
{
return GetTypeName(type).Replace(type.Namespace + ".", "");
}
internal static string GetLongestCommonPrefix(IEnumerable<string> strings)
{
return !strings.Any() ? "" : new string(
strings.First().Substring(0, strings.Min(s => s.Length))
.TakeWhile((c, i) => strings.All(s => s[i] == c)).ToArray());
}
}
}