5

When running a piece of code in Visual Studio 2013 and 2015, I get two different outcomes. On visual Studio 2015 I get an NullReference and in 2013 it works the way it should. Also Visual Studio 2015 is running on Windows 10 and 2013 on Windows 8.1. The piece of code is:

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

The code is called with:

Grid RiskGrid = FindParentOfType<Grid>(ChampViewModelSel);

The error is a Nullreference when it checks for IsAssginableFrom since in VS2015 it finds a Canvas instead of the wished Grid that is found in VS2013?

Stack Trace

  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

Update

Installed VS2013 on Win10, and the issue persists. VS2013 works fine on Win8.1

update

From 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"

From 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 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 /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"

The only difference we can see between the two is the .NetFrameworks being used. One has Version 4.5.51650 and the other uses 4.6.01038.

Update

It seems like the newest win 10 update for the pc has solved this issue.

JTIM
  • 2,774
  • 1
  • 34
  • 74
JonasN89
  • 1,386
  • 2
  • 11
  • 23
  • 1
    Windows 10 and Visual Studio 2015 uses .NET Framework 4.6.x which might be different from .NET 4.5.1 shipped with Visual Studio 2013 on Windows 8.1 if you did not yet upgrade it. Copy the binaries compiled in VS2013 to that Windows 10 machine and run it. If it gives you NRE, then it is very likely to be caused by .NET 4.6.x. – Lex Li Apr 30 '16 at 13:29
  • @LexLi could you elaborate with how to do this and where to find them when it is for a silverlight phone application. – JTIM May 13 '16 at 08:57
  • It is the On Error Resume Next of object-oriented programming. Use the debugger, tell us what you see. And *please* post a real repro snippet that everybody can run. – Hans Passant May 13 '16 at 22:29
  • Sometimes the .NET runtime changes behavior between versions and sometimes that change is completely undocumented. [Here's an answer](http://stackoverflow.com/a/26351745/751579) pointing that out one such case for a completely different problem that has nothing to do with this one - it's just an example. @LexLi's suggestion is a good one. – davidbak May 13 '16 at 22:38
  • The main difference is that in vs2015 you use different compiler (c# 6.0 compiler) Could you provide full stack trace of the exception? Also, @LexLi from what I understand it is the same project so the target framework would be the same. – Peuczynski May 15 '16 at 13:31
  • @Peuczyński target framework is almost useless in this case. The actual runtime that executes the code leads to such differences typically. – Lex Li May 15 '16 at 14:00
  • @LexLi I got a little confused. In your first comment you said that vs2015 uses 4.6 which migth be different from vs2013 4.5.1. It definatelly is. Now you say that the runtime is different whereas in wiki page https://en.wikipedia.org/wiki/Common_Language_Runtime you can see that the CLR is the same since version 4. of .NET. Can you elaborate/point me to some resource that would clarify things for me? – Peuczynski May 15 '16 at 14:35
  • @Peuczyński I said runtime, not CLR. Well, I should have said .NET Framework instead. – Lex Li May 15 '16 at 14:41
  • @Peuczyński I have added the stacktrace and some more information about the error. – JTIM May 16 '16 at 08:24
  • @LexLi I have added some more information stating that it finds a different parent, going through the parent tree, it is not possible to find the grid in VS2015 as it is on VS2013 – JTIM May 16 '16 at 08:26
  • I would try to output more information from the build process in both VS, (http://sedodream.com/2008/06/16/ChangingBuildVerbosityInVisualStudio.aspx) and then compare the compiler command from your project. If you post it here, it can help solve the problem as well. – Paulo Prestes May 17 '16 at 06:04
  • Your compile command will look similar to this. (http://www.pastebin.ca/3601937) – Paulo Prestes May 17 '16 at 06:11
  • .NET Framework or referenced DLL versions can be checked. Sometimes DLLs are not of the same version on different machines/OS/Runtime – Ranvir May 17 '16 at 07:34
  • @PauloPrestes We have now find two places that reference .NET in the output from the two systems, could you tell us if this is the correct or we need another spot? – JTIM May 18 '16 at 18:38
  • @JTIM the output that you posted is from your model dll? Do you have the output from your view dll? From this output the only diference is the version of msbuild being used 12 (vs13) and 14 (vs15). Also, have you tried change the .net framework in vs15 to be the same used in vs13? – Paulo Prestes May 18 '16 at 21:34
  • Show the UI structure you have – Zverev Evgeniy May 19 '16 at 07:12
  • @ZverevEugene do you mean the XAML-code ? If so, no it would be too big. If you mean the model, then we use MVVM, specifically the MVVMlight package. Please clarify this if you want other information? – JTIM May 19 '16 at 12:34
  • @PauloPrestes I have updated the win10 output, the win8 output will come later as JN89 can't access it now. Regarding the Framework I have tried the methods mentioned on MSDN, going into the projects properties, but it is not possible to change the version there. Could you elaborate on how you would change the framework for a wp8.1 silverlight project/solution? – JTIM May 19 '16 at 12:44
  • @JTIM I need to see the UI stack i.e. from the element you send into `FindParentOfType` to the root. – Zverev Evgeniy May 19 '16 at 12:52
  • @jtim probably you've tried this? https://msdn.microsoft.com/en-us/library/bb398202(v=vs.120).aspx You should've be able to change following this step. Anyway, reading your post again I'm a little bit confused. It doesn't work in win10? Doesn't matter the VS version? – Paulo Prestes May 19 '16 at 12:57
  • @PauloPrestes that is precisely the link, yes, however the ``target framework` is not shown only the `Target Windows Phone OS Version` .Correct no matter which version of Visual Studio on Win10. But the VS's on Win10 uses the same newer 4.6.01038. Compared to 4.5.51650 on Win8. – JTIM May 19 '16 at 13:07
  • @JTIM Try this link. https://msdn.microsoft.com/en-us/library/gg597391(v=vs.110).aspx go to "Target Options" – Paulo Prestes May 19 '16 at 13:22
  • @PauloPrestes quick read that is for a portable class Library, which we have. The targets for that is set to .Net Framework 4.5, which could be the issue? However seems odd it would propagate through? Trying to change the Framework I am prompted to install additional targets: http://getdotnet.azurewebsites.net/target-dotnet-platforms.html here it is said that 4.5.1 is no longer supported and it refers me to: https://blogs.msdn.microsoft.com/dotnet/2015/12/09/support-ending-for-the-net-framework-4-4-5-and-4-5-1/ where it is stated that older versions are highly supported in the newer versions:) – JTIM May 19 '16 at 13:34
  • @PauloPrestes it says that I can install another supported framework as 4.5.2, which I could try but I am unsure how changing it in the Portable library would impact the view ? But I am for trying it, if you believe it would have a benefit? or installing 4.5.1 in VS2013? – JTIM May 19 '16 at 13:36
  • 1
    @JTIM I would try install 4.5.2 I believe it can fix yours issue however, I don't know the impact on your app. Also, I don't know if it will impact deploy. As I'm not sure if 4.5.2 framework is installed on Windows Phone OS 10. – Paulo Prestes May 19 '16 at 13:46
  • @PauloPrestes Please provide it in an answer with any information you find relevant, I do not see why the bounty should be wasted, at least not when you have been so helpful. I think we will evaluate the current issue internally and agree on further steps, I appreciate the sparring and communication greatly ! – JTIM May 19 '16 at 13:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/112403/discussion-between-paulo-prestes-and-jtim). – Paulo Prestes May 19 '16 at 13:49
  • @JTIM I was looking to your code again and I believe you don't need to use `dynamic` type to the `parent`. I would suggest you to change to `object` type. It should at least change your stack error. Give a try and let me know. :) – Paulo Prestes May 19 '16 at 20:21

2 Answers2

1

You can try change your .net version following this link.

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

I believe it can be a .net version issue however, I'm not sure the impact it will have in your app.

Paulo Prestes
  • 484
  • 2
  • 8
0

I am just guessing since you haven't posted the error message itself. Usually there should be no difference between VS2013 and VS2015 except the default .NET runtime version as mentioned in the comment.

But looking at the code the static part could be the issue

VisualTreeHelper.GetParent(o);

Sometimes there are sequences that need to be followed before objects are initialized and can be used.

It would be also helpful to see where your method is being called.

However I found this in google by searching for VisualTreeHelper.GetParent null

VisualTreeHelper.GetParent returns null

Martin S
  • 377
  • 3
  • 14