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

时间:2016-04-30 13:13:27

标签: c# windows visual-studio visual-studio-2013 visual-studio-2015

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

在检查Nullreference时,错误是IsAssginableFrom,因为在VS2015中它找到了Canvas而不是在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:en-US / reference:&#34; C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ mscorlib.dll&#34; / reference:&#34; C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ Microsoft.CSharp.dll&#34; / reference:&#34; C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ mscorlib.dll&#34; / reference:&#34; C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ System.Core.dll&#34; / reference:&#34; C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ System.dll&#34; / reference:&#34; C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ System.Net.dll&#34; / reference:&#34; C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ System.Runtime.Serialization.dll&#34; / reference:&#34; C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ System.ServiceModel.dll&#34; / reference:&#34; C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ System.ServiceModel.Web.dll&#34; / reference:&#34; C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ System.Windows.dll&#34; / reference:&#34; C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ System.Xml.dll&#34; / reference:&#34; C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ System.Xml.Linq.dll&#34; / reference:&#34; C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ System.Xml.Serialization.dll&#34; / 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&#34; C:\ Users \ Jonas \ AppData \ Local \ Temp.NETPortable,Version = v4.0,Profile = Profile158.AssemblyAttributes.cs&#34;

来自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:&#34; C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ mscorlib.dll&#34; / reference:&#34; C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ Microsoft.CSharp.dll&#34; / reference:&#34; C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ mscorlib.dll&#34; / reference:&#34; C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ System.Core.dll&#34; / reference:&#34; C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ System.dll&#34; / reference:&#34; C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ System.Net.dll&#34; / reference:&#34; C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ System.Runtime.Serialization.dll&#34; / reference:&#34; C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ System.ServiceModel.dll&#34; / reference:&#34; C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ System.ServiceModel.Web.dll&#34; / reference:&#34; C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ System.Windows.dll&#34; / reference:&#34; C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ System.Xml.dll&#34; / reference:&#34; C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ System.Xml.Linq.dll&#34; / reference:&#34; C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETPortable \ v4.0 \ Profile \ Profile158 \ System.Xml.Serialization.dll&#34; / debug + / debug:full / filealign:512 / optimize- /out:obj\Debug\LibraryOfModels.dll / ruleset:&#34; C:\ Program Files(x86)\ Microsoft Visual Studio 14.0 \ Team Tools \ Static Analysis Tools \ Rule Sets \ MinimumRecommendedRules.ruleset&#34; / 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&#34; C:\ Users \ johann \ AppData \ Local \ Temp.NETPortable,Version = V4.0,档案= Profile158.AssemblyAttributes.cs&#34;

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

更新

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

2 个答案:

答案 0 :(得分:1)

您可以尝试在此链接后更改.net版本。

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

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

答案 1 :(得分:0)

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

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

VisualTreeHelper.GetParent(o);

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

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

但是我通过搜索VisualTreeHelper.GetParent null

在谷歌中找到了这个

VisualTreeHelper.GetParent returns null