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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:36:36+00:00 2026-06-04T19:36:36+00:00

I’m looking for a way to create a class that has a default public

  • 0

I’m looking for a way to create a class that has a default public constructor, but that prohibits it’s use outside of reflection.

In more detail, I’m using an object persistence framework (AWS DynamoDB – I don’t have the option to change) that requires objects implement such a public default constructor. The framework also requires all persisted fields and properties to also have public get and set accessors and uses them when creating and initializing a new object instance. this leads to problems because the accessors need to serve dual purposes – they need to be “bare” when used by the framework, but enforce restrictions and validation when used by code. In any case, I’ve got that part worked out, but it relies on never allowing code to construct a persisted object using the default constructor (the one the persistence framework uses).

Back to the original question – to make this all work, I need to prohibit code from using the default constructor while still making it available for use by the persistence framework (which searches only for an empty public constructor). I started off by using a static flag and setting it before and after any potential object persistence uses and throwing an exception in the default constructor if it wasn’t set, but this started to cause problems in a multi-threaded environment (a web server). One thread would set it for use by the persistence framework which would open up the constructors for use by other threads. I also tried a ThreadStatic flag, but that had the opposite problem of preventing construction by the framework since it sometimes spawns worker threads.

Now I’m wondering if there is actually a way of doing this. At this point it’s become more of a curiosity than an requirement; I can always just write a comment along the lines of “DON’T USE THIS CONSTRUCTOR”, but enforcing the rule directly would make me feel better. I’ve used PostSharp before and perhaps there is a way to use an aspect for compile-time validation to prevent code-based default constructor use? Any other suggestions?

  • 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-06-04T19:36:38+00:00Added an answer on June 4, 2026 at 7:36 pm

    Using PostSharp, you could use an architectural constraint to validate that some constructor is not called from user code (do a ReferentialConstraint and attach the constraint to the class, possibly using multicasting and inheritance). Basically, if the only legitimate use of this code if from reflection, you could emit an error if you find any reference to the constructor, expect chaining from other constructors.

    Note that this would not prevent the constructor to be called from the new T() construct, where T is a generic type parameter. So only forbid the new operators: from your ReferentialAspect, use ReflectionSearch.GetMethodsUsingDeclaration and filter for (Instructions & MethodUsageInstructions.NewObject) != 0.

    EDIT: added details about ReflectionSearch.GetMethodsUsingDeclaration and MethodUsageInstructions.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm trying to create an if statement in PHP that prevents a single post
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) 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.