Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is FEATURE_PAL compiler directive means in .net 4 source code

Tags:

c#

.net

I am having problem with understanding what FEATURE_PAL compiler directive means in .net 4.0 source code. It is used almost in every class that access unmanaged code.

like image 212
baraban Avatar asked Jul 18 '11 19:07

baraban


2 Answers

PAL = Platform Adaptation Layer. It is first and foremost a detail of the CLR, insulating it from the operating system implementation. You'll indeed see it used in the Reference Source copy of the source code for the .NET 4 classes. It appears in any code that has a strong dependency on the underlying operating system implementation, bypassing such code since it cannot work on an operating system other than Windows.

like image 145
Hans Passant Avatar answered Nov 19 '22 11:11

Hans Passant


You may be familiar with the use of the DEBUG and RELEASE directives to wrap blocks of code specific for testing/production code.

The .NET 4.0 framework uses the FEATURE_PAL compiler directive to execute code on newer platforms. It has to adapt to the platform by calling native methods found in native dlls. Hence the name of the feature: 'Platform adaptation layer'.

I came across this post after looking at the code for the Process class. One can image that killing a process works different on a given platform.

Think of the your code running on a newer version of Windows installed on a device that uses an ARM processor. When killing a process you need to call a method inside a native dll tailored to that platform.

So when compiling the framework for such platforms the FEATURE_PAL compiler directive is set as 'conditional compilation symbol'.

like image 38
mathijsuitmegen Avatar answered Nov 19 '22 11:11

mathijsuitmegen