Browse Source

Refactor ParseReflectionName to return IType instead of ITypeReference

pull/3532/head
Siegfried Pammer 5 months ago
parent
commit
33e497eb80
  1. 5
      ICSharpCode.BamlDecompiler/XamlContext.cs
  2. 79
      ICSharpCode.Decompiler.Tests/TypeSystem/ReflectionHelperTests.cs
  3. 4
      ICSharpCode.Decompiler.Tests/TypeSystem/TypeSystemLoaderTests.cs
  4. 2
      ICSharpCode.Decompiler/TypeSystem/FullTypeName.cs
  5. 4
      ICSharpCode.Decompiler/TypeSystem/INamedElement.cs
  6. 4
      ICSharpCode.Decompiler/TypeSystem/ReflectionHelper.cs
  7. 3
      ICSharpCode.Decompiler/TypeSystem/TypeProvider.cs
  8. 3
      ICSharpCode.ILSpyX/Analyzers/Builtin/FindTypeInAttributeDecoder.cs

5
ICSharpCode.BamlDecompiler/XamlContext.cs

@ -27,10 +27,9 @@ using System.Reflection.Metadata;
using System.Threading; using System.Threading;
using System.Xml.Linq; using System.Xml.Linq;
using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.BamlDecompiler.Baml; using ICSharpCode.BamlDecompiler.Baml;
using ICSharpCode.BamlDecompiler.Xaml; using ICSharpCode.BamlDecompiler.Xaml;
using ICSharpCode.Decompiler.TypeSystem;
namespace ICSharpCode.BamlDecompiler namespace ICSharpCode.BamlDecompiler
{ {
@ -123,7 +122,7 @@ namespace ICSharpCode.BamlDecompiler
{ {
var typeRec = Baml.TypeIdMap[id]; var typeRec = Baml.TypeIdMap[id];
(fullAssemblyName, assembly) = Baml.ResolveAssembly(typeRec.AssemblyId); (fullAssemblyName, assembly) = Baml.ResolveAssembly(typeRec.AssemblyId);
type = ReflectionHelper.ParseReflectionName(typeRec.TypeFullName).Resolve(new SimpleTypeResolveContext(TypeSystem)); type = ReflectionHelper.ParseReflectionName(typeRec.TypeFullName, new SimpleTypeResolveContext(TypeSystem));
} }
var clrNs = type.Namespace; var clrNs = type.Namespace;

79
ICSharpCode.Decompiler.Tests/TypeSystem/ReflectionHelperTests.cs

@ -159,138 +159,157 @@ namespace ICSharpCode.Decompiler.Tests.TypeSystem
public void ParseReflectionName() public void ParseReflectionName()
{ {
var context = new SimpleTypeResolveContext(compilation.MainModule); var context = new SimpleTypeResolveContext(compilation.MainModule);
Assert.That(ReflectionHelper.ParseReflectionName("System.Int32").Resolve(context).ReflectionName, Is.EqualTo("System.Int32")); Assert.That(ReflectionHelper.ParseReflectionName("System.Int32", context).ReflectionName, Is.EqualTo("System.Int32"));
Assert.That(ReflectionHelper.ParseReflectionName("System.Int32&").Resolve(context).ReflectionName, Is.EqualTo("System.Int32&")); Assert.That(ReflectionHelper.ParseReflectionName("System.Int32&", context).ReflectionName, Is.EqualTo("System.Int32&"));
Assert.That(ReflectionHelper.ParseReflectionName("System.Int32*&").Resolve(context).ReflectionName, Is.EqualTo("System.Int32*&")); Assert.That(ReflectionHelper.ParseReflectionName("System.Int32*&", context).ReflectionName, Is.EqualTo("System.Int32*&"));
Assert.That(ReflectionHelper.ParseReflectionName(typeof(int).AssemblyQualifiedName).Resolve(context).ReflectionName, Is.EqualTo("System.Int32")); Assert.That(ReflectionHelper.ParseReflectionName(typeof(int).AssemblyQualifiedName, context).ReflectionName, Is.EqualTo("System.Int32"));
Assert.That(ReflectionHelper.ParseReflectionName("System.Action`1[[System.String]]").Resolve(context).ReflectionName, Is.EqualTo("System.Action`1[[System.String]]")); Assert.That(ReflectionHelper.ParseReflectionName("System.Action`1[[System.String]]", context).ReflectionName, Is.EqualTo("System.Action`1[[System.String]]"));
Assert.That(ReflectionHelper.ParseReflectionName("System.Action`1[[System.String, mscorlib]]").Resolve(context).ReflectionName, Is.EqualTo("System.Action`1[[System.String]]")); Assert.That(ReflectionHelper.ParseReflectionName("System.Action`1[[System.String, mscorlib]]", context).ReflectionName, Is.EqualTo("System.Action`1[[System.String]]"));
Assert.That(ReflectionHelper.ParseReflectionName(typeof(int[,][,,]).AssemblyQualifiedName).Resolve(context).ReflectionName, Is.EqualTo("System.Int32[,,][,]")); Assert.That(ReflectionHelper.ParseReflectionName(typeof(int[,][,,]).AssemblyQualifiedName, context).ReflectionName, Is.EqualTo("System.Int32[,,][,]"));
Assert.That(ReflectionHelper.ParseReflectionName("System.Environment+SpecialFolder").Resolve(context).ReflectionName, Is.EqualTo("System.Environment+SpecialFolder")); Assert.That(ReflectionHelper.ParseReflectionName("System.Environment+SpecialFolder", context).ReflectionName, Is.EqualTo("System.Environment+SpecialFolder"));
} }
[Test] [Test]
public void ParseOpenGenericReflectionName() public void ParseOpenGenericReflectionName()
{ {
ITypeReference typeRef = ReflectionHelper.ParseReflectionName("System.Converter`2[[`0],[``0]]"); IType converter = ReflectionHelper.ParseReflectionName("System.Converter`2[[`0],[``0]]", new SimpleTypeResolveContext(compilation.MainModule));
Assert.That(typeRef.Resolve(new SimpleTypeResolveContext(compilation.MainModule)).ReflectionName, Is.EqualTo("System.Converter`2[[`0],[``0]]")); Assert.That(converter.ReflectionName, Is.EqualTo("System.Converter`2[[`0],[``0]]"));
IMethod convertAll = compilation.FindType(typeof(List<>)).GetMethods(m => m.Name == "ConvertAll").Single(); IMethod convertAll = compilation.FindType(typeof(List<>)).GetMethods(m => m.Name == "ConvertAll").Single();
Assert.That(typeRef.Resolve(new SimpleTypeResolveContext(convertAll)).ReflectionName, Is.EqualTo("System.Converter`2[[`0],[``0]]")); IType converter2 = ReflectionHelper.ParseReflectionName("System.Converter`2[[`0],[``0]]", new SimpleTypeResolveContext(convertAll));
Assert.That(converter2.ReflectionName, Is.EqualTo("System.Converter`2[[`0],[``0]]"));
} }
[Test] [Test]
public void ArrayOfTypeParameter() public void ArrayOfTypeParameter()
{ {
var context = new SimpleTypeResolveContext(compilation.MainModule); var context = new SimpleTypeResolveContext(compilation.MainModule);
Assert.That(ReflectionHelper.ParseReflectionName("`0[,]").Resolve(context).ReflectionName, Is.EqualTo("`0[,]")); Assert.That(ReflectionHelper.ParseReflectionName("`0[,]", context).ReflectionName, Is.EqualTo("`0[,]"));
} }
[Test] [Test]
public void ParseNullReflectionName() public void ParseNullReflectionName()
{ {
Assert.Throws<ArgumentNullException>(() => ReflectionHelper.ParseReflectionName(null)); var context = new SimpleTypeResolveContext(compilation.MainModule);
Assert.Throws<ArgumentNullException>(() => ReflectionHelper.ParseReflectionName(null, context));
} }
[Test] [Test]
public void ParseInvalidReflectionName1() public void ParseInvalidReflectionName1()
{ {
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName(string.Empty)); var context = new SimpleTypeResolveContext(compilation.MainModule);
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName(string.Empty, context));
} }
[Test] [Test]
public void ParseInvalidReflectionName2() public void ParseInvalidReflectionName2()
{ {
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("`")); var context = new SimpleTypeResolveContext(compilation.MainModule);
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("`", context));
} }
[Test] [Test]
public void ParseInvalidReflectionName3() public void ParseInvalidReflectionName3()
{ {
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("``")); var context = new SimpleTypeResolveContext(compilation.MainModule);
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("``", context));
} }
[Test] [Test]
public void ParseInvalidReflectionName4() public void ParseInvalidReflectionName4()
{ {
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("System.Action`A")); var context = new SimpleTypeResolveContext(compilation.MainModule);
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("System.Action`A", context));
} }
[Test] [Test]
public void ParseInvalidReflectionName5() public void ParseInvalidReflectionName5()
{ {
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("System.Environment+")); var context = new SimpleTypeResolveContext(compilation.MainModule);
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("System.Environment+", context));
} }
[Test] [Test]
public void ParseInvalidReflectionName5b() public void ParseInvalidReflectionName5b()
{ {
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("System.Environment+`")); var context = new SimpleTypeResolveContext(compilation.MainModule);
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("System.Environment+`", context));
} }
[Test] [Test]
public void ParseInvalidReflectionName6() public void ParseInvalidReflectionName6()
{ {
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("System.Int32[")); var context = new SimpleTypeResolveContext(compilation.MainModule);
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("System.Int32[", context));
} }
[Test] [Test]
public void ParseInvalidReflectionName7() public void ParseInvalidReflectionName7()
{ {
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("System.Int32[`]")); var context = new SimpleTypeResolveContext(compilation.MainModule);
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("System.Int32[`]", context));
} }
[Test] [Test]
public void ParseInvalidReflectionName8() public void ParseInvalidReflectionName8()
{ {
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("System.Int32[,")); var context = new SimpleTypeResolveContext(compilation.MainModule);
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("System.Int32[,", context));
} }
[Test] [Test]
public void ParseInvalidReflectionName9() public void ParseInvalidReflectionName9()
{ {
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("System.Int32]")); var context = new SimpleTypeResolveContext(compilation.MainModule);
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("System.Int32]", context));
} }
[Test] [Test]
public void ParseInvalidReflectionName10() public void ParseInvalidReflectionName10()
{ {
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("System.Int32*a")); var context = new SimpleTypeResolveContext(compilation.MainModule);
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("System.Int32*a", context));
} }
[Test] [Test]
public void ParseInvalidReflectionName11() public void ParseInvalidReflectionName11()
{ {
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("System.Action`1[[]]")); var context = new SimpleTypeResolveContext(compilation.MainModule);
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("System.Action`1[[]]", context));
} }
[Test] [Test]
public void ParseInvalidReflectionName12() public void ParseInvalidReflectionName12()
{ {
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("System.Action`1[[System.Int32]a]")); var context = new SimpleTypeResolveContext(compilation.MainModule);
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("System.Action`1[[System.Int32]a]", context));
} }
[Test] [Test]
public void ParseInvalidReflectionName13() public void ParseInvalidReflectionName13()
{ {
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("System.Action`1[[System.Int32],]")); var context = new SimpleTypeResolveContext(compilation.MainModule);
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("System.Action`1[[System.Int32],]", context));
} }
[Test] [Test]
public void ParseInvalidReflectionName14() public void ParseInvalidReflectionName14()
{ {
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("System.Action`1[[System.Int32]")); var context = new SimpleTypeResolveContext(compilation.MainModule);
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("System.Action`1[[System.Int32]", context));
} }
[Test] [Test]
public void ParseInvalidReflectionName15() public void ParseInvalidReflectionName15()
{ {
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("System.Action`1[[System.Int32")); var context = new SimpleTypeResolveContext(compilation.MainModule);
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("System.Action`1[[System.Int32", context));
} }
[Test] [Test]
public void ParseInvalidReflectionName16() public void ParseInvalidReflectionName16()
{ {
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("System.Action`1[[System.Int32],[System.String")); var context = new SimpleTypeResolveContext(compilation.MainModule);
Assert.Throws<ReflectionNameParseException>(() => ReflectionHelper.ParseReflectionName("System.Action`1[[System.Int32],[System.String", context));
} }
} }
} }

4
ICSharpCode.Decompiler.Tests/TypeSystem/TypeSystemLoaderTests.cs

@ -1952,8 +1952,8 @@ namespace ICSharpCode.Decompiler.Tests.TypeSystem
{ {
var compilationWithSystemCore = new SimpleCompilation(SystemCore, Mscorlib); var compilationWithSystemCore = new SimpleCompilation(SystemCore, Mscorlib);
var typeRef = ReflectionHelper.ParseReflectionName("System.Func`2, System.Core"); var type = ReflectionHelper.ParseReflectionName("System.Func`2, System.Core", new SimpleTypeResolveContext(compilationWithSystemCore));
ITypeDefinition c = typeRef.Resolve(new SimpleTypeResolveContext(compilationWithSystemCore)).GetDefinition(); ITypeDefinition c = type.GetDefinition();
Assert.That(c, Is.Not.Null, "System.Func<,> not found"); Assert.That(c, Is.Not.Null, "System.Func<,> not found");
Assert.That(c.ParentModule.AssemblyName, Is.EqualTo("mscorlib")); Assert.That(c.ParentModule.AssemblyName, Is.EqualTo("mscorlib"));
} }

2
ICSharpCode.Decompiler/TypeSystem/FullTypeName.cs

@ -78,7 +78,7 @@ namespace ICSharpCode.Decompiler.TypeSystem
/// Constructs a FullTypeName by parsing the given reflection name. /// Constructs a FullTypeName by parsing the given reflection name.
/// Note that FullTypeName can only represent type definition names. If the reflection name /// Note that FullTypeName can only represent type definition names. If the reflection name
/// might refer to a parameterized type or array etc., use /// might refer to a parameterized type or array etc., use
/// <see cref="ReflectionHelper.ParseReflectionName(string)"/> instead. /// <see cref="ReflectionHelper.ParseReflectionName(string, ITypeResolveContext)"/> instead.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// Expected syntax: <c>NamespaceName '.' TopLevelTypeName ['`'#] { '+' NestedTypeName ['`'#] }</c> /// Expected syntax: <c>NamespaceName '.' TopLevelTypeName ['`'#] { '+' NestedTypeName ['`'#] }</c>

4
ICSharpCode.Decompiler/TypeSystem/INamedElement.cs

@ -46,8 +46,8 @@ namespace ICSharpCode.Decompiler.TypeSystem
/// Gets the full reflection name of the element. /// Gets the full reflection name of the element.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// For types, the reflection name can be parsed back into a ITypeReference by using /// For types, the reflection name can be parsed back into a IType by using
/// <see cref="ReflectionHelper.ParseReflectionName(string)"/>. /// <see cref="ReflectionHelper.ParseReflectionName(string, ITypeResolveContext)"/>.
/// </remarks> /// </remarks>
/// <returns> /// <returns>
/// "System.Int32[]" for int[]<br/> /// "System.Int32[]" for int[]<br/>

4
ICSharpCode.Decompiler/TypeSystem/ReflectionHelper.cs

@ -260,12 +260,12 @@ namespace ICSharpCode.Decompiler.TypeSystem
/// it will look in all other assemblies of the compilation. /// it will look in all other assemblies of the compilation.
/// </remarks> /// </remarks>
/// <seealso cref="FullTypeName(string)"/> /// <seealso cref="FullTypeName(string)"/>
public static ITypeReference ParseReflectionName(string reflectionTypeName) public static IType ParseReflectionName(string reflectionTypeName, ITypeResolveContext resolveContext)
{ {
if (reflectionTypeName == null) if (reflectionTypeName == null)
throw new ArgumentNullException(nameof(reflectionTypeName)); throw new ArgumentNullException(nameof(reflectionTypeName));
int pos = 0; int pos = 0;
ITypeReference r = ParseReflectionName(reflectionTypeName, ref pos); IType r = ParseReflectionName(reflectionTypeName, ref pos).Resolve(resolveContext);
if (pos < reflectionTypeName.Length) if (pos < reflectionTypeName.Length)
throw new ReflectionNameParseException(pos, "Expected end of type name"); throw new ReflectionNameParseException(pos, "Expected end of type name");
return r; return r;

3
ICSharpCode.Decompiler/TypeSystem/TypeProvider.cs

@ -168,8 +168,7 @@ namespace ICSharpCode.Decompiler.TypeSystem
} }
try try
{ {
return ReflectionHelper.ParseReflectionName(name) return ReflectionHelper.ParseReflectionName(name, module != null ? new SimpleTypeResolveContext(module) : new SimpleTypeResolveContext(compilation));
.Resolve(module != null ? new SimpleTypeResolveContext(module) : new SimpleTypeResolveContext(compilation));
} }
catch (ReflectionNameParseException ex) catch (ReflectionNameParseException ex)
{ {

3
ICSharpCode.ILSpyX/Analyzers/Builtin/FindTypeInAttributeDecoder.cs

@ -112,8 +112,7 @@ namespace ICSharpCode.ILSpyX.Analyzers.Builtin
} }
try try
{ {
IType type = ReflectionHelper.ParseReflectionName(name) IType type = ReflectionHelper.ParseReflectionName(name, new SimpleTypeResolveContext(currentModule));
.Resolve(new SimpleTypeResolveContext(currentModule));
return GetResultFromResolvedType(type); return GetResultFromResolvedType(type);
} }
catch (ReflectionNameParseException) catch (ReflectionNameParseException)

Loading…
Cancel
Save