4

在Visual Studio 2013和2015中运行一段代码时,我得到两个不同的结果。在Visual Studio 2015上,我得到了一个N​​ullReference,在2013年它按照它应该的方式工作。此外,Visual Studio 2015在Windows 8.1和Windows 8.1上运行。的代码段是:在VS 2015和VS 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); 

该错误是一个Nullreference当它检查IsAssginableFrom因为在VS2015它找到一个画布代替希望网格的在VS2013中找到?

堆栈跟踪

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:zh-CN/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“/参考:“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 Assemblies \ 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.NET Portable \ 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: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 \ Jonas \ AppData \ Local \ T emp.NETPortable,版本= V4.0,外形= Profile158.AssemblyAttributes。CS”

从VS 2015赢得10

C:\程序文件(x86)\的MSBuild \ 14.0 \ BIN \ CSC.EXE/noconfig/nowarn:1701,1702,2008包括/ nostdlib +/errorreport:prompt/warn:4/define:DEBUG; TRACE/errorendlocation/preferreduilang:zh-CN/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 Ass插件\ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ System.Core.dll“/ reference:”C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile:\ System.dll“/ reference:”C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ System.Net.dll“/ reference:”C:\ Program Files文件(x86)\参考程序集\ 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 = v 4.0,Profile = Profile158.AssemblyAttributes.cs“

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

更新

这似乎是最新赢得10更新PC已经解决了这个问题。

+1

的Windows 10和Visual Studio 2015年使用,这可能是由与Visual Studio 2013在Windows 8.1发货如果您还不升级.NET 4.5.1不同的.NET Framework 4.6.X。将在VS2013中编译的二进制文件复制到该Windows 10机器并运行它。如果它给你NRE,那很可能是由.NET 4.6.x引起的。 –

+0

@LexLi能否详细说明如何做到这一点,以及在Silverlight手机应用程序中何处可以找到它们。 – JTIM

+0

这是面向对象编程的On Error Resume Next。使用调试器,告诉我们你看到了什么。 *请*发布一个真正的repro片段,每个人都可以运行。 –

回答

0

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

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

VisualTreeHelper.GetParent(o); 

有时是需要遵循的初始化之前的对象,可以使用的序列。

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

但是,我通过搜索VisualTreeHelper发现了这一点。空的getParent

VisualTreeHelper.GetParent returns null