Initializing a Dummy Shared Field

Initializing a Dummy Shared Field

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


lazaroms posted on Thursday, July 08, 2010

Hi all friends,

In the book Expert VB 2008 Business Objects, page 239, chapter 7,  there's a section called "Initializing a Dummy Shared Field". I read it and think I understood it.

I have a doubt, when invoking the protected method OnDeserialized(), the book don't say anything about calling the OnDeserialized() base method.

Is not it necessary to call the base method?


Thanks in advance,

L.M.Santin

RockfordLhotka replied on Thursday, July 08, 2010

When the book was written I think the base method was empty. I don't think that is still true, and you should call the base method.

However, you should know that the _forceInit trick discussed in that section doesn't appear to be reliable in .NET 4 and SL4. Microsoft appears to have added more optimizations to the JIT compiler, and it now optimizes that code completely out of the app (it seems that they detect that the code does nothing useful).

So the recommended solution is to make the PropertyInfo<T> fields public. CSLA 3.8 and higher has code to automatically initialize the static fields for you, but on Silverlight the fields must be public for this to work (due to SL reflection limitations).

lazaroms replied on Thursday, July 08, 2010

Thanks Rocky.

I'm using CSLA 3.6 and .NET 3.5, no SL, ¿Could I keep on using the _forceinit trick?

 

In other hand, you mean changing this line:

  Private Shared FirstNameProperty As PropertyInfo(Of String) = RegisterProperty(New PropertyInfo(Of String)("FirstName", "First name"))

for this one:

  Public Shared FirstNameProperty As PropertyInfo(Of String) = RegisterProperty(New PropertyInfo(Of String)("FirstName", "First name"))

Will it make the user gain direct access to the FirstNameProperty without using Public Property FirstName()?

 

L.M.Santin

 

 

RockfordLhotka replied on Thursday, July 08, 2010

The CSLA auto-init feature was added in 3.8. Without that feature the _forceInit trick is the only option - but that won't be reliable with Silverlight 4 if you build your code in release mode.

So your options, if you are using SL4, are to only build in debug mode and use _forceInit, or upgrade to 3.8 and make your PropertyInfo<T> fields public.

Copyright (c) Marimer LLC