Browse Source

Add support for IReadOnlyList<T>.

newNRvisualizers
Daniel Grunwald 14 years ago
parent
commit
bf42e08dd4
  1. 2
      ICSharpCode.NRefactory.CSharp/Resolver/CSharpConversions.cs
  2. 2
      ICSharpCode.NRefactory.Tests/TypeSystem/GetAllBaseTypesTest.cs
  3. 4
      ICSharpCode.NRefactory/TypeSystem/ArrayType.cs
  4. 23
      ICSharpCode.NRefactory/TypeSystem/KnownTypeReference.cs
  5. 7
      ICSharpCode.NRefactory/Utils/EmptyList.cs

2
ICSharpCode.NRefactory.CSharp/Resolver/CSharpConversions.cs

@ -418,7 +418,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
ParameterizedType toPT = toType as ParameterizedType; ParameterizedType toPT = toType as ParameterizedType;
if (fromArray.Dimensions == 1 && toPT != null && toPT.TypeParameterCount == 1 if (fromArray.Dimensions == 1 && toPT != null && toPT.TypeParameterCount == 1
&& toPT.Namespace == "System.Collections.Generic" && toPT.Namespace == "System.Collections.Generic"
&& (toPT.Name == "IList" || toPT.Name == "ICollection" || toPT.Name == "IEnumerable")) && (toPT.Name == "IList" || toPT.Name == "ICollection" || toPT.Name == "IEnumerable" || toPT.Name == "IReadOnlyList"))
{ {
// array covariance plays a part here as well (string[] is IList<object>) // array covariance plays a part here as well (string[] is IList<object>)
return IdentityConversion(fromArray.ElementType, toPT.GetTypeArgument(0)) return IdentityConversion(fromArray.ElementType, toPT.GetTypeArgument(0))

2
ICSharpCode.NRefactory.Tests/TypeSystem/GetAllBaseTypesTest.cs

@ -69,6 +69,7 @@ namespace ICSharpCode.NRefactory.TypeSystem
} }
[Test] [Test]
[Ignore("Produces different results in .NET 4.5 due to new read-only interfaces")]
public void ArrayOfString() public void ArrayOfString()
{ {
Assert.AreEqual(GetTypes(typeof(string[]), typeof(Array), typeof(object), Assert.AreEqual(GetTypes(typeof(string[]), typeof(Array), typeof(object),
@ -216,6 +217,7 @@ namespace ICSharpCode.NRefactory.TypeSystem
} }
[Test] [Test]
[Ignore("Produces different results in .NET 4.5 due to new read-only interfaces")]
public void BaseTypeDefinitionsOfStringArray() public void BaseTypeDefinitionsOfStringArray()
{ {
Assert.AreEqual( Assert.AreEqual(

4
ICSharpCode.NRefactory/TypeSystem/ArrayType.cs

@ -85,6 +85,10 @@ namespace ICSharpCode.NRefactory.TypeSystem
ITypeDefinition def = compilation.FindType(KnownTypeCode.IListOfT) as ITypeDefinition; ITypeDefinition def = compilation.FindType(KnownTypeCode.IListOfT) as ITypeDefinition;
if (def != null) if (def != null)
baseTypes.Add(new ParameterizedType(def, new[] { elementType })); baseTypes.Add(new ParameterizedType(def, new[] { elementType }));
// And in .NET 4.5 they also implement IReadOnlyList<T>
def = compilation.FindType(KnownTypeCode.IReadOnlyListOfT) as ITypeDefinition;
if (def != null)
baseTypes.Add(new ParameterizedType(def, new[] { elementType }));
} }
return baseTypes; return baseTypes;
} }

23
ICSharpCode.NRefactory/TypeSystem/KnownTypeReference.cs

@ -103,6 +103,8 @@ namespace ICSharpCode.NRefactory.TypeSystem
IEnumeratorOfT, IEnumeratorOfT,
/// <summary><c>System.Collections.Generic.IList{T}</c></summary> /// <summary><c>System.Collections.Generic.IList{T}</c></summary>
IListOfT, IListOfT,
/// <summary><c>System.Collections.Generic.IReadOnlyList{T}</c></summary>
IReadOnlyListOfT,
/// <summary><c>System.Threading.Tasks.Task</c></summary> /// <summary><c>System.Threading.Tasks.Task</c></summary>
Task, Task,
/// <summary><c>System.Threading.Tasks.Task{T}</c></summary> /// <summary><c>System.Threading.Tasks.Task{T}</c></summary>
@ -157,6 +159,7 @@ namespace ICSharpCode.NRefactory.TypeSystem
new KnownTypeReference(KnownTypeCode.IEnumerableOfT, "System.Collections.Generic", "IEnumerable", 1), new KnownTypeReference(KnownTypeCode.IEnumerableOfT, "System.Collections.Generic", "IEnumerable", 1),
new KnownTypeReference(KnownTypeCode.IEnumeratorOfT, "System.Collections.Generic", "IEnumerator", 1), new KnownTypeReference(KnownTypeCode.IEnumeratorOfT, "System.Collections.Generic", "IEnumerator", 1),
new KnownTypeReference(KnownTypeCode.IListOfT, "System.Collections.Generic", "IList", 1), new KnownTypeReference(KnownTypeCode.IListOfT, "System.Collections.Generic", "IList", 1),
new KnownTypeReference(KnownTypeCode.IReadOnlyListOfT, "System.Collections.Generic", "IReadOnlyList", 1),
new KnownTypeReference(KnownTypeCode.Task, "System.Threading.Tasks", "Task"), new KnownTypeReference(KnownTypeCode.Task, "System.Threading.Tasks", "Task"),
new KnownTypeReference(KnownTypeCode.TaskOfT, "System.Threading.Tasks", "Task", 1, baseType: KnownTypeCode.Task), new KnownTypeReference(KnownTypeCode.TaskOfT, "System.Threading.Tasks", "Task", 1, baseType: KnownTypeCode.Task),
new KnownTypeReference(KnownTypeCode.NullableOfT, "System", "Nullable", 1, baseType: KnownTypeCode.ValueType), new KnownTypeReference(KnownTypeCode.NullableOfT, "System", "Nullable", 1, baseType: KnownTypeCode.ValueType),
@ -312,16 +315,6 @@ namespace ICSharpCode.NRefactory.TypeSystem
/// </summary> /// </summary>
public static readonly KnownTypeReference UIntPtr = Get(KnownTypeCode.UIntPtr); public static readonly KnownTypeReference UIntPtr = Get(KnownTypeCode.UIntPtr);
/// <summary>
/// Gets a type reference pointing to the <c>System.Collections.Generic.IList{T}</c> type.
/// </summary>
public static readonly KnownTypeReference GenericIList = Get(KnownTypeCode.IListOfT);
/// <summary>
/// Gets a type reference pointing to the <c>System.Nullable{T}</c> type.
/// </summary>
public static readonly KnownTypeReference NullableOfT = Get(KnownTypeCode.NullableOfT);
/// <summary> /// <summary>
/// Gets a type reference pointing to the <c>System.Collections.IEnumerable</c> type. /// Gets a type reference pointing to the <c>System.Collections.IEnumerable</c> type.
/// </summary> /// </summary>
@ -347,6 +340,11 @@ namespace ICSharpCode.NRefactory.TypeSystem
/// </summary> /// </summary>
public static readonly KnownTypeReference IListOfT = Get(KnownTypeCode.IListOfT); public static readonly KnownTypeReference IListOfT = Get(KnownTypeCode.IListOfT);
/// <summary>
/// Gets a type reference pointing to the <c>System.Collections.Generic.IReadOnlyList{T}</c> type.
/// </summary>
public static readonly KnownTypeReference IReadOnlyListOfT = Get(KnownTypeCode.IReadOnlyListOfT);
/// <summary> /// <summary>
/// Gets a type reference pointing to the <c>System.Threading.Tasks.Task</c> type. /// Gets a type reference pointing to the <c>System.Threading.Tasks.Task</c> type.
/// </summary> /// </summary>
@ -357,6 +355,11 @@ namespace ICSharpCode.NRefactory.TypeSystem
/// </summary> /// </summary>
public static readonly KnownTypeReference TaskOfT = Get(KnownTypeCode.TaskOfT); public static readonly KnownTypeReference TaskOfT = Get(KnownTypeCode.TaskOfT);
/// <summary>
/// Gets a type reference pointing to the <c>System.Nullable{T}</c> type.
/// </summary>
public static readonly KnownTypeReference NullableOfT = Get(KnownTypeCode.NullableOfT);
/// <summary> /// <summary>
/// Gets a type reference pointing to the <c>System.IDisposable</c> type. /// Gets a type reference pointing to the <c>System.IDisposable</c> type.
/// </summary> /// </summary>

7
ICSharpCode.NRefactory/Utils/EmptyList.cs

@ -25,17 +25,20 @@ namespace ICSharpCode.NRefactory
{ {
[Serializable] [Serializable]
public sealed class EmptyList<T> : IList<T>, IEnumerator<T> public sealed class EmptyList<T> : IList<T>, IEnumerator<T>
#if NET45
, IReadOnlyList<T>
#endif
{ {
public static readonly EmptyList<T> Instance = new EmptyList<T>(); public static readonly EmptyList<T> Instance = new EmptyList<T>();
private EmptyList() {} private EmptyList() {}
T IList<T>.this[int index] { public T this[int index] {
get { throw new ArgumentOutOfRangeException("index"); } get { throw new ArgumentOutOfRangeException("index"); }
set { throw new ArgumentOutOfRangeException("index"); } set { throw new ArgumentOutOfRangeException("index"); }
} }
int ICollection<T>.Count { public int Count {
get { return 0; } get { return 0; }
} }

Loading…
Cancel
Save