Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List<T> cast error #91

Closed
jddj007-hydra opened this issue Jan 11, 2022 · 3 comments
Closed

List<T> cast error #91

jddj007-hydra opened this issue Jan 11, 2022 · 3 comments

Comments

@jddj007-hydra
Copy link

when i try to user a List<KeyValuePair<string,string>> or a Dictionary<string,string> like this:
@foreach (var item in Model.Languages){ <option value="@(item.Key)">@(item.Value)</option> }
got this error:

Unable to cast object of type 'System.Collections.Generic.List1[System.Collections.Generic.KeyValuePair2[System.String,System.String]]' to type 'System.Collections.Generic.IEnumerable`1[System.Object]'.

error stackTrace is
at System.Runtime.CompilerServices.CastHelpers.ChkCastAny(Void* toTypeHnd, Object obj)
at RazorEngineCore.AnonymousTypeWrapper.TryGetMember(GetMemberBinder binder, Object& result)
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
at TemplateNamespace.Template.d__0.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at RazorEngineCore.RazorEngineCompiledTemplate.d__12.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at RazorEngineCore.RazorEngineCompiledTemplate.Run(Object model)

i upgrade to dotnet6 ,before this update ,this code runs very well.
then i change model to
Languages = list.Select(v=>(object)v).ToList();
it works!!

hope to fix this issue.

@jddj007-hydra
Copy link
Author

i changed AnonymousTypeWrapper.cs modify TryGetMember method
`

        if (isEnumerable && !(result is string))
        {
            result = result as IEnumerable;
            //((IEnumerable<object>) result)
            //        .Select(e =>
            //        {
            //            if (e.IsAnonymous())
            //            {
            //                return new AnonymousTypeWrapper(e);
            //            }

            //            return e;
            //        })
            //        .ToList();
        }

`
when i use List or Dictionary<TKey,TValue>
looks like ok, plz fix it .

@adoconnection
Copy link
Owner

thanks!

@adoconnection
Copy link
Owner

Install-Package RazorEngineCore -Version 2022.1.1
new package released

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants