在VS 2015和VS 2013上运行相同代码的问题

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

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); 

错误是一个Nullreference当它检查IsAssginableFrom因为在VS2015中,它find了一个Canvas而不是在VS2013中find的希望的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:提示符/警告: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 \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ Microsoft.CSharp.dll“/ reference:”C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \您可能会收到以下错误信息:C:Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ System.Core.dll“/ reference:”C:\ Program Files \文件(x86)\参考程序集\ 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 Assembl ies \ 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:libr ary / 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赢10

C:\ Program Files文件(x86)\ MSBuild \ 14.0 \ bin \ csc.exe / noconfig / nowarn:1701,1702,2008 / nostdlib + / errorreport:提示符/警告: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 \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ Microsoft.CSharp.dll“/ reference:”C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \您可能会收到以下错误信息:C:Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ System.Core.dll“/ reference:”C:\ Program Files \文件(x86)\参考程序集\ 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 Assembl ies \ 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。

更新

这似乎是最新的胜利10更新的PC已经解决了这个问题。

Solutions Collecting From Web of "在VS 2015和VS 2013上运行相同代码的问题"

您可以尝试更改您的.net版本以下链接。

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

我相信这可能是一个.net版本问题,但是我不确定它会在你的应用程序中产生什么影响。

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

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

 VisualTreeHelper.GetParent(o); 

有时在对象初始化之前需要遵循一些序列并可以使用。

看看你的方法被调用的地方也是有帮助的。

不过,我通过搜索VisualTreeHelper.GetParent null找到了这个

VisualTreeHelper.GetParent返回null