System.InvalidCastException for identical types?

1
System.InvalidCastException: '[A]DefaultStockWatchPlugins.Model.EditWatchlistModel cannot be cast to [B]DefaultStockWatchPlugins.Model.EditWatchlistModel. 

Type A originates from 'DefaultStockWatchPlugins, Version=2020.9.8.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'P:\Code Projects\Codefarts.StockMarket\StockWatchWpfCore\bin\Debug\netcoreapp3.1\Plugins\DefaultStockWatchPlugins\DefaultStockWatchPlugins.dll'. 
Type B originates from 'DefaultStockWatchPlugins, Version=2020.9.8.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'P:\Code Projects\Codefarts.StockMarket\StockWatchWpfCore\bin\Debug\netcoreapp3.1\Plugins\DefaultStockWatchPlugins\DefaultStockWatchPlugins.dll'.'

Never had this exception show up like this before. I mean I have had System.InvalidCastException exceptions before but never because of 2 identical types. So has anyone had any experience with this type of exception? It's literally the same type from the same assembly at the same file location with the same version.

Unfortunately it's being thrown via wpf so there is not alot of details about what is invoking my code.

I'm not sure because wpf is throwing he exception, but I believe it's coming from here:

(wpf xaml)

<Button Margin="4,0,0,0" VerticalAlignment="Center" 
Command="{Binding ElementName=MainGrid, Path=DataContext.EditWatchlistCommand, Mode=OneWay}">
    <Button.CommandParameter>
        <MultiBinding Converter="{StaticResource WatchlistConverter}"  >
            <Binding Path="" />
            <Binding ElementName="MainGrid" Path="DataContext.SelectedDataProvider"/>
        </MultiBinding>
    </Button.CommandParameter>
    <Image Source="{StaticResource MyImageSource}" Stretch="UniformToFill" VerticalAlignment="Center" />
</Button>

The property that return the command is:

 public ICommand EditWatchlistCommand
        {
            get
            {
                return this.diProvider.Resolve<EditWatchlistCommand>();
            }
        }

and the code behind for the command:

    private ApplicationModel appModel;
    private IDependencyInjectionProvider diProvider;

    public EditWatchlistCommand(ApplicationModel appModel, IDependencyInjectionProvider diProvider)
    {
        this.appModel = appModel ?? throw new ArgumentNullException(nameof(appModel));
        this.diProvider = diProvider ?? throw new ArgumentNullException(nameof(diProvider));
    }

    public override bool CanExecute(EditWatchlistModel parameter)
    {
        return parameter != null && parameter.DataProvider != null && parameter.Watchlist != null;
    }

    public override void Execute(EditWatchlistModel parameter)
    {
        // new data view
        var dataView = this.diProvider.Resolve<IDataView>();
        dataView.Title = $"Edit: {parameter.Watchlist.Name}";
        dataView.ViewName = "EditWatchlist";
        dataView.Model = parameter;

        this.appModel.Views.DataViews.Add(dataView);
    }

My converter code behind:

 public class EditWatchlistConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var model = new EditWatchlistModel();
        model.DataProvider = values.OfType<IDataSourceProvider>().FirstOrDefault(); 
        model.Watchlist = values.OfType<Watchlist>().FirstOrDefault();
        return model;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

The full exception being thrown

System.InvalidCastException
  HResult=0x80004002
  Message=[A]DefaultStockWatchPlugins.Model.EditWatchlistModel cannot be cast to [B]DefaultStockWatchPlugins.Model.EditWatchlistModel. Type A originates from 'DefaultStockWatchPlugins, Version=2020.9.8.3, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'P:\Code Projects\Codefarts.StockMarket\StockWatchWpfCore\bin\Debug\netcoreapp3.1\Plugins\DefaultStockWatchPlugins\DefaultStockWatchPlugins.dll'. Type B originates from 'DefaultStockWatchPlugins, Version=2020.9.8.3, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'P:\Code Projects\Codefarts.StockMarket\StockWatchWpfCore\bin\Debug\netcoreapp3.1\Plugins\DefaultStockWatchPlugins\DefaultStockWatchPlugins.dll'.
  Source=Codefarts.WPFCommonCore31
  StackTrace:
   at Codefarts.WPFCommon.Commands.GenericDelegateCommand`1.CanExecute(Object parameter) in P:\Code Projects\Codefarts.WPFCommon\Codefarts.WPFCommon\Commands\GenericDelegateCommand.cs:line 178
   at MS.Internal.Commands.CommandHelpers.CanExecuteCommandSource(ICommandSource commandSource)
   at System.Windows.Controls.Primitives.ButtonBase.UpdateCanExecute()
   at System.Windows.Controls.Primitives.ButtonBase.OnCanExecuteChanged(Object sender, EventArgs e)
   at System.Windows.WeakEventManager.ListenerList.DeliverEvent(Listener& listener, Object sender, EventArgs args, Type managerType)
   at System.Windows.WeakEventManager.ListenerList.DeliverEvent(Object sender, EventArgs args, Type managerType)
   at System.Windows.WeakEventManager.DeliverEvent(Object sender, EventArgs args)
   at System.Windows.Input.CommandManager.RequerySuggestedEventManager.OnRequerySuggested(Object sender, EventArgs args)
   at System.Windows.Input.CommandManager.RaiseRequerySuggested(Object obj)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object obj)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
c#
wpf
xaml
exception
casting
asked on Stack Overflow Sep 14, 2020 by Dean Lunz • edited Sep 21, 2020 by StayOnTarget

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0