提交 a2b857fa 编写于 作者: V Victor Zaytsev

Fixed #13158.

上级 9aa3c76c
......@@ -701,6 +701,58 @@ static void Main(string[] args)
await VerifyItemIsAbsentAsync(markup, "D");
}
[WorkItem(13158, "https://github.com/dotnet/roslyn/issues/13158")]
[Fact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task CollectionInitializerForInterfaceType1()
{
var markup = @"
using System.Collections.Generic;
public class Foo
{
public IList<int> Items { get; } = new List<int>();
public int Bar;
}
class Program
{
static void Main(string[] args)
{
var y = new Foo { $$ };
}
}";
await VerifyItemExistsAsync(markup, "Items");
await VerifyItemExistsAsync(markup, "Bar");
}
[WorkItem(13158, "https://github.com/dotnet/roslyn/issues/13158")]
[Fact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task CollectionInitializerForInterfaceType2()
{
var markup = @"
using System.Collections.Generic;
public interface ICustomCollection<T> : ICollection<T> { }
public class Foo
{
public ICustomCollection<int> Items { get; } = new List<int>();
public int Bar;
}
class Program
{
static void Main(string[] args)
{
var y = new Foo { $$ };
}
}";
await VerifyItemExistsAsync(markup, "Items");
await VerifyItemExistsAsync(markup, "Bar");
}
[WorkItem(4754, "https://github.com/dotnet/roslyn/issues/4754")]
[Fact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task CollectionInitializerPatternFromBaseTypeAccessible()
......
......@@ -639,7 +639,10 @@ public static bool CanSupportCollectionInitializer(this ITypeSymbol typeSymbol,
{
return
typeSymbol.AllInterfaces.Any(i => i.SpecialType == SpecialType.System_Collections_IEnumerable) &&
typeSymbol.GetAccessibleMembersInThisAndBaseTypes<IMethodSymbol>(within ?? typeSymbol).Where(s => s.Name == WellKnownMemberNames.CollectionInitializerAddMethodName)
typeSymbol.GetBaseTypesAndThis()
.Union(typeSymbol.GetOriginalInterfacesAndTheirBaseInterfaces())
.SelectAccessibleMembers<IMethodSymbol>(within ?? typeSymbol)
.Where(s => s.Name == WellKnownMemberNames.CollectionInitializerAddMethodName)
.OfType<IMethodSymbol>()
.Any(m => m.Parameters.Any());
}
......@@ -682,9 +685,7 @@ public static INamedTypeSymbol GetDelegateType(this ITypeSymbol typeSymbol, Comp
return ImmutableArray<T>.Empty;
}
var types = containingType.GetBaseTypesAndThis();
return types.SelectMany(x => x.GetMembers().OfType<T>().Where(m => m.IsAccessibleWithin(within)))
.ToImmutableArray();
return containingType.GetBaseTypesAndThis().SelectAccessibleMembers<T>(within).ToImmutableArray();
}
public static bool? AreMoreSpecificThan(this IList<ITypeSymbol> t1, IList<ITypeSymbol> t2)
......@@ -722,6 +723,16 @@ public static INamedTypeSymbol GetDelegateType(this ITypeSymbol typeSymbol, Comp
return result;
}
private static IEnumerable<T> SelectAccessibleMembers<T>(this IEnumerable<ITypeSymbol> types, ISymbol within) where T : class, ISymbol
{
if (types == null)
{
return ImmutableArray<T>.Empty;
}
return types.SelectMany(x => x.GetMembers().OfType<T>().Where(m => m.IsAccessibleWithin(within)));
}
private static bool? IsMoreSpecificThan(this ITypeSymbol t1, ITypeSymbol t2)
{
// SPEC: A type parameter is less specific than a non-type parameter.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册