2023-11-21 03:59:29 +03:00
|
|
|
|
namespace Refit
|
2018-10-06 01:40:17 +03:00
|
|
|
|
{
|
2019-10-19 18:37:24 +03:00
|
|
|
|
class UniqueName
|
2018-10-06 01:40:17 +03:00
|
|
|
|
{
|
|
|
|
|
public static string ForType<T>()
|
2019-04-02 18:47:58 +03:00
|
|
|
|
{
|
|
|
|
|
return ForType(typeof(T));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string ForType(Type refitInterfaceType)
|
2018-10-06 01:40:17 +03:00
|
|
|
|
{
|
2021-02-04 19:01:19 +03:00
|
|
|
|
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)
|
2018-10-06 01:40:17 +03:00
|
|
|
|
{
|
2021-02-04 19:01:19 +03:00
|
|
|
|
interfaceTypeName = interfaceTypeName.Substring(lastDot + 1);
|
2018-10-06 01:40:17 +03:00
|
|
|
|
}
|
2019-02-17 01:11:56 +03:00
|
|
|
|
|
2021-02-04 19:01:19 +03:00
|
|
|
|
// 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
|
2023-11-27 01:50:46 +03:00
|
|
|
|
if (refitInterfaceType.IsGenericType)
|
2021-02-04 19:01:19 +03:00
|
|
|
|
{
|
|
|
|
|
genericArgs = interfaceTypeName.Substring(interfaceTypeName.IndexOf("["));
|
2023-11-27 01:50:46 +03:00
|
|
|
|
interfaceTypeName = interfaceTypeName.Substring(
|
|
|
|
|
0,
|
|
|
|
|
interfaceTypeName.Length - genericArgs.Length
|
|
|
|
|
);
|
2018-10-06 01:40:17 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-04 19:01:19 +03:00
|
|
|
|
// Remove any + from the type name portion
|
|
|
|
|
interfaceTypeName = interfaceTypeName.Replace("+", "");
|
|
|
|
|
|
2021-02-04 20:21:29 +03:00
|
|
|
|
// Get the namespace and remove the dots
|
2021-02-04 19:01:19 +03:00
|
|
|
|
var ns = refitInterfaceType.Namespace?.Replace(".", "");
|
|
|
|
|
|
|
|
|
|
// Refit types will be generated as private classes within a Generated type in namespace
|
|
|
|
|
// Refit.Implementation
|
2024-09-29 22:51:11 +03:00
|
|
|
|
// E.g., Refit.Implementation.Generated.NamespaceContainingTpeInterfaceType
|
2021-02-04 19:01:19 +03:00
|
|
|
|
|
2023-11-27 01:50:46 +03:00
|
|
|
|
var refitTypeName =
|
|
|
|
|
$"Refit.Implementation.Generated+{ns}{interfaceTypeName}{genericArgs}";
|
2021-02-04 19:01:19 +03:00
|
|
|
|
|
|
|
|
|
var assmQualified = $"{refitTypeName}, {refitInterfaceType.Assembly.FullName}";
|
|
|
|
|
|
|
|
|
|
return assmQualified;
|
2018-10-06 01:40:17 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|