forked from reactiveui/refit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UniqueName.cs
56 lines (46 loc) · 1.96 KB
/
UniqueName.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
47
48
49
50
51
52
53
54
55
56
namespace Refit
{
class UniqueName
{
public static string ForType<T>()
{
return ForType(typeof(T));
}
public static string ForType(Type refitInterfaceType)
{
var interfaceTypeName = refitInterfaceType.FullName!;
// remove namespace/nested, up to anything before a `
var searchEnd = interfaceTypeName.IndexOf('`');
if (searchEnd < 0)
searchEnd = interfaceTypeName.Length;
var lastDot = interfaceTypeName.Substring(0, searchEnd).LastIndexOf('.');
if (lastDot > 0)
{
interfaceTypeName = interfaceTypeName.Substring(lastDot + 1);
}
// Now we have the interface name like IFooBar`1[[Some Generic Args]]
// Or Nested+IFrob
var genericArgs = string.Empty;
// if there's any generics, split that
if (refitInterfaceType.IsGenericType)
{
genericArgs = interfaceTypeName.Substring(interfaceTypeName.IndexOf("["));
interfaceTypeName = interfaceTypeName.Substring(
0,
interfaceTypeName.Length - genericArgs.Length
);
}
// Remove any + from the type name portion
interfaceTypeName = interfaceTypeName.Replace("+", "");
// Get the namespace and remove the dots
var ns = refitInterfaceType.Namespace?.Replace(".", "");
// Refit types will be generated as private classes within a Generated type in namespace
// Refit.Implementation
// E.g., Refit.Implementation.Generated.NamespaceContaingTpeInterfaceType
var refitTypeName =
$"Refit.Implementation.Generated+{ns}{interfaceTypeName}{genericArgs}";
var assmQualified = $"{refitTypeName}, {refitInterfaceType.Assembly.FullName}";
return assmQualified;
}
}
}