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

5

在Visual Studio 2013和2015中运行一段代码时,我得到了两个不同的结果。在Visual Studio 2015中,我得到了一个NullReference错误,而在2013年,它按照预期工作。此外,Visual Studio 2015运行在Windows 10上,而2013年则运行在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);

当检查 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,但问题仍然存在。在Win8.1上,VS2013可以正常工作。

更新

来自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 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.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: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\Temp.NETPortable,Version=v4.0,Profile=Profile158.AssemblyAttributes.cs"

来自VS 2015 Win 10

"C:\Program Files (x86)\MSBuild\14.0\bin\csc.exe" is a command to compile C# code. The long list of options and references following the command specify various settings for the compilation process, such as which .NET framework libraries to use and where to output the compiled code.
我们能够看到这两个之间唯一的区别就是使用的.Net框架版本不同,其中一个是4.5.51650,而另一个则是4.6.01038。
更新:
看起来最新的win10电脑更新已经解决了这个问题。

1
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引起的。 - Lex Li
这是面向对象编程中的On Error Resume Next。使用调试器,告诉我们你看到了什么。并且发布一个每个人都可以运行的真实重现代码片段。 - Hans Passant
@Peuczyński 目标框架在这种情况下几乎没用。实际运行代码的运行时通常导致这样的差异。 - Lex Li
@LexLi,我有点困惑。在您的第一条评论中,您说vs2015使用4.6,这可能与vs2013 4.5.1不同。它确实是不同的。现在您说运行时不同,而在维基页面https://en.wikipedia.org/wiki/Common_Language_Runtime上,您可以看到自.NET的4.版本以来CLR是相同的。您能详细说明/指导我一些资源,以澄清我的疑虑吗? - Peuczynski
1
@JTIM 我建议尝试安装4.5.2版本。我相信它可以解决你的问题,但是我不确定对你的应用程序会产生什么影响。此外,我也不知道它是否会影响部署。因为我不确定Windows Phone OS 10上是否已安装4.5.2框架。 - Paulo Prestes
显示剩余23条评论
2个回答

1

0

我只是猜测,因为您没有发布错误消息本身。通常情况下,除了评论中提到的默认.NET运行时版本之外,VS2013和VS2015之间不应该有任何区别。

但是,从代码来看,静态部分可能是问题所在。

VisualTreeHelper.GetParent(o);

有时需要按照一定的顺序初始化对象才能使用。
看到方法被调用的位置也会很有帮助。
然而,我通过在 Google 上搜索 VisualTreeHelper.GetParent null 找到了这个。 VisualTreeHelper.GetParent 返回 null

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接