Vogen icon indicating copy to clipboard operation
Vogen copied to clipboard

Analyzer to detect and prohibit `Activator.CreateInstance(typeof(MyVo))`

Open SteveDunn opened this issue 3 years ago • 0 comments

Describe the feature

e.g. var i = (Score)Activator.CreateInstance(typeof(Score))!;

Currently, this results in a runtime error when the Value is used, as the generated code knows it wasn't created via the From method. The stack trace looks like this (in #DEBUG):

    Vogen.ValueObjectValidationException : Use of uninitialized Value Object at:    at Vogen.Tests.RecordTests.MyRecord..ctor()
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)

An analyzer would make this obvious at compile time.

SteveDunn avatar May 07 '22 06:05 SteveDunn