Forum Discussion

hala's avatar
hala
New Contributor
6 years ago

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?

 

No RepliesBe the first to reply