5 // Atsushi Enomoto (atsushi@ximian.com)
7 // (C) 2009 Novell Inc.
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System
.Reflection
;
37 public class XmlXapResolver
: XmlResolver
39 static readonly MethodInfo method
;
40 static readonly PropertyInfo property
;
41 static XmlXapResolver ()
43 var at
= Type
.GetType ("System.Windows.Application, System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e", true);
44 method
= at
.GetMethod ("GetResourceStream", new Type
[] {typeof (Uri)}
);
45 var rt
= Type
.GetType ("System.Windows.Resources.StreamResourceInfo, System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e", true);
46 property
= rt
.GetProperty ("Stream");
49 public override object GetEntity (Uri absoluteUri
, string role
, Type ofObjectToReturn
)
51 if (absoluteUri
== null)
52 throw new ArgumentNullException ("absoluteUri");
53 if (absoluteUri
.IsAbsoluteUri
)
54 throw new XmlException ("URI must be relative to the application XAP");
55 if (!SupportsType (absoluteUri
, ofObjectToReturn
))
56 throw new XmlException (String
.Format ("Unsupported entity type '{0}'", ofObjectToReturn
.GetType ()));
59 var sri
= method
.Invoke (null, new object [] {absoluteUri}
);
61 throw new XmlException (String
.Format ("Resource '{0}' not found", absoluteUri
));
62 return property
.GetValue (sri
, new object [0]);
63 } catch (TargetInvocationException tie
) {
64 // throw the original exception
65 throw tie
.InnerException
;