I am writing a parser which would determine all the properties for a web method. This particular static metnod would return us an array of xelement.
My function is
private static XElement[] GetProperty(Assembly assembly, string typeName)
{
var result = new List<XElement>();
if (typeName.EndsWith("[]"))
typeName = typeName.Replace("[]", "");
var type = assembly.GetExportedTypes().FirstOrDefault(t => t.Name == typeName);
foreach (var prop in type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.SetProperty))
{
if (prop.PropertyType.IsPrimitive())
result.Add(new XElement(prop.Name, prop.PropertyType.Name.Trim('&')));
else
{
if (prop.PropertyType.Name.EndsWith("[]"))
{
if (assembly.GetExportedTypes().FirstOrDefault(t => t.Name == prop.PropertyType.Name.TrimEnd('[', ']')) != null)
{
XElement element = new XElement(string.Format("{0}", prop.Name));
element.Add(new XElement(string.Format("{0}", "a"), GetProperty(assembly, prop.PropertyType.Name)));
result.Add(element);
}
}
else
{
....
}
}
}
return result.ToArray();
}
while parsing for an array variable i am stuck. I am not sure how to get the value "PhoneNumber" and replace to the hard coded value "a"(element.Add(new XElement(string.Format("{0}", "a"), )))) which is described in xml "". This has to be done through reflection.
-<xsd:complexType name="GeneralType">
-<xsd:sequence>
-<xsd:element name="PhoneNumbers">
-<xsd:annotation>
<xsd:documentation>Contains the claimants phone number(s).</xsd:documentation>
</xsd:annotation>
-<xsd:complexType>
-<xsd:sequence>
-<xsd:element type="person:TelephoneStructure" name="PhoneNumber" minOccurs="0" maxOccurs="unbounded">
-<xsd:annotation>
<xsd:documentation>An individual number using TelephoneStructure where [TelUse specified the use of this particular number, work or home, TelMobile indicates whether this number is a mobile yes or no, TelPreferred in the case of the claimant having multiple telephone numbers, is this number preferred? yes or no.</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
-<xsd:element type="LocalAuthorityIdType" name="ClaimReferenceId" minOccurs="0">
-<xsd:annotation>
<xsd:documentation>The back office Claim Reference Id.</xsd:documentation>
</xsd:annotation>
</xsd:element>
-<xsd:element type="LocalAuthorityIdType" name="CouncilTenantRentAccount" minOccurs="0">
-<xsd:annotation>
<xsd:documentation>The back office Council Tenant Rent Account Id.</xsd:documentation>
</xsd:annotation>
</xsd:element>
-<xsd:element type="LocalAuthorityIdType" name="CouncilTaxAccount" minOccurs="0">
-<xsd:annotation>
Can some one please help me on this. I know i can do serialization..
Aucun commentaire:
Enregistrer un commentaire