22// Use of this source code is governed by GNU General Public License (GPL-2.0) that can be found in the COPYING file.
33
44using System ;
5+ using System . IO ;
56using System . Reflection ;
7+ using System . Text ;
68using Svg ;
79
810namespace SvgFileTypePlugin . Extensions ;
911
1012internal static class SvgElementExtensions
1113{
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" ) ;
1419
1520 public static string GetName ( this SvgElement element )
1621 {
1722 ArgumentNullException . ThrowIfNull ( element ) ;
1823
19- return element . GetType ( ) . GetCustomAttribute < SvgElementAttribute > ( ) ? . ElementName
20- ?? ElementNameGetter ? . Invoke ( element , null ) as string
24+ return element . GetType ( ) . GetCustomAttribute < SvgElementAttribute > ( ) ? . ElementName
25+ ?? GetElementName ( element )
2126 ?? element . GetType ( ) . Name ;
2227 }
2328
24- public static SvgAttributeCollection ? GetAttributes ( this SvgElement element )
29+ public static SvgAttributeCollection GetAttributes ( this SvgElement element )
2530 {
2631 ArgumentNullException . ThrowIfNull ( element ) ;
2732
28- return ( SvgAttributeCollection ? ) AttributesGetter ? . Invoke ( element , null ) ;
33+ return GetElementAttributes ( element ) ;
2934 }
3035
3136 public static void RemoveInvisibleAndNonTextElements ( this SvgElement element )
@@ -42,8 +47,28 @@ public static void RemoveInvisibleAndNonTextElements(this SvgElement element)
4247 }
4348 }
4449
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
4667 {
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 > ( ) ;
4873 }
4974}
0 commit comments