Skip to content

Commit 642c8bc

Browse files
Improve parameter type naming for generic types (#343)
1 parent 4642769 commit 642c8bc

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
* Enhance BoDi error handling to provide the name of the interface being registered when that interface has already been resolved (#324)
66
* Improve code-behind feature file compilation speed (#336)
7+
* Improve parameter type naming for generic types (#343)
78

89
## Bug fixes:
910

10-
*Contributors of this release (in alphabetical order):* @clrudolphi, @obligaron
11+
*Contributors of this release (in alphabetical order):* @clrudolphi, @obligaron, @olegKoshmeliuk
1112

1213
# v2.2.1 - 2024-11-08
1314

Reqnroll/Bindings/Reflection/BindingReflectionExtensions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static bool IsAssignableTo(this Type type, IBindingType baseType)
1717
if (baseType is RuntimeBindingType runtimeBindingType)
1818
return runtimeBindingType.Type.IsAssignableFrom(type);
1919

20-
if (type.FullName == baseType.FullName)
20+
if (new RuntimeBindingType(type).FullName == baseType.FullName)
2121
return true;
2222

2323
if (type.BaseType != null && IsAssignableTo(type.BaseType, baseType))
@@ -28,10 +28,11 @@ public static bool IsAssignableTo(this Type type, IBindingType baseType)
2828

2929
public static bool IsAssignableFrom(this Type baseType, IBindingType type)
3030
{
31+
var bindingType = new RuntimeBindingType(baseType);
3132
if (type is IPolymorphicBindingType polymorphicBindingType)
32-
return polymorphicBindingType.IsAssignableTo(new RuntimeBindingType(baseType));
33+
return polymorphicBindingType.IsAssignableTo(bindingType);
3334

34-
if (type.FullName == baseType.FullName)
35+
if (type.FullName == bindingType.FullName)
3536
return true;
3637

3738
return false;

Reqnroll/Bindings/Reflection/RuntimeBindingType.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23

34
namespace Reqnroll.Bindings.Reflection
45
{
@@ -8,13 +9,30 @@ public class RuntimeBindingType : IPolymorphicBindingType
89

910
public string Name => Type.Name;
1011

11-
public string FullName => Type.FullName;
12+
public string FullName { get; }
1213

1314
public string AssemblyName => Type.Assembly.GetName().Name;
1415

1516
public RuntimeBindingType(Type type)
1617
{
1718
Type = type;
19+
FullName = GetFullName(type);
20+
}
21+
22+
private static string GetFullName(Type type)
23+
{
24+
if (!type.IsConstructedGenericType)
25+
{
26+
return type.FullName;
27+
}
28+
29+
if (type.GetGenericTypeDefinition() == typeof(Nullable<>))
30+
{
31+
return type.GenericTypeArguments[0].FullName + "?";
32+
}
33+
34+
var genericParams = string.Join(",", type.GenericTypeArguments.Select(x => x.Name));
35+
return $"{type.Namespace}.{type.Name.Split('`')[0]}<{genericParams}>";
1836
}
1937

2038
public bool IsAssignableTo(IBindingType baseType)
@@ -37,7 +55,7 @@ public override bool Equals(object obj)
3755
if (ReferenceEquals(null, obj)) return false;
3856
if (ReferenceEquals(this, obj)) return true;
3957
if (obj.GetType() != GetType()) return false;
40-
return Equals((RuntimeBindingType) obj);
58+
return Equals((RuntimeBindingType)obj);
4159
}
4260

4361
public override int GetHashCode()

0 commit comments

Comments
 (0)