public void TestPrimitiveHeaderGeneration() { ProjectMappingManagerSetup.InitializeManager(ServiceProvider, "ProjectMapping.DataContractDsl.Tests.xml"); PrimitiveDataTypeCollection collectionElement = CreateDefaultPrimitiveDataTypeCollection(typeof(Collection <>), PrimitiveDataElementType1); string content = RunTemplate(collectionElement); Type generatedType = CompileAndGetType(content); Assert.IsTrue(generatedType.IsClass); Assert.AreEqual <Type>(typeof(Collection <int>), generatedType.BaseType); CollectionDataContractAttribute collectionAttr = TypeAsserter.AssertAttribute <CollectionDataContractAttribute>(generatedType); Assert.AreEqual <string>(ElementNamespace, collectionAttr.Namespace); }
public void Ctor_Default() { var attribute = new CollectionDataContractAttribute(); Assert.False(attribute.IsReference); Assert.False(attribute.IsReferenceSetExplicitly); Assert.Null(attribute.ItemName); Assert.False(attribute.IsItemNameSetExplicitly); Assert.Null(attribute.Name); Assert.False(attribute.IsNameSetExplicitly); Assert.Null(attribute.Namespace); Assert.False(attribute.IsNamespaceSetExplicitly); Assert.Null(attribute.KeyName); Assert.False(attribute.IsKeyNameSetExplicitly); Assert.Null(attribute.ValueName); Assert.False(attribute.IsValueNameSetExplicitly); }
public void TestHeaderGeneration() { ProjectMappingManagerSetup.InitializeManager(ServiceProvider, "ProjectMapping.DataContractDsl.Tests.xml"); DataContractCollection collectionElement = CreateDefaultDataContractCollectionElement(typeof(Collection <>)); string content = RunTemplate(collectionElement); this.EnsureType(ref content, PrimitiveDataElementName1); Type generatedType = CompileAndGetType(content); Assert.IsTrue(generatedType.IsClass); Assert.AreEqual <string>(((WCFDataContractCollection)collectionElement.ObjectExtender).CollectionType.Name, generatedType.BaseType.Name); Assert.IsTrue(generatedType.BaseType.FullName.Contains(PrimitiveDataElementName1)); CollectionDataContractAttribute collectionAttr = TypeAsserter.AssertAttribute <CollectionDataContractAttribute>(generatedType); Assert.AreEqual <string>(ElementNamespace, collectionAttr.Namespace); }
/// <summary> /// Returns the xml qualified name for the specified system type id. /// </summary> /// <remarks> /// Returns the xml qualified name for the specified system type id. /// </remarks> /// <param name="systemType">The underlying type to query and return the Xml qualified name of</param> public static XmlQualifiedName GetXmlName(System.Type systemType) { if (systemType == null) { return null; } object[] attributes = systemType.GetCustomAttributes(typeof(DataContractAttribute), true); if (attributes != null) { for (int ii = 0; ii < attributes.Length; ii++) { DataContractAttribute contract = attributes[ii] as DataContractAttribute; if (contract != null) { if (String.IsNullOrEmpty(contract.Name)) { return new XmlQualifiedName(systemType.Name, contract.Namespace); } return new XmlQualifiedName(contract.Name, contract.Namespace); } } } attributes = systemType.GetCustomAttributes(typeof(CollectionDataContractAttribute), true); if (attributes != null) { for (int ii = 0; ii < attributes.Length; ii++) { CollectionDataContractAttribute contract = attributes[ii] as CollectionDataContractAttribute; if (contract != null) { if (String.IsNullOrEmpty(contract.Name)) { return new XmlQualifiedName(systemType.Name, contract.Namespace); } return new XmlQualifiedName(contract.Name, contract.Namespace); } } } return new XmlQualifiedName(systemType.FullName); }
public static XElement CollectionDataContractToXElement <T>(T obj) where T : new() { if ((object)obj == null) { throw new ArgumentNullException("obj"); } string name = typeof(T).Name; object[] customAttributes = typeof(T).GetCustomAttributes(typeof(CollectionDataContractAttribute), true); if (customAttributes != null && customAttributes.Length > 0) { CollectionDataContractAttribute contractAttribute = (CollectionDataContractAttribute)customAttributes[0]; if (!string.IsNullOrWhiteSpace(contractAttribute.Name)) { name = contractAttribute.Name; } } XElement xelement = new XElement((XName)name); PropertyInfo property1 = obj.GetType().GetProperty("Count"); if (property1 == (PropertyInfo)null) { return(xelement); } int num = (int)property1.GetGetMethod().Invoke((object)obj, (object[])null); if (num <= 0) { return(xelement); } PropertyInfo property2 = obj.GetType().GetProperty("Item"); Type propertyType = property2.PropertyType; for (int index = 0; index < num; ++index) { object content = property2.GetGetMethod().Invoke((object)obj, new object[1] { (object)index }); if (content != null) { if (propertyType.IsValueType || propertyType == typeof(string)) { if (propertyType == typeof(DateTime)) { content = (object)((DateTime)content).ToString("yyyy-MM-ddTHH:mm:ss.ff"); } xelement.Add((object)new XElement((XName)"Item", content)); } else { object obj1 = MessageReader._dataContractToXElement.MakeGenericMethod(propertyType).Invoke((object)null, new object[1] { content }); xelement.Add((object)(XElement)obj1); } } } return(xelement); }