Well… maybe I’m misunderstanding the question, so apologies in advance.
You’re correct in that there’s no “namespace” keyword. Your class name includes an implicit namespace, e.g., MyCompany.MyClass would have a namespace of “MyCompany,” but it’s really just part of the class name. Kinda like the “AD” prefix in Get-ADUser is a sort of ersatz namespace. But you don’t declare a namespace per se.
The class support in v5 is still a little nascent, so it’s not going to do everything you’re accustomed to from other languages. The team’s plan seems to be to improve it over time.
I’m sorry. I might have done some wrong assumptions. I haven’t written that much C# (which I find very similar to PowerShell in many ways) and I’m not sure about the full meaning of the “namespace” keyword in C#.
Let me try to explain more what I’m trying to do.
What I would like to be able to do in PowerShell is something similar to the following C# code:
using System;
namespace MyNamespace1 {
class MyClass {
public static void MyMethod() {
Console.WriteLine("This is class: {0}", typeof(MyClass));
}
}
}
namespace MyNamespace2 {
class MyClass {
public static void MyMethod() {
Console.WriteLine("This is class: {0}", typeof(MyClass));
}
}
}
class MyProgram {
static int Main() {
MyNamespace1.MyClass.MyMethod();
MyNamespace2.MyClass.MyMethod();
return 0;
}
}
From your blog post and reply I understand that you should be able to write something like this in PowerShell:
class MyNamespace1.MyClass {
MyMethod() {
Write-Host "This is a test"
}
}
Sadly this does not work and will provide the error: “Missing ‘class’ body in ‘class’ declaration.”.
I looks like periods aren’t allowed in the class name in WMF5.0. Maybe this has changed since the preview you used when writing your blog post?
So if the “namespace” keyword doesn’t exist and periods aren’t allowed in the class name, how are you supposed to declare classes in a namespace?
Yeah, you’re just trying to do a bit more with it than PowerShell’s ready for, yet ;). Classes were introduced mainly to support class-based DSC Resources, which are a very specific use case. While they can be used for more than that, they’re not a fully fleshed-out feature, as yet. And yes, it’s changed a lot in the various preview releases.