Skip to content

Commit

Permalink
Improve parameter type naming for generic types (#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
olegKoshmeliuk authored Dec 18, 2024
1 parent 4642769 commit 642c8bc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

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

## Bug fixes:

*Contributors of this release (in alphabetical order):* @clrudolphi, @obligaron
*Contributors of this release (in alphabetical order):* @clrudolphi, @obligaron, @olegKoshmeliuk

# v2.2.1 - 2024-11-08

Expand Down
7 changes: 4 additions & 3 deletions Reqnroll/Bindings/Reflection/BindingReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static bool IsAssignableTo(this Type type, IBindingType baseType)
if (baseType is RuntimeBindingType runtimeBindingType)
return runtimeBindingType.Type.IsAssignableFrom(type);

if (type.FullName == baseType.FullName)
if (new RuntimeBindingType(type).FullName == baseType.FullName)
return true;

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

public static bool IsAssignableFrom(this Type baseType, IBindingType type)
{
var bindingType = new RuntimeBindingType(baseType);
if (type is IPolymorphicBindingType polymorphicBindingType)
return polymorphicBindingType.IsAssignableTo(new RuntimeBindingType(baseType));
return polymorphicBindingType.IsAssignableTo(bindingType);

if (type.FullName == baseType.FullName)
if (type.FullName == bindingType.FullName)
return true;

return false;
Expand Down
22 changes: 20 additions & 2 deletions Reqnroll/Bindings/Reflection/RuntimeBindingType.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;

namespace Reqnroll.Bindings.Reflection
{
Expand All @@ -8,13 +9,30 @@ public class RuntimeBindingType : IPolymorphicBindingType

public string Name => Type.Name;

public string FullName => Type.FullName;
public string FullName { get; }

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

public RuntimeBindingType(Type type)
{
Type = type;
FullName = GetFullName(type);
}

private static string GetFullName(Type type)
{
if (!type.IsConstructedGenericType)
{
return type.FullName;
}

if (type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
return type.GenericTypeArguments[0].FullName + "?";
}

var genericParams = string.Join(",", type.GenericTypeArguments.Select(x => x.Name));
return $"{type.Namespace}.{type.Name.Split('`')[0]}<{genericParams}>";
}

public bool IsAssignableTo(IBindingType baseType)
Expand All @@ -37,7 +55,7 @@ public override bool Equals(object obj)
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((RuntimeBindingType) obj);
return Equals((RuntimeBindingType)obj);
}

public override int GetHashCode()
Expand Down

0 comments on commit 642c8bc

Please sign in to comment.