-1
我想要的對象,也有它由類來創建實例複合類的設定值:使用反射
[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
的實例。是否有可能或者我做錯了什麼?
你試試這個: '如果(typeof運算(IList的).IsAssignableFrom(item.PropertyType)){ IList的 名單= (IList)Activator.CreateInstance(item.PropertyType);' –
我已經厭倦了,它已經創建了列表的一個實例,但問題是我如何添加創建列表的類的對象。 IList list =(IList)Activator.CreateInstance(item.PropertyType);現在我得到清單對象數爲0 – Salman