public void VisitElement(XElement element, IContainer container, IInitializationContext context) { string?abstractTypeName = element.Attributes(AbstractTypeAttributeName) .Select(attr => attr.Value) .FirstOrDefault(); string?concreteClassName = element.Attributes(ConcreteTypeAttributeName) .Select(attr => attr.Value) .FirstOrDefault(); bool isSingleton = element .Attributes(SingletonAttributeName) .Any(a => singletonIsTrueValues.Contains(a.Value.ToLower())); if (abstractTypeName == null || concreteClassName == null) { throw new ConfirurationFileFormatException("Bind element should have abstarct and concrete attributes!"); } var abstractType = context.GetType(abstractTypeName); var concreteType = context.GetType(concreteClassName); var binder = container.Bind(abstractType); if (isSingleton) { binder = binder.Singleton(); } if (!element.HasElements) { binder.To(concreteType); } else { var factoryMethod = CreateFactoryMethodFor(concreteType, element, context); binder.ToMethod(factoryMethod); } }
public override Expression VisitElement(XElement element, IInitializationContext context) { var className = element .Attributes(ClassAttributeName) .Select(attr => attr.Value) .FirstOrDefault(); if (className == null) { throw new ConfirurationFileFormatException("Object element should have class attribute"); } var type = context.GetType(className); if (element.HasElements) { var subExpression = element .Elements() .Select(e => builder.BuildObjectBuildingExpressionTree(e, context)) .ToArray(); var types = subExpression .Select(expression => expression.Type) .ToArray(); var constructor = type.GetConstructor(types); if (constructor == null) { var typesStr = string.Join(", ", types.Select(t => t.FullName).ToArray()); throw new ConfirurationFileFormatException($"Class {className} doesn't have constructor with parameter types \"{typesStr}\""); } return(Expression.New(constructor, subExpression)); } else { var method = CreateInstanceMethod.MakeGenericMethod(new[] { type }); return(Expression.Call(builder.containerParameter, method, new Expression[0])); } }
public override Expression VisitElement(XElement element, IInitializationContext context) { if (element.HasElements) { throw new ConfirurationFileFormatException("Instance element shouldn't have child elements"); } var typeAttribute = element.Attributes(TypeAttributeName) .Select(attr => attr.Value) .FirstOrDefault(); if (typeAttribute == null) { throw new ConfirurationFileFormatException("Instance element should have type attribute"); } var type = context.GetType(typeAttribute); var method = GetGenericMethod.MakeGenericMethod(new[] { type }); return(Expression.Call(builder.containerParameter, method, new Expression[0])); }