SmartInt and other Smart data types

SmartInt and other Smart data types

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


Nemisis posted on Tuesday, March 24, 2009

Hi all, i have created a SmartInt datatype, which is nearly the same as the SmartDate found in the csla library, but i am getting an error in the following routine

public static object CoerceValue(Type desiredType, Type valueType, object oldValue, object value)

The error is

Invalid cast from 'System.Int32' to 'SamplePropertyApplication.SmartInt'.

Am i right in thinking that for any new datatype like SmartInt i would have to manually add it to

public static object CoerceValue(Type desiredType, Type valueType, object oldValue, object value)

I am using the Managed backing fields option and not private backing fields, as recommended. I would like to create other Smart datatypes as our application requires them, but wondered if there was something i have done wrong?

Nemisis replied on Tuesday, March 24, 2009

encase a stack trace is needed, here it is


[InvalidCastException: Invalid cast from 'System.Int32' to 'SamplePropertyApplication.SmartInt'.]
System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) +7567404
System.Int32.System.IConvertible.ToType(Type type, IFormatProvider provider) +36
System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) +7599280
System.Convert.ChangeType(Object value, Type conversionType) +32
Csla.Utilities.CoerceValue(Type desiredType, Type valueType, Object oldValue, Object value) in C:\BslStore\Doc\My Software\cslacs\Csla\Utilities.cs:199
Csla.Utilities.CoerceValue(Type valueType, Object oldValue, Object value) in C:\BslStore\Doc\My Software\cslacs\Csla\Utilities.cs:238
Csla.Core.BusinessBase.LoadPropertyConvert(PropertyInfo`1 propertyInfo, F newValue) in C:\BslStore\Doc\My Software\cslacs\Csla\Core\BusinessBase.cs:2436

[PropertyLoadException: Property load or set failed for property Number (Invalid cast from 'System.Int32' to 'SamplePropertyApplication.SmartInt'.)]
Csla.Core.BusinessBase.LoadPropertyConvert(PropertyInfo`1 propertyInfo, F newValue) in C:\BslStore\Doc\My Software\cslacs\Csla\Core\BusinessBase.cs:2440
SamplePropertyApplication.Company.DataPortal_Fetch(SingleCriteria`2 criteria) in C:\BslStore\Doc\Visual Studio 2008\Websites\SamplePropertyApplication\SamplePropertyApplication\App_Code\Company.vb:119
dm(Object , Object[] ) +74
Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\BslStore\Doc\My Software\cslacs\Csla\Reflection\MethodCaller.cs:298

[CallMethodException: DataPortal_Fetch method call failed]
Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\BslStore\Doc\My Software\cslacs\Csla\Reflection\MethodCaller.cs:302
Csla.Reflection.MethodCaller.CallMethod(Object obj, String method, Object[] parameters) in C:\BslStore\Doc\My Software\cslacs\Csla\Reflection\MethodCaller.cs:226
Csla.Reflection.LateBoundObject.CallMethod(String method, Object[] parameters) in C:\BslStore\Doc\My Software\cslacs\Csla\Reflection\LateBoundObject.cs:86
Csla.Server.SimpleDataPortal.Fetch(Type objectType, Object criteria, DataPortalContext context) in C:\BslStore\Doc\My Software\cslacs\Csla\Server\SimpleDataPortal.cs:125

[DataPortalException: DataPortal.Fetch failed (Property load or set failed for property Number (Invalid cast from 'System.Int32' to 'SamplePropertyApplication.SmartInt'.))]
Csla.DataPortal.Fetch(Type objectType, Object criteria) in C:\BslStore\Doc\My Software\cslacs\Csla\DataPortal.cs:262
Csla.DataPortal.Fetch(Object criteria) in C:\BslStore\Doc\My Software\cslacs\Csla\DataPortal.cs:173
SamplePropertyApplication.Company.GetEditableRoot(Int32 id) in C:\BslStore\Doc\Visual Studio 2008\Websites\SamplePropertyApplication\SamplePropertyApplication\App_Code\Company.vb:84

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) +0
System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) +71
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) +261
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +29
System.Web.UI.WebControls.ObjectDataSourceView.InvokeMethod(ObjectDataSourceMethod method, Boolean disposeInstance, Object& instance) +488
System.Web.UI.WebControls.ObjectDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +1247
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +19
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
System.Web.UI.WebControls.DetailsView.DataBind() +4
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
System.Web.UI.WebControls.DetailsView.EnsureDataBound() +184
System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +72
System.Web.UI.Control.EnsureChildControls() +87
System.Web.UI.Control.PreRenderRecursiveInternal() +44
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842

Nemisis replied on Tuesday, March 24, 2009

Never mind, a TypeConvertor needs to be created! Cheers anyway

Copyright (c) Marimer LLC