Skip to content

Commit 79da44e

Browse files
authored
Merge pull request #50 from Cheesebaron/feature/gh-49-overload-and-requiresunref-code-attrs
Add RequiresUnreferencedCode attribute for reflection code and add OverloadResolutionPriority attribute to prefer non-object methods
2 parents 3b2c2d0 + f00da59 commit 79da44e

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

src/Jeffijoe.MessageFormat/Helpers/ObjectHelper.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using System.Collections;
77
using System.Collections.Generic;
8+
using System.Diagnostics.CodeAnalysis;
89
using System.Linq;
910
using System.Reflection;
1011

@@ -26,6 +27,7 @@ internal static class ObjectHelper
2627
/// <returns>
2728
/// The <see cref="IEnumerable" />.
2829
/// </returns>
30+
[RequiresUnreferencedCode("This method uses reflection to read property information on object")]
2931
internal static IEnumerable<PropertyInfo> GetProperties(object obj)
3032
{
3133
var properties = new List<PropertyInfo>();
@@ -54,6 +56,7 @@ internal static IEnumerable<PropertyInfo> GetProperties(object obj)
5456
/// <returns>
5557
/// The <see cref="IDictionary" />.
5658
/// </returns>
59+
[RequiresUnreferencedCode("This method uses reflection to convert object into dictionary")]
5760
internal static Dictionary<string, object?> ToDictionary(this object obj)
5861
{
5962
// We want to be able to read the property, and it should not be an indexer.
@@ -69,4 +72,4 @@ internal static IEnumerable<PropertyInfo> GetProperties(object obj)
6972
}
7073

7174
#endregion
72-
}
75+
}

src/Jeffijoe.MessageFormat/Jeffijoe.MessageFormat.csproj

+13-7
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
<SignAssembly>True</SignAssembly>
55
<AssemblyOriginatorKeyFile>MessageFormat.snk</AssemblyOriginatorKeyFile>
66
<PackageId>MessageFormat</PackageId>
7-
<GenerateAssemblyInfo>True</GenerateAssemblyInfo>
8-
<Authors>Jeff Hansen</Authors>
9-
<PackageTags>messageformat,messageformatter,xamarin,ui,messages,formatting,plural,pluralization,singular,select,strings,stringformat,format</PackageTags>
10-
<RepositoryUrl>https://github.com/jeffijoe/messageformat.net</RepositoryUrl>
11-
<LangVersion>latest</LangVersion>
12-
<Nullable>enable</Nullable>
13-
<TargetFrameworks>net6.0;net8.0;netstandard2.0;netstandard2.1</TargetFrameworks>
7+
<GenerateAssemblyInfo>True</GenerateAssemblyInfo>
8+
<Authors>Jeff Hansen</Authors>
9+
<PackageTags>messageformat,messageformatter,xamarin,ui,messages,formatting,plural,pluralization,singular,select,strings,stringformat,format</PackageTags>
10+
<RepositoryUrl>https://github.com/jeffijoe/messageformat.net</RepositoryUrl>
11+
<LangVersion>latest</LangVersion>
12+
<Nullable>enable</Nullable>
13+
<TargetFrameworks>net6.0;net8.0;netstandard2.0;netstandard2.1</TargetFrameworks>
14+
<PolySharpIncludeRuntimeSupportedAttributes>true</PolySharpIncludeRuntimeSupportedAttributes>
15+
<IsTrimmable>true</IsTrimmable>
1416
</PropertyGroup>
1517

1618
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
@@ -23,6 +25,10 @@
2325
<PrivateAssets>all</PrivateAssets>
2426
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2527
</PackageReference>
28+
<PackageReference Include="PolySharp" Version="1.15.0">
29+
<PrivateAssets>all</PrivateAssets>
30+
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
31+
</PackageReference>
2632
</ItemGroup>
2733

2834
<ItemGroup>

src/Jeffijoe.MessageFormat/MessageFormatter.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
using System;
77
using System.Collections.Generic;
88
using System.Collections.Concurrent;
9+
using System.Diagnostics.CodeAnalysis;
910
using System.Linq;
11+
using System.Runtime.CompilerServices;
1012
using System.Text;
1113
using Jeffijoe.MessageFormat.Formatting;
1214
using Jeffijoe.MessageFormat.Formatting.Formatters;
@@ -202,6 +204,8 @@ public static string Format(string pattern, IReadOnlyDictionary<string, object?>
202204
/// <returns>
203205
/// The formatted message.
204206
/// </returns>
207+
[OverloadResolutionPriority(-1)]
208+
[RequiresUnreferencedCode("This method uses the FormatMessage extension which uses reflection to convert object into dictionary")]
205209
public static string Format(string pattern, object data)
206210
{
207211
lock (Lock)
@@ -390,4 +394,4 @@ private IFormatterRequestCollection ParseRequests(string pattern, StringBuilder
390394
}
391395

392396
#endregion
393-
}
397+
}

src/Jeffijoe.MessageFormat/MessageFormatterExtensions.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System.Collections.Generic;
2+
using System.Diagnostics.CodeAnalysis;
3+
using System.Runtime.CompilerServices;
24
using Jeffijoe.MessageFormat.Helpers;
35

46
namespace Jeffijoe.MessageFormat;
@@ -46,8 +48,10 @@ public static string FormatMessage(
4648
/// <returns>
4749
/// The <see cref="string" />.
4850
/// </returns>
51+
[OverloadResolutionPriority(-1)]
52+
[RequiresUnreferencedCode("This method uses the ToDictionary extension which uses reflection to convert object into dictionary")]
4953
public static string FormatMessage(this IMessageFormatter formatter, string pattern, object args)
5054
{
5155
return formatter.FormatMessage(pattern, args.ToDictionary());
5256
}
53-
}
57+
}

0 commit comments

Comments
 (0)