Skip to content

Commit

Permalink
Implement TypeRef.GetCustomAttributes(bool)
Browse files Browse the repository at this point in the history
  • Loading branch information
MSDN-WhiteKnight committed Jul 2, 2023
1 parent 1ae0854 commit a3b9f62
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 21 deletions.
5 changes: 4 additions & 1 deletion CilTools.Metadata/TypeRef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,10 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit)

public override object[] GetCustomAttributes(bool inherit)
{
return new object[] { };
this.LoadImpl();

if (this.impl != null) return this.impl.GetCustomAttributes(inherit);
else return new object[] { };
}

public override bool IsDefined(Type attributeType, bool inherit)
Expand Down
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
<PropertyGroup>
<Version>2.8.0</Version>
<FileVersion>2.8.0.0</FileVersion>
<Authors>MSDN-WhiteKnight</Authors>
<Authors>SmallSoft</Authors>
<Company>CIL Tools</Company>
<Product>CIL Tools</Product>
<Copyright>Copyright (c) 2023, MSDN.WhiteKnight</Copyright>
<Copyright>Copyright (c) 2023, SmallSoft</Copyright>
<PackageProjectUrl>https://gitflic.ru/project/smallsoft/ciltools</PackageProjectUrl>
<RepositoryUrl>https://gitflic.ru/project/smallsoft/ciltools</RepositoryUrl>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2019, MSDN.WhiteKnight
Copyright (c) 2023, SmallSoft
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

**License:** [BSD 2.0](https://gitflic.ru/project/smallsoft/ciltools/blob?file=LICENSE&branch=master)

[![gitflic-catalog](https://img.shields.io/badge/gitflic--catalog-blue)](https://gitflic.ru/project/smallsoft/gitflic-catalog) &nbsp; [![GitHub release (latest by date)](https://img.shields.io/github/v/release/MSDN-WhiteKnight/CilTools)](https://github.com/MSDN-WhiteKnight/CilTools/releases)

[Documentation](https://msdn-whiteknight.github.io/CilTools/) | [Examples](https://gitflic.ru/project/smallsoft/ciltools/file?file=Examples&branch=master)

CIL tools is a set of software to work with Common Intermediate Language in .NET:
Expand All @@ -18,7 +20,7 @@ CIL tools is a set of software to work with Common Intermediate Language in .NET

**Requirements:** .NET Framework 3.5+ or .NET Standard 2.0+

[![Nuget](https://img.shields.io/nuget/v/CilTools.BytecodeAnalysis)](https://www.nuget.org/packages/CilTools.BytecodeAnalysis/) &nbsp; [![GitHub release (latest by date)](https://img.shields.io/github/v/release/MSDN-WhiteKnight/CilTools)](https://github.com/MSDN-WhiteKnight/CilTools/releases)
[![Nuget](https://img.shields.io/nuget/v/CilTools.BytecodeAnalysis)](https://www.nuget.org/packages/CilTools.BytecodeAnalysis/)

CilTools.BytecodeAnalysis reads .NET methods' Common Intermediate Language (CIL) bytecode and converts it into high-level objects or textual CIL representation so they can be easily studied and programmatically processed.

Expand Down Expand Up @@ -113,7 +115,7 @@ Install as global .NET tool:

dotnet tool install --global CilTools.CommandLine

For more information see [readme file](https://github.com/MSDN-WhiteKnight/CilTools/blob/master/CilTools.CommandLine/readme.md).
For more information see [readme file](https://gitflic.ru/project/smallsoft/ciltools/blob?file=CilTools.CommandLine%2Freadme.md&branch=master).

## CilView

Expand All @@ -127,4 +129,4 @@ A windows application to display CIL code of methods in the given assembly file

---

Copyright (c) 2023, MSDN.WhiteKnight
Copyright (c) 2023, SmallSoft
4 changes: 2 additions & 2 deletions browser.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
; CIL Browser options (https://github.com/MSDN-WhiteKnight/CilBrowser)
; CIL Browser options (https://gitflic.ru/project/smallsoft/cilbrowser)
SourceControlURL=https://github.com/MSDN-WhiteKnight/CilTools/blob/master/
SourceExtensions=cs,csproj,sln
SourceExtensions=cs,csproj,sln,xaml
6 changes: 3 additions & 3 deletions docfx_project/articles/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CIL Tools consist of the following projects:
- [CilTools.BytecodeAnalysis](https://www.nuget.org/packages/CilTools.BytecodeAnalysis/)
- [CilTools.Runtime](https://www.nuget.org/packages/CilTools.Runtime/)
- [CilTools.Metadata](https://www.nuget.org/packages/CilTools.Metadata/)
- [CIL View](https://github.com/MSDN-WhiteKnight/CilTools/tree/master/CilView)
- [CIL View](https://gitflic.ru/project/smallsoft/ciltools/file?file=CilView&branch=master)

Documentation pages:

Expand All @@ -21,8 +21,8 @@ Documentation pages:

## See also

[Source code](https://github.com/MSDN-WhiteKnight/CilTools)
[Source code](https://gitflic.ru/project/smallsoft/ciltools)

[Releases](https://github.com/MSDN-WhiteKnight/CilTools/releases)

[Examples](https://github.com/MSDN-WhiteKnight/CilTools/tree/master/Examples)
[Examples](https://gitflic.ru/project/smallsoft/ciltools/file?file=Examples&branch=master)
24 changes: 22 additions & 2 deletions tests/CilTools.Metadata.Tests/ReaderFactory.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
/* CilTools.Metadata tests
* Copyright (c) 2021, MSDN.WhiteKnight (https://github.com/MSDN-WhiteKnight)
* Copyright (c) 2023, MSDN.WhiteKnight (https://github.com/MSDN-WhiteKnight)
* License: BSD 2.0 */
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using CilTools.Tests.Common;

namespace CilTools.Metadata.Tests
{
static class ReaderFactory
{
static readonly AssemblyReader s_globalReader = new AssemblyReader();
static readonly AssemblyReader s_globalReader;

static ReaderFactory()
{
s_globalReader = new AssemblyReader();
s_globalReader.AssemblyResolve += globalReader_AssemblyResolve;
}

static Assembly globalReader_AssemblyResolve(object sender, ResolveEventArgs args)
{
AssemblyName an = new AssemblyName(args.Name);

if (an.Name == "CilTools.Tests.Common")
{
//manually resolve CilTools.Tests.Common, as it is not resolved automatically under tests runner
return s_globalReader.LoadFrom(typeof(SampleMethods).Assembly.Location);
}
else return null;
}

public static AssemblyReader GetReader()
{
Expand Down
9 changes: 2 additions & 7 deletions tests/CilTools.Metadata.Tests/TypeDefTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,20 +1007,15 @@ public void Test_GetCustomAttributes_Empty(Type t)
Assert.AreEqual(0, attrs.Length);
}

static void VerifyCustomAtrribute(object attr, string expectedType)
{
Assert.AreEqual(expectedType, ((ICustomAttribute)attr).Constructor.DeclaringType.FullName);
}

[TestMethod]
[TypeTestData(typeof(AttrInheritanceTestType), BytecodeProviders.Metadata)]
public void Test_GetCustomAttributes_Inherit(Type t)
{
object[] attrs = t.GetCustomAttributes(true);

Assert.AreEqual(2, attrs.Length);
VerifyCustomAtrribute(attrs[0], "System.ComponentModel.CategoryAttribute");
VerifyCustomAtrribute(attrs[1], "CilTools.Tests.Common.MyAttribute");
AssertThat.CustomAtrributeIsOfType(attrs[0], "System.ComponentModel.CategoryAttribute");
AssertThat.CustomAtrributeIsOfType(attrs[1], "CilTools.Tests.Common.MyAttribute");
}
}
}
22 changes: 22 additions & 0 deletions tests/CilTools.Metadata.Tests/TypeRefTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,27 @@ public void Test_IsEnum()
t = TypeRefTests_Data.GetTypeRef(typeof(Console).FullName);
Assert.IsFalse(t.IsEnum);
}

[TestMethod]
public void Test_GetCustomAttributes()
{
Type t = TypeRefTests_Data.GetTypeRef(typeof(TestType).FullName);
object[] attrs = t.GetCustomAttributes(false);

Assert.AreEqual(1, attrs.Length);
AssertThat.CustomAtrributeIsOfType(attrs[0], "CilTools.Tests.Common.MyAttribute");
}

[TestMethod]
public void Test_GetCustomAttributes_Empty()
{
Type t = TypeRefTests_Data.GetTypeRef(typeof(Console).FullName);
object[] attrs = t.GetCustomAttributes(false);
Assert.AreEqual(0, attrs.Length);

attrs = t.GetCustomAttributes(true);
Assert.AreEqual(0, attrs.Length);
}
}

public class TypeRefTests_Data
Expand All @@ -113,6 +134,7 @@ public static void Method()
Console.WriteLine(typeof(AttributeTargets));
Console.WriteLine(typeof(Console));
Console.WriteLine(typeof(IDisposable));
Console.WriteLine(typeof(TestType));
}

public static Type GetTypeRef(string typeName)
Expand Down
10 changes: 10 additions & 0 deletions tests/CilTools.Tests.Common/AssertThat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Linq;
using System.Text;
using CilTools.BytecodeAnalysis;
using CilTools.Reflection;
using CilTools.Syntax;
using CilTools.Tests.Common.TextUtils;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -382,5 +383,14 @@ public static void CilContains(string testedString, string stringToFind)
const string mes = "AssertThat.CilContains failed. Tested string does not contain the specified string.";
Assert.IsTrue(s2.Contains(s1), mes);
}

/// <summary>
/// Assert that specified custom attribute's type full name matches the specified string. Only works with custom
/// attribute objects implementing <see cref="ICustomAttribute"/>.
/// </summary>
public static void CustomAtrributeIsOfType(object attr, string expectedType)
{
Assert.AreEqual(expectedType, ((ICustomAttribute)attr).Constructor.DeclaringType.FullName);
}
}
}

0 comments on commit a3b9f62

Please sign in to comment.