|
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; |
2 | 7 | using System.Diagnostics.CodeAnalysis; |
3 | 8 | using System.Text; |
4 | | -using Oxage.Wmf.Records; |
5 | 9 |
|
6 | 10 | using Color = Oxage.Wmf.Primitive.WmfColor; |
7 | 11 | using Point = Oxage.Wmf.Primitive.WmfPoint; |
8 | | -using Oxage.Wmf.Primitive; |
9 | 12 |
|
10 | 13 | namespace Oxage.Wmf |
11 | 14 | { |
12 | | - public class WmfHelper |
| 15 | + public partial class WmfHelper |
13 | 16 | { |
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) |
19 | 18 | { |
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 | + } |
34 | 23 |
|
35 | 24 | return null; |
36 | 25 | } |
37 | 26 |
|
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() |
39 | 53 | { |
40 | 54 | //ANSI Encoding: http://weblogs.asp.net/ahoffman/archive/2004/01/19/60094.aspx |
41 | 55 | //Not sure, should be Encoding.Default? Documentation says "ANSI Character Set" but not specifically which code page |
|
0 commit comments