Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand conditional compilation for different .NET versions

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to understand conditional compilation for different .NET versions. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.

In order to compile successfully under .NET 2.0, I wrote a file Patch.cs that defines System.Runtime.CompilerServices.ExtensionAttribute types, so that lambda expressions and extension methods can be used in 2.0, and several Action types introduced by System.Core.dll are added:

Namespace System.Runtime.CompilerServices {public class ExtensionAttribute: Attribute {} namespace System {public delegate void Action (); public delegate void Action (T0t0T0T1T1);}

However, to compile under .NET 4.0, it is troublesome to comment out Patch.cs because the type already exists. So I want to solve it through conditional compilation, that is:

# if NET2 namespace System.Runtime.CompilerServices {public class ExtensionAttribute: Attribute {}} namespace System {public delegate void Action (); public delegate void Action (T0t0T1 T1);} # endif

The problem is that there are no indicators related to the. net version defined in. Net. What should I do? Do it yourself, have plenty of food and clothing, and use Build Events to automatically detect the .net version of the project before compilation and define the indicators we want.

In C # template programming (2): write C # preprocessor to make the template come naturally, a program Csmacro.exe is written to realize the template mechanism under C #. This paper adds the function of detecting the version of. Net referenced by the project on the basis of Csmacro.exe.

Principle: find the csproj file in the project directory, parse it, find the node TargetFrameworkVersion, judge the .net version, and then generate a Csmacro_Template.cs file with a # define version indicator. For example, for a .net 2.0 project, the contents of the generated Csmacro_Template.cs file are:

# define NET2

The modified Csmacro code can be downloaded at: https://github.com/xiaotie/GebCommon (currently only .net 2.0 and 4.0 are dealt with, and you can modify the code for other versions if you need it). With Csmacro, everything will be easy.

* step, put the Csmacro.exe under the Path path

Second, open the project that requires conditional compilation and add the Pre-build event: Csmacro.exe $(ProjectDir)

The third step is to edit the source file, for example, the Patch.cs file is modified to:

# region include "Csmacro_Template.cs" # endregion # if NET2 namespace System.Runtime.CompilerServices {public class ExtensionAttribute: Attribute {}} namespace System {public delegate void Action (); public delegate void Action (T0 t0 T1 T1);} # endif

# region include is the Csmacro macro syntax I introduced. For details, see C# template programming (2): write a C# preprocessor to make the template come naturally in 1.1 texts. Click compile, and the system will generate a Patch_Csmacro.cs file with the following contents:

# define NET2 # if NET2 namespace System.Runtime.CompilerServices {public class ExtensionAttribute: Attribute {}} namespace System {public delegate void Action (); public delegate void Action (T0t0T1 T1);} # endif

Step 4, add the generated Patch_Csmacro.cs to the project.

When you are done, choose a different target, and the compilation time produces the conditional compilation of the target!

The above is how to understand conditional compilation for different .NET versions. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report