Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7070463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:34:10+00:00 2026-05-28T05:34:10+00:00

I have an interface with a property like this: public interface IFoo { //

  • 0

I have an interface with a property like this:

public interface IFoo {
    // ...

    [JsonIgnore]
    string SecretProperty { get; }

    // ...
}

I want the SecretProperty to be ignored when serializing all implementing classes. But it seems I have to define the JsonIgnore attribute on every implementation of the property. Is there a way to achieve this without having to add the JsonIgnore attribute to every implementation? I didn’t find any serializer setting which helped me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:34:11+00:00Added an answer on May 28, 2026 at 5:34 am

    After a bit of searching, I found this question:

    How to inherit the attribute from interface to object when serializing it using JSON.NET

    I took the code by Jeff Sternal and added JsonIgnoreAttribute detection, so it looks like this:

    class InterfaceContractResolver : DefaultContractResolver
    {
        public InterfaceContractResolver() : this(false) { }
    
        public InterfaceContractResolver(bool shareCache) : base(shareCache) { }
    
        protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
        {
            var property = base.CreateProperty(member, memberSerialization);
            var interfaces = member.DeclaringType.GetInterfaces();
            foreach (var @interface in interfaces)
            {
                foreach (var interfaceProperty in @interface.GetProperties())
                {
                    // This is weak: among other things, an implementation 
                    // may be deliberately hiding an interface member
                    if (interfaceProperty.Name == member.Name && interfaceProperty.MemberType == member.MemberType)
                    {
                        if (interfaceProperty.GetCustomAttributes(typeof(JsonIgnoreAttribute), true).Any())
                        {
                            property.Ignored = true;
                            return property;
                        }
    
                        if (interfaceProperty.GetCustomAttributes(typeof(JsonPropertyAttribute), true).Any())
                        {
                            property.Ignored = false;
                            return property;
                        }
                    }
                }
            }
    
            return property;
        }
    }
    

    Using this InterfaceContractResolver in my JsonSerializerSettings, all properties which have a JsonIgnoreAttribute in any interface are ignored, too, even if they have a JsonPropertyAttribute (due to the order of the inner if blocks).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have an interface like this: public interface ISomeInterface { ... } I
I have an entity like this: public class Person { public string Name {
Let's assume I have an annotated bean property setter like this: public class Foo
I have a legacy COM-component with an interface declaring a property like this (IDL
I have this property, which is working fine: public IEnumerable<IGrouping<MessageType, Message>> MessageGroups { get
Does dot net have an interface like IEnumerable with a count property? I know
I have an interface for a creaky property-map: interface IPropertyMap { bool Exists(string key);
I have a property called IsSecureConnection that is part of my object's interface. This
I've implemented a class that looks like this interface: [ImmutableObject(true)] public interface ICustomEvent {
Lets say we have this interface: public interface ILog { void Add(Message message); }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.