refit/Refit/UniqueName.cs

57 строки
2.0 KiB
C#
Исходник Постоянная ссылка Обычный вид История

namespace Refit
{
2019-10-19 18:37:24 +03:00
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);
}
2019-02-17 01:11:56 +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)
{
genericArgs = interfaceTypeName.Substring(interfaceTypeName.IndexOf("["));
2023-11-27 01:50:46 +03:00
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.NamespaceContainingTpeInterfaceType
2023-11-27 01:50:46 +03:00
var refitTypeName =
$"Refit.Implementation.Generated+{ns}{interfaceTypeName}{genericArgs}";
var assmQualified = $"{refitTypeName}, {refitInterfaceType.Assembly.FullName}";
return assmQualified;
}
}
}