tags:

views:

67

answers:

4

I have a class in C# say for example

public class MyComputer : PSObject
{
    public string UserName
    {
        get { return userName; }
        set { userName = value; }
    }
    private string userName;

    public string DeviceName
    {
        get { return deviceName; }
        set { deviceName = value; }
    }
    public string deviceName;
}

which is derived from PSObject. I am loading the DLL having this code in powershell using import-module. Then I tried to create a new object of the MyComputer class in PowerShell.

PS C:> $MyCompObj = New-Object MyComputer

but it throws an error saying make sure assembly containing this type is loaded. Note: I am able to call Cmdlets successfully which is present in the DLL.

I am not sure is this the right way to go ahead on creating a new object. Please correct to me make this work.

+1  A: 

You don't need to have PSObject as base. Simply declare class without base.

Add-Type -typedef @"
public class MyComputer
{
    public string UserName
    {
        get { return _userName; }
        set { _userName = value; }
    }
    string _userName;

    public string DeviceName
    {
        get { return _deviceName; }
        set { _deviceName = value; }
    }
    string _deviceName;
}
"@

New-Object MyComputer | fl *

Later when you will work with the object, PowerShell will automatically wrap it into PsObject instance.

[3]: $a = New-Object MyComputer
[4]: $a -is [psobject]
True
stej
Thanks for your reply.If I am defining the class directly in PowerShell then what you said will work. But the class is inside the C# DLL, so this technique fails.
Shaj
@stej, C# automatic properties are your friend. :-) Just be sure to set the Language parameter to CSharpVersion3.
Keith Hill
@Keith, right. I just edited Shaj's example :)
stej
A: 

Is the MyComputer class in a namespace? If so, you probably need to use the namespace-qualifed name of the class in the New-Object command.

Also, PowerShell does not like the public names DeviceName and deviceName which differ only in case. You probably meant to declare deviceName private. (But why not use auto-properties?)

Finally, stej is correct. There is no need to derive the MyComputer class from PSObject.

OldFart
No luck. Tried with namespace too.Also tried after removing PSObject derivation.
Shaj
FYI: MyComputer is a class inside one namespace.
Shaj
A: 

Here is how it got worked.

public class MyComputer

{ public string UserName { get { return userName; } set { userName = value; } } private string userName;

public string DeviceName
{
    get { return deviceName; }
    set { deviceName = value; }
}
public string deviceName;

}

//PS C:\> $object = New-Object Namespace.ClassName

PS C:> $object = New-Object Namespace.MyComputer PS C:> $object.UserName = "Shaj" PS C:> $object.DeviceName = "PowerShell"

Shaj
+1  A: 

First, make sure the assembly is loaded using

[System.Reflection.Assembly]::LoadFrom("C:\path-to\my\assembly.dll")

Next, use the fully qualified class name

$MyCompObj = New-Object My.Assembly.MyComputer
devio