使用反射设置复合类的值



我试图创建一个对象的实例,也有类组成到它:

[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的实例。这是可能的还是我做错了什么?

如果我理解正确的话,这段代码就是您想要的:

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);
    }

相关内容

  • 没有找到相关文章