PoshCode Archive  Artifact [d221ba8949]

Artifact d221ba8949d0c7b0c94b7ef2b213e7e31c1a12dfb4a49094a8f368a79d30d86a:

  • File Invoke-AddTypeTypeDefini.ps1 — part of check-in [bff95b023f] at 2018-06-10 13:06:24 on branch trunk — From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes (user: Lee Holmes size: 1311)

# encoding: ascii
# api: powershell
# title: Invoke-AddTypeTypeDefini
# description: From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes
# version: 0.1
# type: class
# author: Lee Holmes
# license: CC0
# x-poshcode-id: 2173
# x-archived: 2016-03-19T00:35:42
# x-published: 2011-09-09T21:41:00
#
#
#############################################################################
##
## Invoke-AddTypeTypeDefinition
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
##############################################################################

<#

.SYNOPSIS

Demonstrates the use of the -TypeDefinition parameter of the Add-Type
cmdlet.

#>

Set-StrictMode -Version Latest

## Define the new C# class
$newType = @'
using System;

namespace PowerShellCookbook
{
    public class AddTypeTypeDefinitionDemo
    {
        public string SayHello(string name)
        {
            string result = String.Format("Hello {0}", name);
            return result;
        }
    }
}

'@

## Add it to the Powershell session
Add-Type -TypeDefinition $newType

## Show that we can access it like any other .NET type
$greeter = New-Object PowerShellCookbook.AddTypeTypeDefinitionDemo
$greeter.SayHello("World");