Skip to content

Commit debc674

Browse files
committed
支持 aot 构建减少反射代码实现
1 parent f8727f5 commit debc674

3 files changed

Lines changed: 43 additions & 24 deletions

File tree

Workbench/Wmf/SkiaWmfRenderer/samples/sample/Sample.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<TargetFramework>net9.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
8+
<PublishAot>true</PublishAot>
89
</PropertyGroup>
910

1011
<ItemGroup>

Workbench/Wmf/SkiaWmfRenderer/src/wieslawsoltes-wmf/src/library/Library.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424

2525
<ItemGroup>
2626
<PackageReference Include="dotnetCampus.LatestCSharpFeatures" Version="12.0.1" />
27+
<PackageReference Include="dotnetCampus.Telescope.SourceGeneratorAnalyzers" Version="0.10.7-alpha21">
28+
<PrivateAssets>all</PrivateAssets>
29+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
30+
</PackageReference>
2731
</ItemGroup>
2832

2933
</Project>

Workbench/Wmf/SkiaWmfRenderer/src/wieslawsoltes-wmf/src/library/WmfHelper.cs

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,55 @@
1-
using System;
1+
#nullable enable
2+
using Oxage.Wmf.Primitive;
3+
using Oxage.Wmf.Records;
4+
5+
using System;
6+
using System.Collections.Generic;
27
using System.Diagnostics.CodeAnalysis;
38
using System.Text;
4-
using Oxage.Wmf.Records;
59

610
using Color = Oxage.Wmf.Primitive.WmfColor;
711
using Point = Oxage.Wmf.Primitive.WmfPoint;
8-
using Oxage.Wmf.Primitive;
912

1013
namespace Oxage.Wmf
1114
{
12-
public class WmfHelper
15+
public partial class WmfHelper
1316
{
14-
#if NET9_0_OR_GREATER
15-
[RequiresDynamicCode("")]
16-
[RequiresUnreferencedCode("xxx")]
17-
#endif
18-
public static IBinaryRecord GetRecordByType(RecordType rt)
17+
public static IBinaryRecord? GetRecordByType(RecordType rt)
1918
{
20-
var types = typeof(WmfHelper).Assembly.GetTypes();
21-
22-
foreach (var type in types)
23-
{
24-
if (typeof(IBinaryRecord).IsAssignableFrom(type))
25-
{
26-
var attribute = Attribute.GetCustomAttribute(type, typeof(WmfRecordAttribute)) as WmfRecordAttribute;
27-
if (attribute != null && attribute.Type == rt)
28-
{
29-
var record = Activator.CreateInstance(type) as IBinaryRecord;
30-
return record;
31-
}
32-
}
33-
}
19+
if (RecordCreatorDictionary.TryGetValue(rt,out var creator))
20+
{
21+
return creator.Creator();
22+
}
3423

3524
return null;
3625
}
3726

38-
public static Encoding GetAnsiEncoding()
27+
private static Dictionary<RecordType, RecordCreator> RecordCreatorDictionary
28+
{
29+
get
30+
{
31+
if (_recordCreatorDictionary is null)
32+
{
33+
var dictionary = new Dictionary<RecordType, RecordCreator>();
34+
foreach (var (type, attribute, creator) in ExportBinaryRecordEnumerable())
35+
{
36+
dictionary.Add(attribute.Type, new RecordCreator(type, attribute, creator));
37+
}
38+
_recordCreatorDictionary = dictionary;
39+
}
40+
41+
return _recordCreatorDictionary;
42+
}
43+
}
44+
45+
private static Dictionary<RecordType, RecordCreator>? _recordCreatorDictionary;
46+
47+
readonly record struct RecordCreator(Type Type, WmfRecordAttribute Attribute, Func<IBinaryRecord> Creator);
48+
49+
[dotnetCampus.Telescope.TelescopeExportAttribute()]
50+
private static partial IEnumerable<(Type type, WmfRecordAttribute attribute, Func<IBinaryRecord> creator)> ExportBinaryRecordEnumerable();
51+
52+
public static Encoding GetAnsiEncoding()
3953
{
4054
//ANSI Encoding: http://weblogs.asp.net/ahoffman/archive/2004/01/19/60094.aspx
4155
//Not sure, should be Encoding.Default? Documentation says "ANSI Character Set" but not specifically which code page

0 commit comments

Comments
 (0)