RegisterProperty - TypeInitializationException

RegisterProperty - TypeInitializationException

Old forum URL: forums.lhotka.net/forums/t/8667.aspx


matt tag posted on Tuesday, March 16, 2010

I am occasionally (not consistently) getting the following exception:

System.TypeInitializationException was unhandled
  Message="The type initializer for 'DV.Lib.Query' threw an exception."
  Source="DV.Lib"
  TypeName="DV.Lib.Query"
  StackTrace:
       at DV.Lib.Query.NewObject()
    (lots of callstack stuff missing)


  InnerException: System.InvalidOperationException
       Message="Can not register property Params after containing type (ListCore) has been instantiated"
       Source="Csla"
       StackTrace:
            at Csla.Core.FieldManager.PropertyInfoManager.RegisterProperty[T](Type objectType, PropertyInfo'1 info)
            at Csla.BusinessBase'1.RegisterProperty[P](PropertyInfo'1 info)
            at DV.Lib.Query..cctor() in C:\Develop\dotNet\DV\DV.Lib\Qry\Query.vb:line 104
       InnerException:

 

Some background - "DV.Lib.ListCore" inherits off of BusinessBase.  "DV.Lib.Query" inherits from "ListCore", and "Params" is a child collection property off of "Query".  Whenever I get this error, it is during the creation of one of the subclasses of ListCore - almost like "RegisterProperty" doesn't like things where there's an extra subclass layer between my class and BusinessBase.

And, just to really throw things off, this happens only occasionally.  I could stop the program and retrace the steps exactly and not run into the same exception.

Params is declared off of "DV.Lib.Query" as follows:

Private Shared ParamsProperty As PropertyInfo(Of QueryParameters) = RegisterProperty(New PropertyInfo(Of QueryParameters)("Params"))
    Public ReadOnly Property Params() As QueryParameters
        Get
            If Not FieldManager.FieldExists(ParamsProperty) Then
                LoadProperty(Of QueryParameters)(ParamsProperty, QueryParameters.NewObject)
            End If
            Return GetProperty(Of QueryParameters)(ParamsProperty)
        End Get
    End Property

 

 

marthac replied on Wednesday, September 29, 2010

Did you ever figure this out?

I'm having the same problem. :(

RockfordLhotka replied on Wednesday, September 29, 2010

I strongly recommend using the RegisterProperty() overload that accepts a lambda expression for the property, so CSLA can just do the right thing for you.

The RegisterProperty() call you show is invalid - the first parameter needs to be the type of the declaring class. And that is a source of numerous copy-paste bugs, which is why the lambda is better - with the lambda CSLA can figure out the containing type for you.

Copyright (c) Marimer LLC