System.InvalidCastException: At least one element in the source array could not be cast down to the destination array type.

System.InvalidCastException: At least one element in the source array could not be cast down to the destination array type.

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


AKaplan posted on Friday, June 26, 2009

I'm using csla.net 3.5 and receiving this error "System.InvalidCastException: At least one element in the source array could not be cast down to the destination array type." when pulling a sproc using linqtosql. I've followed the sample projecttracker with csla.net. Not sure what I am doing wrong or what is causing this.


_
Protected Overloads Sub DataPortal_Fetch()
RaiseListChangedEvents = False
Using ctx = ContextManager(Of CKDBDataContext).GetManager(Database.CreativeKnightConnection)
For Each value In ctx.DataContext.esp_SelectRolesAll
Add(Role.GetRole(value))
Next
End Using
RaiseListChangedEvents = True
End Sub

Friend Shared Function GetRole(ByVal data As CK.Linq.esp_SelectRolesAllResult) As Role
Return DataPortal.FetchChild(Of Role)(data)
End Function

Private Sub Child_Fetch(ByVal data As CK.Linq.esp_SelectRolesAllResult)
LoadProperty(Of Guid)(_Id, data.RoleId)
LoadProperty(Of Guid)(_ApplicationID, data.ApplicationId)
LoadProperty(Of String)(_Name, data.RoleName)
LoadProperty(Of String)(_Description, data.Description)
LoadProperty(Of RoleCategoryList)(_RoleCategoryList, RoleCategoryList.GetRoleCategoryList(data))
End Sub


The error seems be kicking off here in the csla\Reflection\MethodCaller.vb
'''
''' Uses reflection to dynamically invoke a method,
''' throwing an exception if it is not implemented
''' on the target object.
'''
'''
''' Object containing method.
'''
'''
''' MethodHandle for the method.
'''
'''
''' Parameters to pass to method.
'''
Private Function CallMethod(ByVal obj As Object, ByVal methodHandle As DynamicMethodHandle, ByVal ParamArray parameters() As Object) As Object
Dim result As Object = Nothing
Dim method = methodHandle.DynamicMethod

Dim inParams() As Object = Nothing
If parameters Is Nothing Then
inParams = New Object() {Nothing}
Else
inParams = parameters
End If

If methodHandle.HasFinalArrayParam Then
Dim pCount = methodHandle.MethodParamsLength
' last param is a param array or only param is an array
Dim extras = inParams.Length - (pCount - 1)

' 1 or more params go in the param array
' copy extras into an array
Dim extraArray() As Object = GetExtrasArray(extras, methodHandle.FinalArrayElementType)
Array.Copy(inParams, extraArray, extras)

' copy items into new array
Dim paramList() As Object = New Object(pCount - 1) {}
For pos = 0 To pCount - 2
paramList(pos) = parameters(pos)
Next pos
paramList(paramList.Length - 1) = extraArray

' use new array
inParams = paramList
End If
Try
result = methodHandle.DynamicMethod(obj, inParams)
Catch ex As Exception
Throw New CallMethodException(methodHandle.MethodName & " " & My.Resources.MethodCallFailed, ex)
End Try
Return result
End Function

I need to understand why and how to fix it.

RockfordLhotka replied on Friday, June 26, 2009

MethodCaller attempts to apply normal method overloading rules. In other words, it takes the type of parameter you pass in, and tries to find a matching method to call.

If it can't find an exact match, it finds one with the same number of parameters, hoping that .NET can cast the parameter to that type.

My guess is that your Child_Fetch() parameter type is actually not the same as the type of value you are passing to DataPortal.FetchChild(), so this cast fails.

AKaplan replied on Friday, June 26, 2009

How is my parameter type different?

RockfordLhotka replied on Friday, June 26, 2009

I don't know - use intellisense and see if they are the same. Or switch your code to directly call the Child_Fetch() method (temporarily) to see if the types match.

Are you using the current version of the framework? There was a bug in earlier versions where some types of array wouldn't pass through MethodCaller correctly.

AKaplan replied on Friday, June 26, 2009

I am using CSLA.net 3.5.0-080330. Time to update ... again?

AKaplan replied on Friday, June 26, 2009

Ok I've updated to CSLA.net 3.5.3 and I'm still having the same issue.

AKaplan replied on Saturday, June 27, 2009

I figured out the problem. What I did was I had a Child Object within the Role Object and once I took that out. POOF it worked. I'll play with it more to find the true culprit and why I wasn't able to load child objects.

Copyright (c) Marimer LLC