SerializedParameterElement.cs
32 lines
| 1.0 KiB
| text/x-csharp
|
CSharpLexer
cin
|
r274 | using System; | |
cin
|
r271 | using System.Xml; | |
using System.Xml.Schema; | |||
using System.Xml.Serialization; | |||
cin
|
r270 | namespace Implab.ServiceHost.Unity | |
{ | |||
cin
|
r281 | public class SerializedParameterElement : AbstractInjectionParameter { | |
cin
|
r273 | [XmlAttribute("href")] | |
public string Location { get; set; } | |||
cin
|
r271 | ||
[XmlAnyElement] | |||
public XmlElement[] Content { get; set; } | |||
cin
|
r272 | ||
cin
|
r274 | public XmlReader GetReader() { | |
if (!string.IsNullOrEmpty(Location)) | |||
return XmlReader.Create(Location); | |||
if (Content != null && Content.Length > 0) | |||
return Content[0].CreateNavigator().ReadSubtree(); | |||
throw new Exception("No content found, expected XML document"); | |||
} | |||
cin
|
r278 | public override void Visit(InjectionParameterBuilder builder) { | |
cin
|
r277 | var type = builder.ResolveInjectedValueType(TypeName); | |
var serializer = new XmlSerializer(type); | |||
using(var reader = GetReader()) | |||
builder.SetValue(type, serializer.Deserialize(reader)); | |||
cin
|
r272 | } | |
cin
|
r270 | } | |
} |