问题
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