Try our conversational search powered by Generative AI!

A better ContentTypeModel validation


Hi there,

We just encountered a curious incident in our production environment.

A page property was not reflected to our page class that inherited from PageData, the reason was that it was missing the virtual modifier.

Normally we would get a YSOD like this:

The property 'Hide' on the content type 'SettingsBlock' is autogenerated but not virtual declared.

However, our page class inherits the property from an interface and is thereby always virtual.

Looking at EPiServer.DataAbstraction.RuntimeModel.ContentDataAttributeScanningAssigner.AssignValuesToPropertyDefinition (our version is 9.6.0) the logic is to verify that the property is virtual and public.

A fix could be to also test if the property is not final.

I created the following code to show the test case:

void Main()
  typeof(B).GetProperty("P1").GetGetMethod().IsVirtual.Dump(); // true
  typeof(B).GetProperty("P1").GetGetMethod().IsFinal.Dump();   // false
  typeof(B).GetProperty("P2").GetGetMethod().IsVirtual.Dump(); // true
  typeof(B).GetProperty("P2").GetGetMethod().IsFinal.Dump();   // true

class B : A
  public virtual bool P1 { get; set; }
  public bool P2 { get; set; }

interface A
  bool P1 { get; set; }
  bool P2 { get; set; }
Feb 24, 2016 10:31

Taken from MSDN IsFinal:

To establish with certainty whether a method is overridable, use code such as this:

if (MethodInfo.IsVirtual && !MethodInfo.IsFinal)

Feb 24, 2016 10:36
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.