-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. 有可能还是我做错了什么?

4

1 回答 1

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);
    }
于 2013-04-04T13:44:26.040 回答