如果使用datamember属性进行装饰,则动态ServiceStack'ExcludePropertyReferences'

时间:2014-10-22 14:56:40

标签: servicestack

我想在运行时忽略我的Object中的一些属性。属性用数据成员属性修饰(没有数据成员属性excludepropertyreferencces工作正常)。你能提供一些见解吗?感谢

问题:如何在运行时排除物业,如果它们是以DATAMEMBER ATTRIBUTE装饰? ServiceStack,ExcludePropertyReferences

    var model = new Model {
        FirstName = "First Name",
        LastName = "Last Name",
        Children = new List<ModelChild>{
            new ModelChild { ChildFirstName = "ChildFirstName 1", ChildLastName = "ChildLastName 1" },
            new ModelChild { ChildFirstName = "ChildFirstName 2", ChildLastName = "ChildLastName 2" }
        }
    };

    var model1 = new Model1 {
        FirstName = "First Name",
        LastName = "Last Name",
        Children = new List<Model1Child>{
            new Model1Child { ChildFirstName = "ChildFirstName 1", ChildLastName = "ChildLastName 1" },
            new Model1Child { ChildFirstName = "ChildFirstName 2", ChildLastName = "ChildLastName 2" }
        }
    };

    Console.WriteLine("Properties won't get ignored because the Model is decorated with Serialization Attributes");

    using(MemoryStream stream = new MemoryStream())
    using (var jsConfig = JsConfig.BeginScope()) {
        jsConfig.ExcludeTypeInfo = true;
        jsConfig.ExcludePropertyReferences = new [] { "Model.LastName", "ModelChild.ChildLastName" }.ToArray();
        JsonSerializer.SerializeToStream(model, model.GetType(), stream);
        LINQPad.Extensions.Dump(System.Text.Encoding.Default.GetString(stream.ToArray()));
    }

    Console.WriteLine();
    Console.WriteLine();

    Console.WriteLine("Properties will get ignored because the Model is not decorated with Serialization Attributes");
    using(MemoryStream stream = new MemoryStream())
    using (var jsConfig = JsConfig.BeginScope()) {
        jsConfig.ExcludeTypeInfo = true;
        jsConfig.ExcludePropertyReferences = new [] { "Model1.LastName", "Model1Child.ChildLastName" }.ToArray();
        JsonSerializer.SerializeToStream(model1, model1.GetType(), stream);
        LINQPad.Extensions.Dump(System.Text.Encoding.Default.GetString(stream.ToArray()));

    }




// Define other methods and classes here
[DataContract()]
public class Model {

    [DataMember(Name = "first_name",EmitDefaultValue = false )]   
    public string FirstName { get; set; }
    [DataMember(Name = "last_name")]
    public string LastName { get; set; }
    [DataMember(Name = "collections")]
    public List<ModelChild> Children { get; set; }
}

[DataContract()]
public class ModelChild {
    [DataMember(Name = "child_first_name")]
    public string ChildFirstName { get; set; }
    [DataMember(Name = "child_last_name")]
    public string ChildLastName { get; set; }
}

public class Model1 {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public List<Model1Child> Children { get; set; }
}

public class Model1Child {
    public string ChildFirstName { get; set; }
    public string ChildLastName { get; set; }
}

0 个答案:

没有答案