views:

266

answers:

6

If I want to create a .NET object in the powershell I write something like the following:

[System.Reflection.Assembly]::LoadWithPartialName("System.Xml") | out-null"
$doc = new-object -typename System.Xml.XmlDocument"

If I want to call a static .Net method I use a command similar to the following line:

$path = [System.String]::Format("{0} {1}", "Hello", "World")

I don't see the rule behind that. If it works in the first example, why can't I use System.String.Format in the second one?

+2  A: 

I believe you are confusing types and members here.

// type "Assembly" in the "System.Reflection" namespace
[System.Reflection.Assembly] 

// member method "LoadWithPartialName" of type "Assembly"
[System.Reflection.Assembly]::LoadWithPartialName 

// type "String" in the "System" namespace
[System.String] 

// member method "Format" of type "String"
[System.String]::Format

It's working as it should. Where does your confusion come from?

Tomalak
A: 

Yes, the way you worded your question is a bit confusing. System.Xml.XmlDocument and System.String are defined as classes, so you can use new-object for each of these to create an object of that particular type.

Just because you have "foo.bar.joe" doesn't mean that you're doing anything different than "abc.def". Both are valid classes, it just happens that they have a different namespace length/definition.

Marco Shaw
A: 

I see, probably, my question was a bit confusing.

What I was asking is: What's the reason for this syntax with the brackets and the colons? Why didn't Microsoft define it the way it is in C# - everything seperated by dots?

The question came up when I saw the following line (I actually wanted to put attention on):

$doc = new-object -typename "System.Xml.XmlDocument"

Obviously, sometimes the brackets/colon syntax is not needed, so why is it even existing?

next time, edit your original question. Don't think of stackoverflow as a forum. Your "clarification" is going to be at the bottom of this list of answers and nobody will see it. That will be confusing to those who come afrter.
halr9000
A: 

I'm guessing new-object just passes it to Activator, which expects a period-delimited name -- powershell doesn't actually manage that part.

Jimmy
A: 

In the case of line:

$doc = new-object -typename "System.Xml.XmlDocument"

the brackets are not needed because the argument -typename is of type string. Technically, we could call it like this:

$doc = new-object -typename "TypeThatDoesntExsit"

and it would show an error because it tries to dynamically resolve the type to a string.

siz
+3  A: 

The brackets are used when you are using a static method on a given class. When you are instantiating a new object of a particular class, you don't need the brackets.

The brackets are also used to cast variables to a certain type

PS C:\> $i = [int]"1"
PS C:\> $i.gettype().Name
Int32
PS C:\> $j = "1"
PS C:\> $j.gettype().Name
String
PS C:\>
Andy Schneider
And I thought the question author had a concept of static methods because he explicitly mentioned them. +1
Tomalak