How to Exclude Specific class list

Jul 12, 2012 at 11:51 AM
Edited Jul 12, 2012 at 11:52 AM

I can‘t edit Microsoft's dll,

and ca't  added Attribute [Ignore] to that class,

but I do not want to serialize it's some class

Jul 13, 2012 at 4:05 AM
Edited Jul 13, 2012 at 4:05 AM

I Find

add class:

 

    public class ExcludePropertiesContractResolver : DefaultContractResolver
    {
        IEnumerable<string> lstExclude;

        public ExcludePropertiesContractResolver(IEnumerable<string> excludedProperties)
        {
            lstExclude = excludedProperties;
        }

        protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
        {
            return base.CreateProperties(type, memberSerialization)
                .ToList()
                .FindAll(p => !lstExclude.Contains(p.PropertyName));
        }
    }


use:
            var json = JsonConvert.SerializeObject(TfsClient.QueryAllBuildDefinitions(), Formatting.Indented, new JsonSerializerSettings
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                ContractResolver = new ExcludePropertiesContractResolver(new List<string> {
                    "TeamFoundationServer", "ConfigurationServer", "TeamProjectCollection", "RetentionPolicyList","BuildController","Schedules" }),
                Error = delegate(object sender2, Newtonsoft.Json.Serialization.ErrorEventArgs e2)
                {
                    e2.ErrorContext.Handled = true;
                },

            });