我正在尝试创建一个对象的实例,该对象也包含其中的类:
[DataContract(Name = "share", Namespace = "")]
public class RequestShareDocument
{
[DataMember(Order = 0)]
public string profile_name { get; set; }
[DataMember(Order = 1)]
public List<RequestSigner> signers {get; set;}
[DataMember(Order = 2)]
public bool createcopy { get; set; }
}
[DataContract(Name = "signer", Namespace = "")]
public class RequestSigner
{
[DataMember(Order = 0)]
public string email { get; set; }
[DataMember(Order = 1)]
public string order { get; set; }
[DataMember(Order = 2)]
public string reminder { get; set; }
[DataMember(Order = 3)]
public RequestSignerPermissions permissions { get; set; }
[DataMember(Order = 4)]
public string signature_field { get; set; }
}
[DataContract(Name = "permissions", Namespace = "")]
public class RequestSignerPermissions
{
[DataMember(Order = 0)]
public bool save_locally {get;set;}
[DataMember(Order = 1)]
public string open_password { get; set; }
[DataMember(Order = 2)]
public DateTime accessible_after { get; set; }
[DataMember(Order = 3)]
public DateTime accessible_before { get; set; }
[DataMember(Order = 4)]
public string certify_level { get; set; }
}
我需要创建一个类的实例并序列化为 XML,但我不知道该怎么做?
我能够创建一个类的实例,但是当涉及到List<T>
属性时,我无法创建它。我已经尝试过这个:
if (typeof(IEnumerable).IsAssignableFrom(item.PropertyType))
IList list = (IList)Activator.CreateInstance(typeof(List<>));
...并序列化我正在使用:
XmlSerializer mySerializer = new XmlSerializer(typeof(T), new
XmlRootAttribute(RootElementName));
StringWriter sww = new StringWriter();
XmlWriter writer = XmlWriter.Create(sww, new XmlWriterSettings()
{ OmitXmlDeclaration = true, Indent = true });
mySerializer.Serialize(writer, item);
writer.Close();
sww.Close();
问题是当我循环所有属性并到达List
我不知道如何创建List
. 有可能还是我做错了什么?