As you may have tried the FamilyTree written in PowerShell class. It seems to be straight forward. And in some cases, you may want to write a class in C#. Following example shows you how to build a C# class library and to be loaded into SHiPS.
- Follow the instruction here to compile SHiPS.
- if you build the SHiPS code from Visual Studio,
Microsoft.PowerShell.SHiPS.dllwill be generated under SHiPS\test\ folder - if you using dotnet cli build, the
Microsoft.PowerShell.SHiPS.dllcan be found under SHiPS\src\out\SHiPS - future: you can
Install-Module SHiPSto install the latest version of SHiPS.
- if you build the SHiPS code from Visual Studio,
- Open your Visual Studio or your favorite editor, copy the following code, save it as FamilyTreeInCSharp.cs
- Add a reference to Microsoft.PowerShell.SHiPS.dll
- Compile it. An assembly, FamilyTreeInCSharp.dll, will be generated.
using System.Collections.Generic;
using Microsoft.PowerShell.SHiPS;
namespace FamilyTreeInCSharp
{
/// <summary>
/// A class defines the family tree root.
/// </summary>
public class Root : SHiPSDirectory
{
public Root(string name) : base(name)
{
}
public override object[] GetChildItem()
{
return new List<object>
{
new Erin("Erin"),
new Ethen("Ethen")
}.ToArray();
}
}
/// <summary>
/// Defines a node with children.
/// </summary>
public class Erin : SHiPSDirectory
{
public Erin(string name) : base(name)
{
}
public override object[] GetChildItem()
{
return new object[] { new Mike("Erin's kid")};
}
}
/// <summary>
/// Defines a node with children.
/// </summary>
public class Mike : SHiPSDirectory
{
public Mike(string name) : base(name)
{
}
public override object[] GetChildItem()
{
return new object[] {"Hello I am Mike."};
}
}
/// <summary>
/// Defines a leaf node.
/// </summary>
public class Ethen : SHiPSLeaf
{
public Ethen(string name) : base(name)
{
}
}
}New-ModuleManifest -path .\FamilyTreeInCSharp.psd1Open the FamilyTreeInCSharp.psd1 in your favorite editor, edit the following line in the FamilyTreeInCSharp.psd1. Since the business logic is implemented in FamilyTreeInCSharp.dll, set RootModule to FamilyTreeInCSharp.dll.
RootModule = 'FamilyTreeInCSharp.dll'Save the file.
-
If you build the code using dotnet Cli
- cd to your git clone folder
- cd SHiPS\src\out
- Import-Module SHiPS
-
If you build via Visual Studio
- cd to youclonefolder\SHiPS\Test
- Import-Module SHiPS
-
If you installed the module from PowerShellGallery, simply Import-Module SHiPS
NOTE: If you build the assembly, you may need to ignore strong name signing on Windows.
Import-Module .\FamilyTreeInCSharp.psd1
new-psdrive -name ft -psprovider SHiPS -root 'FamilyTreeInCSharp#FamilyTreeInCSharp.Root'
cd ft:
dirOutput:
PS C:\> new-psdrive -name ft -psprovider SHiPS -root 'FamilyTreeInCSharp#FamilyTreeInCSharp.Root'
Name Used (GB) Free (GB) Provider Root
---- --------- --------- -------- ----
ft SHiPS FamilyTreeInCSharp#FamilyTreeInC...
PS ft:\> dir
Container: Microsoft.PowerShell.SHiPS\SHiPS::FamilyTreeInCSharp#FamilyTreeInCSharp.Root
Mode Name
---- ----
+ Erin
. Ethen
PS ft:\> cd Erin
PS ft:\Erin> dir
Container: Microsoft.PowerShell.SHiPS\SHiPS::FamilyTreeInCSharp#FamilyTreeInCSharp.Root\Erin
Mode Name
---- ----
+ Erin's kid
PS ft:\Erin> cd '.\Erin''s kid\'
PS ft:\Erin\Erin's kid> dir
Hello I am Mike.