Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typing fully qualified type names in powershell?

I want to reduce typing System and other root namespaces, for example

[string] $ComputerName = [Environment]::MachineName

vs.

[System.String] $ComputerName = [System.Environment]::MachineName

Another example:

[Net.Dns]::GetHostEntry("192.168.1.1")

vs.

[System.Net.Dns]::GetHostEntry("192.168.1.1")

Are there any reasons and specific situation when typing System and similar parent namespaces is required?

I often wonder why is there System namespace after all since everything is inside that namespace so what the deal with that namespace? it's a nonsense; what is the term System supposed to mean anyway? it's not related to operating system here. but to everything in NET framework.

I assume that there may be exceptions when calling static methods, but I don't know C# so unable to answer this myself.

like image 981
metablaster Avatar asked Mar 03 '23 18:03

metablaster


1 Answers

In somewhat jumbled order:

it's a nonsense; what is the term System supposed to mean anyway?

The "System" in question is the .NET runtime and framework. The original idea, as far as I understand, was that code that doesn't ship with .NET would fall under different namespaces - but multiple Microsoft-authored components built on top of .NET has since made use of System parent namespace - including all the APIs that make up PowerShell itself.

I often wonder why is there [a] System namespace after all since everything is inside that namespace so what the deal with that namespace?

Not "everything" is inside the System namespace, but as mentioned above, everything that ships with the runtime or base class libary is - which is exactly why PowerShell automatically resolve type literals even if you omit System. from a qualified type name - PowerShell is trying to help you reduce typing already

Are there any reasons and specific situation when typing System and similar parent namespaces is required?

Yes - when the parent namespace is not System.

The first example that comes to mind is the .NET wrapper classes for the Win32 registry API on Windows:

$HKLM = [Microsoft.Win32.RegistryKey]::OpenBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, [Microsoft.Win32.RegistryView]::Default)

Now, for the actual question:

I want to reduce typing System and other root namespaces

You can't add custom namespace prefixes (like System) to PowerShell's name resolution method, but you can declare automatic resolution of type names in specific namespaces in PowerShell 5 and up, with the using namespace directive.

Without using namespace:

[System.Net.Dns]::GetHostEntry("192.168.1.1")

With using namespace:

using namespace System.Net

[Dns]::GetHostEntry("192.168.1.1")

When used in a script, any using directives must precede anything else in the file, including the param block.

using namespace directives will work in an interactive session as well, granted that you issue it as a separate statement:

PS> using namespace System.Net 
PS> [Dns] # still works!
like image 164
Mathias R. Jessen Avatar answered Mar 11 '23 08:03

Mathias R. Jessen