##// END OF EJS Templates
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)...
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete) Implab.ServiceHost: rewritten TypeReference (added support for nested types), stable API

File last commit:

r278:6691aff01de1 v3
r278:6691aff01de1 v3
Show More
InjectionValueBuilder.cs
91 lines | 3.1 KiB | text/x-csharp | CSharpLexer
cin
Unity xml configuration, alpha2
r274 using System;
cin
Refactoring...
r277 using System.Collections.Generic;
cin
Unity xml configuration, alpha2
r274 using System.ComponentModel;
cin
Refactoring...
r277 using System.Linq;
cin
Unity xml configuration, alpha2
r274 using System.Xml.Serialization;
using Unity.Injection;
namespace Implab.ServiceHost.Unity {
cin
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)...
r278 public class InjectionParameterBuilder {
cin
Unity xml configuration, alpha2
r274
readonly TypeResolver m_resolver;
public Type DefaultType { get; private set; }
cin
Refactoring...
r277 public Type ValueType { get; private set; }
cin
Unity xml configuration, alpha2
r274
public object Value { get; set; }
cin
Refactoring...
r277 internal InjectionParameterValue Injection {
cin
Unity xml configuration, alpha2
r274 get {
if (Value != null)
return InjectionParameterValue.ToParameter(Value);
return new InjectionParameter(ValueType, null);
}
}
cin
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)...
r278 internal InjectionParameterBuilder(TypeResolver resolver, Type defaultType) {
cin
Unity xml configuration, alpha2
r274 m_resolver = resolver;
cin
Refactoring...
r277 DefaultType = defaultType;
cin
Unity xml configuration, alpha2
r274 }
cin
Refactoring...
r277 public Type ResolveInjectedValueType(string typeSpec) {
cin
Unity xml configuration, alpha2
r274 if (string.IsNullOrEmpty(typeSpec)) {
if (DefaultType == null)
throw new Exception("The type must be specified");
return DefaultType;
}
cin
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)...
r278 return m_resolver.Resolve(typeSpec, true);
cin
Unity xml configuration, alpha2
r274 }
cin
Refactoring...
r277 public Type ResolveType(string typeSpec) {
cin
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)...
r278 return m_resolver.Resolve(typeSpec, true);
cin
Refactoring...
r277 }
cin
Unity xml configuration, alpha2
r274
cin
Refactoring...
r277 public void SetValue(Type type, object value) {
ValueType = type;
Value = value;
}
public void SetValue<T>(T value) {
SetValue(typeof(T), value);
}
public void SetDependencyReference(Type type, string name, bool optional) {
ValueType = type;
Value = optional ? (object)new OptionalParameter(type, name) : new ResolvedParameter(type, name);
cin
Unity xml configuration, alpha2
r274 }
cin
Refactoring...
r277 internal void Visit(ArrayParameterElement arrayParameter) {
Type itemsType = null;
var arrayType = string.IsNullOrEmpty(arrayParameter.TypeName) ? null : ResolveType(arrayParameter.TypeName);
cin
Unity xml configuration, alpha2
r274
cin
Refactoring...
r277 if (!string.IsNullOrEmpty(arrayParameter.ItemsType)) {
itemsType = ResolveType(arrayParameter.ItemsType);
if (arrayType == null)
arrayType = itemsType.MakeArrayType();
} else {
itemsType = arrayType?.GetInterface(typeof(IEnumerable<>).FullName)?.GetGenericArguments()[0];
}
cin
Unity xml configuration, alpha2
r274
cin
Refactoring...
r277 if (itemsType == null)
throw new Exception("Failed to determine array elements type");
cin
Unity xml configuration, alpha2
r274
cin
Refactoring...
r277 InjectionParameterValue[] injections = (arrayParameter.Items ?? new InjectionParameterElement[0])
.Select(x => {
cin
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)...
r278 var builder = new InjectionParameterBuilder(m_resolver, itemsType);
cin
Refactoring...
r277 x.Visit(builder);
return builder.Injection;
})
.ToArray();
cin
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)...
r278 var array = itemsType.IsGenericParameter ?
(object)new GenericResolvedArrayParameter(itemsType.Name, injections) :
new ResolvedArrayParameter(itemsType, injections);
cin
Refactoring...
r277 ValueType = arrayType;
Value = array;
cin
Unity xml configuration, alpha2
r274 }
}
}