/// <summary> /// Extends GetEntity so that methods that return a specific type object given a Type parameter can be /// used as generic method and casting is not required. /// <example> /// xmlxapresolver.GetEntity<int>(absoluteUri, role); /// </example> /// </summary> public static T GetEntity <T>(this XmlXapResolver xmlxapresolver, Uri absoluteUri, String role) { if (xmlxapresolver == null) { throw new ArgumentNullException("xmlxapresolver"); } return((T)xmlxapresolver.GetEntity(absoluteUri, role, typeof(T))); }
public void GetEntity() { XmlXapResolver xr = new XmlXapResolver(); Assert.Throws <ArgumentNullException> (delegate { xr.GetEntity(null, "role", typeof(Stream)); }, "null,string,type"); Uri absolute = new Uri("http://www.mono-project.com/", UriKind.Absolute); Assert.Throws <XmlException> (delegate { xr.GetEntity(absolute, "role", typeof(Stream)); }, "absolute,string,type"); Uri relative = new Uri(String.Empty, UriKind.Relative); Assert.Throws <IndexOutOfRangeException> (delegate { xr.GetEntity(relative, "role", typeof(Stream)); }, "empty,string,type"); relative = new Uri("AppManifest.xaml", UriKind.Relative); Stream s = (Stream)xr.GetEntity(relative, null, typeof(Stream)); Assert.IsNotNull(s, "relative,null,type"); Assert.IsTrue(s is Stream, "Stream"); s = (Stream)xr.GetEntity(relative, String.Empty, null); Assert.IsNotNull(s, "relative,empty,null"); Assert.Throws <XmlException> (delegate { xr.GetEntity(relative, null, s.GetType()); }, "relative,string,type/bad"); absolute = new Uri(Application.Current.Host.Source, relative); Assert.Throws <XmlException> (delegate { xr.GetEntity(absolute, String.Empty, null); }, "absolute(app),empty,null"); absolute = new Uri(Application.Current.Host.Source + "#AppManifest.xaml"); Assert.Throws <XmlException> (delegate { xr.GetEntity(absolute, String.Empty, null); }, "absolute(fragment),empty,null"); relative = new Uri("does-not-exists.file", UriKind.Relative); Assert.Throws <XmlException> (delegate { xr.GetEntity(relative, String.Empty, null); }, "relative(doesnotexists),empty,null"); }