在 VS 2015 和 VS 2013 上运行同一段代码时出现问题

Posted

技术标签:

【中文标题】在 VS 2015 和 VS 2013 上运行同一段代码时出现问题【英文标题】:Problems running same piece of code on VS 2015 and VS 2013 【发布时间】:2016-08-25 14:53:10 【问题描述】:

在 Visual Studio 2013 和 2015 中运行一段代码时,我得到了两种不同的结果。在 Visual Studio 2015 上,我得到一个 NullReference,而在 2013 年,它按应有的方式工作。此外,Visual Studio 2015 在 Windows 8.1 上的 Windows 10 和 2013 上运行。这段代码是:

private static T FindParentOfType<T>(DependencyObject o)

    dynamic parent = VisualTreeHelper.GetParent(o);
    return parent.GetType().IsAssignableFrom(typeof(T)) ? parent : FindParentOfType<T>(parent);

代码被调用:

Grid RiskGrid = FindParentOfType<Grid>(ChampViewModelSel);

当它检查 IsAssginableFrom 时,错误是 Nullreference,因为在 VS2015 中它找到的是 Canvas 而不是在 VS2013 中找到的希望的 Grid?

堆栈跟踪

  StackTrace  "   at Microsoft.CSharp.RuntimeBinder.SymbolTable.GetOriginalTypeParameterType(Type t)\r\n   
    at Microsoft.CSharp.RuntimeBinder.SymbolTable.AreTypeParametersEquivalent(Type t1, Type t2)\r\n   
    at Microsoft.CSharp.RuntimeBinder.SymbolTable.LoadMethodTypeParameter(MethodSymbol parent, Type t)\r\n   
    at Microsoft.CSharp.RuntimeBinder.SymbolTable.LoadSymbolsFromType(Type originalType)\r\n   
    at Microsoft.CSharp.RuntimeBinder.SymbolTable.AddMethodToSymbolTable(MemberInfo member, AggregateSymbol callingAggregate, MethodKindEnum kind)\r\n   
    at Microsoft.CSharp.RuntimeBinder.SymbolTable.AddNamesInInheritanceHierarchy(String name, BindingFlags flags, List 1 inheritance)\r\n   
    at Microsoft.CSharp.RuntimeBinder.SymbolTable.PopulateSymbolTableWithName(String name, IEnumerable 1 typeArguments, Type callingType)\r\n   
    at Microsoft.CSharp.RuntimeBinder.RuntimeBinder.PopulateSymbolTableWithPayloadInformation(DynamicMetaObjectBinder payload, Type callingType, ArgumentObject[] arguments)\r\n  
    at Microsoft.CSharp.RuntimeBinder.RuntimeBinder.BindCore(DynamicMetaObjectBinder payload, IEnumerable 1 parameters, DynamicMetaObject[] args, DynamicMetaObject& deferredBinding)\r\n
    at Microsoft.CSharp.RuntimeBinder.RuntimeBinder.Bind(DynamicMetaObjectBinder payload, IEnumerable 1 parameters, DynamicMetaObject[] args, DynamicMetaObject& deferredBinding)\r\n 
    at Microsoft.CSharp.RuntimeBinder.BinderHelper.Bind(DynamicMetaObjectBinder action, RuntimeBinder binder, IEnumerable 1 args, IEnumerable 1 arginfos, DynamicMetaObject onBindingError)\r\n 
    at Microsoft.CSharp.RuntimeBinder.CSharpInvokeMemberBinder.FallbackInvokeMember(DynamicMetaObject target, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion)\r\n
    at System.Dynamic.DynamicMetaObject.BindInvokeMember(InvokeMemberBinder binder, DynamicMetaObject[] args)\r\n 
    at System.Dynamic.InvokeMemberBinder.Bind(DynamicMetaObject target, DynamicMetaObject[] args)\r\n
    at System.Dynamic.DynamicMetaObjectBinder.Bind(Object[] args, ReadOnlyCollection 1 parameters, LabelTarget returnLabel)\r\n
    at System.Runtime.CompilerServices.CallSiteBinder.BindCore[T](CallSite 1 site, Object[] args)\r\n 
    at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)\r\n
    at BC_Game.ViewModel.ChampionViewModel.FindParentOfType[T](DependencyObject o)\r\n 
    at BC_Game.ViewModel.ChampionViewModel.ManStart(ManipulationStartedEventArgs e)"    string

更新

在Win10上安装了VS2013,问题依旧。 VS2013在Win8.1上运行良好

更新

来自 Visual Studio 2013 wp 8.1

C:\Program Files (x86)\MSBuild\12.0\bin\Csc.exe /noconfig /nowarn:1701,1702,2008 /nostdlib+ /errorreport:prompt /warn:4 /define:DEBUG;TRACE /errorendlocation / preferreduilang:en-US /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETPortable\v4.0\Profile\Profile158\mscorlib.dll" /reference:"C:\Program Files (x86 )\Reference Assemblies\Microsoft\Framework.NETPortable\v4.0\Profile\Profile158\Microsoft.CSharp.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETPortable\v4.0 \Profile\Profile158\mscorlib.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETPortable\v4.0\Profile\Profile158\System.Core.dll" /reference:"C :\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETPortable\v4.0\Profile\Profile158\System.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETPortable \v4.0\Profile\Profile158\System.Net.dll" /reference:"C:\Program Files (x86)\Reference Asse mblies\Microsoft\Framework.NETPortable\v4.0\Profile\Profile158\System.Runtime.Serialization.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETPortable\v4.0\ Profile\Profile158\System.ServiceModel.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETPortable\v4.0\Profile\Profile158\System.ServiceModel.Web.dll" /reference :"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETPortable\v4.0\Profile\Profile158\System.Windows.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\ Microsoft\Framework.NETPortable\v4.0\Profile\Profile158\System.Xml.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETPortable\v4.0\Profile\Profile158\ System.Xml.Linq.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETPortable\v4.0\Profile\Profile158\System.Xml.Serialization.dll" /debug+ /debug :full /filealign:512 /optimize- /out:obj\Debug\LibraryOfModels.dll /target:l ibrary /utf8output BuyMenuClass.cs CastleModel.cs ChampionModel.cs ChatDTO.cs CollectionCombiner.cs Country.cs CurrentUser.cs DecorationListItems.cs GameDTO.cs MapTransform.cs MarketplaceModel.cs MenuFirstPageModel.cs NotifyBase.cs Player.cs PopupColorPickerModel.cs PopupExtraInfoModel。 cs PopUpModel.cs Properties\AssemblyInfo.cs RankingDTO.cs RankingModel.cs ShieldGearModel.cs UnitModel.cs UserDTO.cs WarCalculations.cs "C:\Users\Jonas\AppData\Local\Temp.NETPortable,Version=v4.0,Profile =Profile158.AssemblyAttributes.cs"

从 VS 2015 Win 10

C:\Program Files (x86)\MSBuild\14.0\bin\csc.exe /noconfig /nowarn:1701,1702,2008 /nostdlib+ /errorreport:prompt /warn:4 /define:DEBUG;TRACE /errorendlocation / preferreduilang:en-US /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETPortable\v4.0\Profile\Profile158\mscorlib.dll" /reference:"C:\Program Files (x86 )\Reference Assemblies\Microsoft\Framework.NETPortable\v4.0\Profile\Profile158\Microsoft.CSharp.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETPortable\v4.0 \Profile\Profile158\mscorlib.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETPortable\v4.0\Profile\Profile158\System.Core.dll" /reference:"C :\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETPortable\v4.0\Profile\Profile158\System.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETPortable \v4.0\Profile\Profile158\System.Net.dll" /reference:"C:\Program Files (x86)\Reference Asse mblies\Microsoft\Framework.NETPortable\v4.0\Profile\Profile158\System.Runtime.Serialization.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETPortable\v4.0\ Profile\Profile158\System.ServiceModel.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETPortable\v4.0\Profile\Profile158\System.ServiceModel.Web.dll" /reference :"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETPortable\v4.0\Profile\Profile158\System.Windows.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\ Microsoft\Framework.NETPortable\v4.0\Profile\Profile158\System.Xml.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETPortable\v4.0\Profile\Profile158\ System.Xml.Linq.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETPortable\v4.0\Profile\Profile158\System.Xml.Serialization.dll" /debug+ /debug :full /filealign:512 /optimize- /out:obj\Debug\LibraryOfModels.dll /ruleset: "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Static Analysis Tools\Rule Sets\MinimumRecommendedRules.ruleset" /target:library /utf8output BuyMenuClass.cs CastleModel.cs ChampionModel.cs ChatDTO.cs CollectionCombiner.cs Country.cs CurrentUser.cs DecorationListItems.cs GameDTO.cs MapTransform.cs MarketplaceModel.cs MenuFirstPageModel.cs NotifyBase.cs Player.cs PopupColorPickerModel.cs PopupExtraInfoModel.cs PopUpModel.cs Properties\AssemblyInfo.cs RankingDTO.cs RankingModel.cs ShieldGearModel.cs UnitModel.cs UserDTO.cs WarCalculations.cs "C:\Users\johann\AppData\Local\Temp.NETPortable,Version=v4.0,Profile=Profile158.AssemblyAttributes.cs"

我们可以看到两者之间的唯一区别是所使用的 .NetFrameworks。一个版本为 4.5.51650,另一个使用 4.6.01038。

更新

看来最新的电脑版 win 10 更新已经解决了这个问题。

【问题讨论】:

Windows 10 和 Visual Studio 2015 使用 .NET Framework 4.6.x,如果您尚未升级,它可能与 Windows 8.1 上的 Visual Studio 2013 附带的 .NET 4.5.1 不同。将在 VS2013 中编译的二进制文件复制到该 Windows 10 机器并运行它。如果它给你 NRE,那么它很可能是由 .NET 4.6.x 引起的。 @LexLi 您能否详细说明如何执行此操作以及在用于 silverlight 电话应用程序时在哪里可以找到它们。 是面向对象编程的On Error Resume Next。使用调试器,告诉我们你看到了什么。并且发布一个每个人都可以运行的真实repro sn-p。 有时 .NET 运行时会更改版本之间的行为,有时这种更改完全没有记录。 Here's an answer 指出一个完全不同的问题与这个问题无关——这只是一个例子。 @LexLi 的建议不错。 @JTIM 我会尝试安装 4.5.2 我相信它可以解决您的问题但是,我不知道对您的应用程序的影响。另外,我不知道它是否会影响部署。因为我不确定 Windows Phone OS 10 上是否安装了 4.5.2 框架。 【参考方案1】:

您可以尝试通过此链接更改您的 .net 版本。

https://msdn.microsoft.com/en-us/library/gg597391(v=vs.110).aspx

我相信这可能是 .net 版本的问题,但是我不确定它会对您的应用产生什么影响。

【讨论】:

【参考方案2】:

我只是猜测,因为您还没有发布错误消息本身。 通常 VS2013 和 VS2015 之间应该没有区别,除了注释中提到的默认 .NET 运行时版本。

但看看代码,静态部分可能是问题

VisualTreeHelper.GetParent(o);

在对象初始化和使用之前,有时需要遵循一些顺序。

查看调用方法的位置也会很有帮助。

但是我通过搜索 VisualTreeHelper.GetParent null 在 google 中找到了这个

VisualTreeHelper.GetParent returns null

【讨论】:

以上是关于在 VS 2015 和 VS 2013 上运行同一段代码时出现问题的主要内容,如果未能解决你的问题,请参考以下文章

visual stdio2015里解决方案管理器窗口隐藏后怎么恢复

vs2013怎么升级成vs2015

win10下驱动开发 怎么安装vs2015

VS2015 的 DLL TeamFoundation 错误

卸载VS2013 2015

LNK2001 在 VS 2013 (MSVC 18) 上,但不在 VS 2015 (MSVC 19) 上