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 7445583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:52:13+00:00 2026-05-29T11:52:13+00:00

I wrote a custom serializer that works by setting object properties by reflection. Serializable

  • 0

I wrote a custom serializer that works by setting object properties by reflection. Serializable classes are tagged with serializable attribute and all serializable properties are also tagged. For example, the following class is serializable:

[Serializable]
public class Foo
{
   [SerializableProperty]
   public string SomethingSerializable {get; set;}

   public string SometthingNotSerializable {get; set;}
}

When the serializer is asked to deserialize SomethingSerializable, it gets the set method of the property and uses it to set it by doing something like this:

PropertyInfo propertyInfo; //the property info of the property to set
//...//
if (propertyInfo.CanWrite && propertyInfo.GetSetMethod() != null)
{
   propertyInfo.GetSetMethod().Invoke(obj, new object[]{val});
}

This works fine, however, how can I make the property setter accessible only to the serializer? If the setter is private:

public string SomethingSerializable {get; private set;}

then the call to propertyInfo.GetSetMethod() returns null in the serializer. Is there any way to access the private setter or any other way to ensure that only the serializer can access the setter? The serializer is not guaranteed to be in the same assembly.

  • 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-29T11:52:15+00:00Added an answer on May 29, 2026 at 11:52 am

    As you already figured out, one way to access a non-public setter is as follows:

    PropertyInfo property = typeof(Type).GetProperty("Property");
    property.DeclaringType.GetProperty("Property");
    property.GetSetMethod(true).Invoke(obj, new object[] { value });
    

    There is another way, though:

    PropertyInfo property = typeof(Type).GetProperty("Property");
    // if Property is defined by a base class of Type, SetValue throws
    property = property.DeclaringType.GetProperty("Property");
    property.SetValue(obj, value, BindingFlags.NonPublic | BindingFlags.Instance, null, null, null);  // If the setter might be public, add the BindingFlags.Public flag.
    

    Coming here from a search engine?

    This question is specifically about accessing a non-public setter in a public property.

    • If both the property and setter are public, only the first example will work for you. To get the second example to work, you will need to add the BindingFlags.Public flag.
    • If the property is declared in a parent type and not visible to the type on which you’re calling GetProperty, you won’t be able to access it. You’ll need to call GetProperty on a type to which the property is visible. (This doesn’t affect private setters as long as the property itself is visible.)
    • If there are multiple declarations for the same property in the inheritance chain (via the new keyword), these examples will target the property that is immediately visible to the type on which GetProperty is called. For example, if class A declares Property using public int Property, and class B re-declares Property via public new int Property, typeof(B).GetProperty("Property") will return the property declared in B, while typeof(A).GetProperty("Property") will return the property declared in A.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a custom web part for SharePoint 2007 that loads an existing user
I have a client that is running a custom VB 6 application they wrote
I have read that generally abstract classes should not be made Serializable in Java.
I have rails application that connects to multiple databases. I wrote custom rake task
I wrote custom java proposals contributor. It works fine when using Java Proposals advanced
I've wrote custom model binder in project, that uses ASP.NET MVC 2. This model
I wrote a custom URL rewriting module, to take certain paths and map them
I wrote a custom WCF binding a while back to allow my load balanced
I wrote a custom ordering LINQ extension method as below but I think it
I have wrote a custom MultipleChoiceField. I have everything working ok but when I

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.