How to execute different actions based on the framework? (ASP.NET 5)

99封情书 提交于 2020-01-04 23:59:13

问题


Suppose that you have to migrate a C# application from the .NET framework to the new .NET Core, because you want to have some of it new capabilities.

You have your code base and you want to get fully working parts at a time, since it will be a lot of work migrating all at once. The main idea is to be fully core in a future.

My question is:

  • Is there a way to check for functionality that is present in one framework but not in the other? Or to execute different actions based on the framework?

回答1:


There are compiler directives (#if) to check for symbols that correspond to the two frameworks. And it is also possible to target both frameworks.

If for instance you have code that uses resources that are not available as part of .NET Core, you can surround them in a conditional compilation directive.

Usage example of the compiler directives...

#if DNX451
    // Do something
#elif DNXCORE50
    // Do something
#else
#error No implementation for this target
# endif

Where DNX451 represents the .NET Framework and DNXCORE50 represents .NET Core.

See them in action on this video at 36 min.



来源:https://stackoverflow.com/questions/31438371/how-to-execute-different-actions-based-on-the-framework-asp-net-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!