2
2
// Use of this source code is governed by GNU General Public License (GPL-2.0) that can be found in the COPYING file.
3
3
4
4
using System ;
5
+ using System . IO ;
5
6
using System . Reflection ;
7
+ using System . Text ;
6
8
using Svg ;
7
9
8
10
namespace SvgFileTypePlugin . Extensions ;
9
11
10
12
internal static class SvgElementExtensions
11
13
{
12
- private static readonly MethodInfo ? ElementNameGetter = GetGetMethod ( "ElementName" ) ;
13
- private static readonly MethodInfo ? AttributesGetter = GetGetMethod ( "Attributes" ) ;
14
+ private delegate SvgAttributeCollection AttributesGetterDelegate ( SvgElement element ) ;
15
+ private delegate string ? ElementNameGetterDelegate ( SvgElement element ) ;
16
+
17
+ private static readonly AttributesGetterDelegate GetElementAttributes = CreateGetterDelegate < AttributesGetterDelegate > ( "Attributes" ) ;
18
+ private static readonly ElementNameGetterDelegate GetElementName = CreateGetterDelegate < ElementNameGetterDelegate > ( "ElementName" ) ;
14
19
15
20
public static string GetName ( this SvgElement element )
16
21
{
17
22
ArgumentNullException . ThrowIfNull ( element ) ;
18
23
19
- return element . GetType ( ) . GetCustomAttribute < SvgElementAttribute > ( ) ? . ElementName
20
- ?? ElementNameGetter ? . Invoke ( element , null ) as string
24
+ return element . GetType ( ) . GetCustomAttribute < SvgElementAttribute > ( ) ? . ElementName
25
+ ?? GetElementName ( element )
21
26
?? element . GetType ( ) . Name ;
22
27
}
23
28
24
- public static SvgAttributeCollection ? GetAttributes ( this SvgElement element )
29
+ public static SvgAttributeCollection GetAttributes ( this SvgElement element )
25
30
{
26
31
ArgumentNullException . ThrowIfNull ( element ) ;
27
32
28
- return ( SvgAttributeCollection ? ) AttributesGetter ? . Invoke ( element , null ) ;
33
+ return GetElementAttributes ( element ) ;
29
34
}
30
35
31
36
public static void RemoveInvisibleAndNonTextElements ( this SvgElement element )
@@ -42,8 +47,28 @@ public static void RemoveInvisibleAndNonTextElements(this SvgElement element)
42
47
}
43
48
}
44
49
45
- private static MethodInfo ? GetGetMethod ( string propertyName )
50
+ public static string GetXML_QuotedFuncIRIHack ( this SvgElement svg )
51
+ {
52
+ ArgumentNullException . ThrowIfNull ( svg ) ;
53
+
54
+ return svg . GetXML ( ) . Replace ( """ , string . Empty ) ;
55
+ }
56
+
57
+ public static Stream GetXMLAsStream ( this SvgElement svg )
58
+ {
59
+ ArgumentNullException . ThrowIfNull ( svg ) ;
60
+
61
+ string xml = svg . GetXML_QuotedFuncIRIHack ( ) ;
62
+ byte [ ] bytes = Encoding . UTF8 . GetBytes ( xml ) ;
63
+ return new MemoryStream ( bytes ) ;
64
+ }
65
+
66
+ private static T CreateGetterDelegate < T > ( string propertyName ) where T : Delegate
46
67
{
47
- return typeof ( SvgElement ) . GetProperty ( propertyName , BindingFlags . NonPublic | BindingFlags . Instance ) ? . GetGetMethod ( true ) ;
68
+ MethodInfo getter = typeof ( SvgElement )
69
+ ? . GetProperty ( propertyName , BindingFlags . NonPublic | BindingFlags . Instance )
70
+ ? . GetGetMethod ( true )
71
+ ?? throw new MissingMemberException ( nameof ( SvgElement ) , propertyName ) ;
72
+ return getter . CreateDelegate < T > ( ) ;
48
73
}
49
74
}
0 commit comments