Specified cast is not valid error on LoadProperty of child classSpecified cast is not valid error on LoadProperty of child class
Old forum URL: forums.lhotka.net/forums/t/6516.aspx
gidzone posted on Thursday, February 26, 2009
Hello,
I am receiving a "Specified cast is not valid" error from the FieldManager when I try and call a LoadProperty statement in a child class. All the types seem to be fine and the data returned from the db seems to be correct. Please see my class setup below.
Any help would be appreciated.
thanks!
-sanjay
My class set up is as follows:
Class: ParentBase:Businessbase<ParentBase>
Class: Child
public abstract class ParentBase: Businessbase<ParentBase>
{
protected virtual void DataPortal_Fetch(BaseCriteria criteria)
{
//fetching code
}
}
public class Child:ParentBase
{
protected override void DataPortal_Fetch(BaseCriteria criteria)
{
base.DataPortal_Fetch(criteria);
//Fetching Code
LoadProperty(SomeProperty, data.SomeProperty); <--- Line throws "Specific Cast Is Invalid"
}
}
JoeFallon1 replied on Thursday, February 26, 2009
You do not say what version of CSLA you are on.
The latest version 3.6.2 solves the problem of static field initialization for you. Older versions require you to use the _forceInit trick in every class and sublcass of your hierarchy.
Since you probably did not implement the trick, your static Property declarations got loaded out of sequence and the indexes are messed up and it is actually trying to load something else into the wrong property. Thus the invalid cast error.
Joe
gidzone replied on Thursday, February 26, 2009
Thanks Joe.
I am on 3.6.0. I will upgrade and check it out.
thanks!
-sanjay
gidzone replied on Thursday, February 26, 2009
I upgraded and I now get a Cannot Register Property Error on all child classes. I went back and read the changelog for 3.6.1/3.6.2 to see if it explained how to register properties on base classes, but I did not see anything.
Any help would be appreciated.
Thanks!
-sanjay
gidzone replied on Thursday, February 26, 2009
I think I found the solution here:
http://forums.lhotka.net/forums/thread/29224.aspx
thanks!
-sanjay
JoeFallon1 replied on Friday, February 27, 2009
That is the _forceInit trick I was referring to.
You should not need it for 3.6.2!
Although I have not tested it myself yet.
Joe
gidzone replied on Sunday, March 01, 2009
It turns out my issue after upgrading to 3.6.2 was not specifying the child type in my RegisterProperty calls. It was trying to register the properties in the base class after the base class had initialized thus the error.
Joe, thanks for all of your help.
-sanjay
Copyright (c) Marimer LLC