Skip to content

Commit 995ce0c

Browse files
iqv-csisJohanLarsson
authored andcommitted
Don't report REFL026 when interface types is given
1 parent 4fc7852 commit 995ce0c

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

ReflectionAnalyzers.Tests/REFL026MissingDefaultConstructorTests/Valid.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,5 +149,32 @@ public class C
149149

150150
RoslynAssert.Valid(Analyzer, Descriptor, code);
151151
}
152+
153+
[Test]
154+
public static void WhenOnInterfaceTypes()
155+
{
156+
var code = @"
157+
namespace N
158+
{
159+
using System;
160+
161+
public interface A { }
162+
163+
public class B : A
164+
{
165+
public B() { }
166+
}
167+
168+
public class C
169+
{
170+
public C(A a)
171+
{
172+
var foo = (A?)Activator.CreateInstance(a.GetType());
173+
}
174+
}
175+
}";
176+
177+
RoslynAssert.Valid(Analyzer, Descriptor, code);
178+
}
152179
}
153180
}

ReflectionAnalyzers/NodeAnalzers/ActivatorAnalyzer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ private static bool TryGetCreatedType(IMethodSymbol createInstance, InvocationEx
104104

105105
private static bool IsMissingDefaultConstructor(IMethodSymbol createInstance, InvocationExpressionSyntax invocation, INamedTypeSymbol createdType)
106106
{
107+
if (createdType.TypeKind == TypeKind.Interface)
108+
return false;
109+
107110
if (createInstance.IsGenericMethod &&
108111
!HasDefaultConstructor())
109112
{

0 commit comments

Comments
 (0)