2013-04-03 105 views
-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的實例。是否有可能或者我做錯了什麼?

+0

你試試這個: '如果(typeof運算(IList的).IsAssignableFrom(item.PropertyType)){ IList的 名單= (IList)Activator.CreateInstance(item.PropertyType);' –

+0

我已經厭倦了,它已經創建了列表的一個實例,但問題是我如何添加創建列表的類的對象。 IList list =(IList)Activator.CreateInstance(item.PropertyType);現在我得到清單對象數爲0 – Salman

回答

1

如果我理解正確的,這個代碼你想要做什麼:

public static object InitializeTarget(Type type) 
    { 
     object target = CreateObject(type); 
     foreach (PropertyInfo propertyInfo in type.GetProperties()) 
     { 
      if (propertyInfo.PropertyType.IsClass && propertyInfo.CanWrite 
       && propertyInfo.PropertyType != typeof(string) 
       && (propertyInfo.IsDefined(typeof(DataMemberAttribute), true))) 
       propertyInfo.SetValue(target, InitializeTarget(propertyInfo.PropertyType), null); 
     } 
     return target; 
    } 

    public static object CreateObject(Type type) 
    { 
     if (typeof(IList).IsAssignableFrom(type) && type.IsGenericType) 
     { 
      Type[] genericArguments = type.GetGenericArguments(); 
      if (genericArguments.Length == 1) 
      { 
       var list = (IList)Activator.CreateInstance(type); 
       //Adding default value 
       list.Add(CreateObject(genericArguments[0])); 
       return list; 
      } 
     } 
     //If the object is only to serialize, you can use it 
     return FormatterServices.GetUninitializedObject(type); 
     //or 
     //   return Activator.CreateInstance(type); 
    } 
+0

Thanku它解決了我的問題:)非常感謝 – Salman