Skip to content

Commit 99bd936

Browse files
fixed bug
1 parent 29c3451 commit 99bd936

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

src/Reflector/IsType.cs

+46-2
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,13 @@ public static bool Enumerable(Type type)
151151
{
152152
return type.IsArray || (type != typeof(string) && AssignableTo(type, typeof(System.Collections.IEnumerable)));
153153
}
154-
public static bool AssignableTo(Type assignableType, Type type)
154+
public static bool AssignableTo(Type fromType, Type toType)
155155
{
156-
return type?.IsAssignableFrom(assignableType) ?? false;
156+
if (fromType == null || toType == null) return false;
157+
158+
return toType.IsGenericTypeDefinition
159+
? toType.IsAssignableTo(fromType)
160+
: fromType.IsAssignableFrom(toType);
157161
}
158162
public static bool CanBeNull(Type type)
159163
{
@@ -285,5 +289,45 @@ public static bool ReadOnlyMemory(Type type, [NotNullWhen(true)] out Type? eleme
285289
elementType = null;
286290
return false;
287291
}
292+
public static bool AssignableToOpenGeneric(Type type, Type elementType)
293+
{
294+
if (elementType.IsInterface)
295+
{
296+
return ImplementationOfOpenGeneric(type, elementType);
297+
}
288298

299+
return type == elementType || DerivedFromOpenGeneric(type, elementType);
300+
}
301+
public static bool ImplementationOfOpenGeneric(Type type, Type elementType)
302+
{
303+
if (type.IsInterface && type.IsGenericType &&
304+
type.GetGenericTypeDefinition() == elementType)
305+
{
306+
return true;
307+
}
308+
return type.GetInterfaces()
309+
.Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == elementType);
310+
}
311+
public static bool DerivedFromOpenGeneric(Type? type, Type? elementType)
312+
{
313+
if (type == null || elementType == null)
314+
{
315+
return false;
316+
}
317+
318+
if (type == elementType)
319+
{
320+
return false;
321+
}
322+
323+
for (Type? baseType = type; baseType is not null; baseType = baseType.BaseType)
324+
{
325+
if (baseType.IsGenericType && baseType.GetGenericTypeDefinition() == elementType)
326+
{
327+
return true;
328+
}
329+
}
330+
331+
return false;
332+
}
289333
}

src/Reflector/VReflector.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>net8.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
7-
<VersionPrefix>2.0.0</VersionPrefix>
7+
<VersionPrefix>2.0.0.1</VersionPrefix>
88
<SignAssembly>True</SignAssembly>
99
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
1010
<Title>VReflector</Title>

0 commit comments

Comments
 (0)