Ask a Question

CodeGen & Default Constructor

hala
New Contributor

CodeGen & Default Constructor

What is the reason the default constructor is set as protected? After all, the code generator is creating a constructor with all default arguments. The problem is that by setting default values you can't dynamically create instances without providing arguments. So something like this doesn't work

 

System.Type[] types = DLL.GetExportedTypes();
var theType = DLL.GetType("[MyClass]");
var c = System.Activator.CreateInstance(theType);

 

This blog post mentions exactly the same issue: https://nicolas.guelpa.me/blog/2015/03/29/c-sharp-constructor.html

Is there a workaround short of modifying the generated code (not good as I would have to redo those changes each time I regenerate the code) or deriving my own class (doesn't work in my case) which simply adds a default constructor?

 

0 REPLIES 0
cancel
Showing results for 
Search instead for 
Did you mean: