/// <summary> /// Produce a <see cref="ContractCollection"/> containing all contracts found in the provided XML <paramref name="element"/>. /// </summary> /// <param name="element">The XML element to parse.</param> /// <returns>A contract collection containing the contracts found in the XML element.</returns> /// <exception cref="ContractParseException">Parsing failed.</exception> public static ContractCollection FromXml(XElement element) { var bindActions = new List <Action>(); var collection = new ContractCollection { AllowResolution = false }; foreach (var el in element.Elements()) { var id = el.Attribute("Id")?.Value; if (string.IsNullOrWhiteSpace(id)) { throw new ContractParseException("Contract XML element must contain a non-empty \"Id\" attribute."); } ByRefContract contract; switch (el.Name.LocalName) { case "ComplexRead": contract = new ComplexReadContract(el, collection.ResolveReadContract, bindActions); break; case "ComplexWrite": contract = new ComplexWriteContract(el, collection.ResolveWriteContract, bindActions); break; case "UnionRead": contract = new UnionReadContract(el, collection.ResolveReadContract, bindActions); break; case "UnionWrite": contract = new UnionWriteContract(el, collection.ResolveWriteContract, bindActions); break; case "Enum": contract = new EnumContract(el); break; default: throw new ContractParseException($"Unsupported contract XML element with name \"{el.Name.LocalName}\"."); } contract.Id = id; collection._contracts.Add(contract); } collection.AllowResolution = true; foreach (var bindAction in bindActions) { bindAction(); } return(collection); }
/// <summary> /// Get or create a write contract for <paramref name="type"/>. /// </summary> /// <param name="type">The type to get or create a write contract for.</param> /// <returns>The write contract corresponding to <paramref name="type"/>.</returns> public IWriteContract GetOrAddWriteContract(Type type) { if (type == typeof(Empty)) { return(Intern(new EmptyContract())); } if (PrimitiveContract.CanProcess(type)) { return(Intern(new PrimitiveContract(type))); } if (EnumContract.CanProcess(type)) { return(Intern(new EnumContract(type))); } if (TupleWriteContract.CanProcess(type)) { return(Intern(new TupleWriteContract(type, this))); } if (NullableWriteContract.CanProcess(type)) { return(Intern(new NullableWriteContract(type, this))); } if (ListWriteContract.CanProcess(type)) { return(Intern(new ListWriteContract(type, this))); } if (DictionaryWriteContract.CanProcess(type)) { return(Intern(new DictionaryWriteContract(type, this))); } if (UnionWriteContract.CanProcess(type)) { return(Intern(new UnionWriteContract(type, this))); } return(Intern(new ComplexWriteContract(type, this))); }