locked
Enforce parameterless constructor only RRS feed

  • Question

  • I'm trying to implement a pattern that works with classes having parameterless constructors only.
    Is there a way to force this be it via interfaces, abstract or concrete base classes or other means?

    Sunday, November 15, 2020 9:03 PM

Answers

  • You cannot enforce this at compile time for anything. You can enforce the need for a default constructor with generic constraints but that is the extent of the behavior. To enforce this for anything else you'll have to use reflection which requires you use runtime code. However I'm not sure why it would matter. Who cares whether a type has constructors that aren't defaults?

    Note that if you're building a code analysis tool then you can also enforce it via the IDE using a custom code analysis rule. However this would require that you first write the rule and then the user of your rule set the rule severity to error. Fortunately the IDE supports adding new analyzer via NuGet packages so you could include it with any NuGet package you're shipping this rule for. But again, it seems odd to need it.


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, November 16, 2020 2:47 PM

All replies

  • Hello,

    For better responses please post what you have tried so far.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    My GitHub code samples
    GitHub page

    Sunday, November 15, 2020 9:09 PM
  • If your code calls the parameterless constructor, then it will fail if the class does not have a parameterless constructor.  It won't matter if the class has other constructors, right?  Why would you care?

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Monday, November 16, 2020 6:41 AM
  • You cannot enforce this at compile time for anything. You can enforce the need for a default constructor with generic constraints but that is the extent of the behavior. To enforce this for anything else you'll have to use reflection which requires you use runtime code. However I'm not sure why it would matter. Who cares whether a type has constructors that aren't defaults?

    Note that if you're building a code analysis tool then you can also enforce it via the IDE using a custom code analysis rule. However this would require that you first write the rule and then the user of your rule set the rule severity to error. Fortunately the IDE supports adding new analyzer via NuGet packages so you could include it with any NuGet package you're shipping this rule for. But again, it seems odd to need it.


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, November 16, 2020 2:47 PM
  • Thank you. The use case is a kind of factory pattern. If it worked somehow (without reflection) I would have used it in conjunction with `new()` generic constraint in order to create members. But this would not deal with parametered constructors.
    I know I can use reflection to invoke any constructors, passing the parameters but I thought there would be a simple clean solution.
    Tuesday, November 17, 2020 12:23 PM
  • There are already libraries out there that allow creation of arbitrary types that I would recommend you use instead of writing your own. This is the basis of IoC which would solve your problem and allow any constructor. They do rely on reflection but they use optimizations such that performance isn't too bad.



    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, November 17, 2020 2:26 PM
  • If your code calls the parameterless constructor, then it will fail if the class does not have a parameterless constructor.  It won't matter if the class has other constructors, right?  Why would you care?

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Right! It's just this logic that I needed but for parametered constructors instead of parameterless.
    Friday, November 20, 2020 10:16 AM