< Back
Add-CType
Post
NAME Add-CType
SYNOPSIS
Add a locally-defined type to this runspace.
SYNTAX
Add-CType [-TypeName] <String> [[-Namespace] <String>] [[-ParentTypeName] <String>] [[-Using] <String[]>]
[[-ReferencedAssemblies] <String[]>] [-SqlWrapper] [[-SqlTemplateObject] <DataRow>] [-NoFormatData]
[[-PropertyName] <String>] [[-PropertyType] <String>] [[-PropertyTableWidth] <String>] [[-PropertyKeywords]
<String[]>] [<CommonParameters>]
DESCRIPTION
Add a locally-defined type to this runspace, and optionally define table formatting for the type.
These types can be used as parameter types and in OutputType([typename]) declarations.
PARAMETERS
-TypeName <String>
Name for the new type. You can include the namespace here or else specify it separately.
Required? true
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
-Namespace <String>
Namespace containing the new type. If not specified, the namespace is extracted from TypeName.
Required? false
Position? 2
Default value
Accept pipeline input? false
Accept wildcard characters? false
-ParentTypeName <String>
Parent class of the new class, default is System.Object
Required? false
Position? 3
Default value
Accept pipeline input? false
Accept wildcard characters? false
-Using <String[]>
Other namespaces, typically those containing types specified in PropertyType
Required? false
Position? 4
Default value
Accept pipeline input? false
Accept wildcard characters? false
-ReferencedAssemblies <String[]>
Other assemblies, typically those containing types specified in PropertyType
Required? false
Position? 5
Default value
Accept pipeline input? false
Accept wildcard characters? false
-SqlWrapper [<SwitchParameter>]
Make the new type a SQL wrapper class for use in ConvertTo-CTypeSqlWrapper.
This is set automatically if any PropertyKeyword contains SQLWrapper,
or if SQLTemplateObject is set.
Required? false
Position? named
Default value False
Accept pipeline input? false
Accept wildcard characters? false
-SqlTemplateObject <DataRow>
If specified, include all properties of this SQL template object in the SQL wrapper class.
Properties already specified in PropertyName are not affected.
SqlWrapper is automatically set if this is specified.
Required? false
Position? 6
Default value
Accept pipeline input? false
Accept wildcard characters? false
-NoFormatData [<SwitchParameter>]
Skip defining formatting metadata for this type.
For example, you might want to combine all the formatting for the types in your module
into a single file.
Required? false
Position? named
Default value False
Accept pipeline input? false
Accept wildcard characters? false
-PropertyName <String>
Name of a property of the type being defined
Required? false
Position? 7
Default value
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false
-PropertyType <String>
Type of a property of the type being defined
Required? false
Position? 8
Default value
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false
-PropertyTableWidth <String>
Table width in characters of a property of the type being defined.
If one or more properties specify PropertyTableWidth, a formatting directive
will be generated for this type and entered into the runspace.
If not specified, this property is not in the default table format.
This can also be set to '*', in which case this property is displayed at full width;
this should generally only be used for the last property with defined PropertyTableWidth.
Required? false
Position? 9
Default value
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false
-PropertyKeywords <String[]>
Other keywords. The only keywords currently implemented are "Trim"
which trims leading/trailing whitespace from the output string in the table formatting,
and "SQLWrapper" which designates a read-only property read from the wrapped SQL object.
Required? false
Position? 10
Default value
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).
INPUTS
OUTPUTS
NOTES
This method should only be called once for any class. Calling a second time
is generally harmless as long as the type definition is identical,
otherwise an error will be reported. Types cannot be altered once added to
a process (AppDomain?).
-------------------------- EXAMPLE 1 --------------------------
PS C:\\># Define a new type
PS> @"
PropertyName,PropertyType,PropertyTableWidth,PropertyKeywords
PrimaryTarget,string,25,Trim
SecondaryTarget,string,25,Trim
Weight,int,10
IsPrimary,Boolean,10
Description,String,*
MainConnection,SqlConnection
SecondaryConnection,SqlConnection
"@ | ConvertFrom-Csv | Add-CType -TypeName MyType `
-Namespace MyCompany.Namespace `
-Using System.Data.SqlClient `
-ReferencedAssemblies System.Data
# You can now use this as a full-fledged type, for example define parameters as
# param( [MyCompany.Namespace.MyType]$MyParameter )
# Create an instance of the type
PS> New-Object -TypeName MyCompany.Namespace.MyType -Property @{
PrimaryTarget = "String1";
SecondaryTarget = "String2";
Weight = 4;
Description = "This is a very long description"
}
PrimaryTarget SecondaryTarget Weight IsPrimary Description
------------- --------------- ------ --------- -----------
String1 String2 4 False This is a very long description
-------------------------- EXAMPLE 2 --------------------------
PS C:\\># Define a new CType Class
ConvertFrom-Csv @'
PropertyType,PropertyName,PropertyTableWidth,PropertyKeywords
string,Name,20
string,HostName,20
DateTime,WhenCreated,25
DateTime,WhenDeleted,25
'@ | Add-CType -TypeName MyCompany.MyNamespace.MyClass
# Create wrapped object
New-Object -Type MyCompany.MyNamespace.MyClass -Property @{
Name = "MyName"
WhenCreated = (get-date)
}
-------------------------- EXAMPLE 3 --------------------------
PS C:\\># Define a new SQL Wrapper Class
ConvertFrom-Csv @'
PropertyType,PropertyName,PropertyTableWidth,PropertyKeywords
string,Name,20,SQLWrapper
string,HostName,20,SQLWrapper
DateTime,WhenCreated,25,SQLWrapper
DateTime,WhenDeleted,25,SQLWrapper
'@ | Add-CType -TypeName MyCompany.MyNamespace.MyClass -SqlWrapper
# Create wrapped objects
$SqlWrappedObjects = $SqlDataRows | ConvertTo-CTypeSqlWrapper -TypeName MyCompany.MyNamespace.MyClass
RELATED LINKS
ConvertTo-CTypeSqlWrapper
Add-Type
Update-CTypeFormatData
Update-FormatData
SYNOPSIS
Add a locally-defined type to this runspace.
SYNTAX
Add-CType [-TypeName] <String> [[-Namespace] <String>] [[-ParentTypeName] <String>] [[-Using] <String[]>]
[[-ReferencedAssemblies] <String[]>] [-SqlWrapper] [[-SqlTemplateObject] <DataRow>] [-NoFormatData]
[[-PropertyName] <String>] [[-PropertyType] <String>] [[-PropertyTableWidth] <String>] [[-PropertyKeywords]
<String[]>] [<CommonParameters>]
DESCRIPTION
Add a locally-defined type to this runspace, and optionally define table formatting for the type.
These types can be used as parameter types and in OutputType([typename]) declarations.
PARAMETERS
-TypeName <String>
Name for the new type. You can include the namespace here or else specify it separately.
Required? true
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
-Namespace <String>
Namespace containing the new type. If not specified, the namespace is extracted from TypeName.
Required? false
Position? 2
Default value
Accept pipeline input? false
Accept wildcard characters? false
-ParentTypeName <String>
Parent class of the new class, default is System.Object
Required? false
Position? 3
Default value
Accept pipeline input? false
Accept wildcard characters? false
-Using <String[]>
Other namespaces, typically those containing types specified in PropertyType
Required? false
Position? 4
Default value
Accept pipeline input? false
Accept wildcard characters? false
-ReferencedAssemblies <String[]>
Other assemblies, typically those containing types specified in PropertyType
Required? false
Position? 5
Default value
Accept pipeline input? false
Accept wildcard characters? false
-SqlWrapper [<SwitchParameter>]
Make the new type a SQL wrapper class for use in ConvertTo-CTypeSqlWrapper.
This is set automatically if any PropertyKeyword contains SQLWrapper,
or if SQLTemplateObject is set.
Required? false
Position? named
Default value False
Accept pipeline input? false
Accept wildcard characters? false
-SqlTemplateObject <DataRow>
If specified, include all properties of this SQL template object in the SQL wrapper class.
Properties already specified in PropertyName are not affected.
SqlWrapper is automatically set if this is specified.
Required? false
Position? 6
Default value
Accept pipeline input? false
Accept wildcard characters? false
-NoFormatData [<SwitchParameter>]
Skip defining formatting metadata for this type.
For example, you might want to combine all the formatting for the types in your module
into a single file.
Required? false
Position? named
Default value False
Accept pipeline input? false
Accept wildcard characters? false
-PropertyName <String>
Name of a property of the type being defined
Required? false
Position? 7
Default value
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false
-PropertyType <String>
Type of a property of the type being defined
Required? false
Position? 8
Default value
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false
-PropertyTableWidth <String>
Table width in characters of a property of the type being defined.
If one or more properties specify PropertyTableWidth, a formatting directive
will be generated for this type and entered into the runspace.
If not specified, this property is not in the default table format.
This can also be set to '*', in which case this property is displayed at full width;
this should generally only be used for the last property with defined PropertyTableWidth.
Required? false
Position? 9
Default value
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false
-PropertyKeywords <String[]>
Other keywords. The only keywords currently implemented are "Trim"
which trims leading/trailing whitespace from the output string in the table formatting,
and "SQLWrapper" which designates a read-only property read from the wrapped SQL object.
Required? false
Position? 10
Default value
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).
INPUTS
OUTPUTS
NOTES
This method should only be called once for any class. Calling a second time
is generally harmless as long as the type definition is identical,
otherwise an error will be reported. Types cannot be altered once added to
a process (AppDomain?).
-------------------------- EXAMPLE 1 --------------------------
PS C:\\># Define a new type
PS> @"
PropertyName,PropertyType,PropertyTableWidth,PropertyKeywords
PrimaryTarget,string,25,Trim
SecondaryTarget,string,25,Trim
Weight,int,10
IsPrimary,Boolean,10
Description,String,*
MainConnection,SqlConnection
SecondaryConnection,SqlConnection
"@ | ConvertFrom-Csv | Add-CType -TypeName MyType `
-Namespace MyCompany.Namespace `
-Using System.Data.SqlClient `
-ReferencedAssemblies System.Data
# You can now use this as a full-fledged type, for example define parameters as
# param( [MyCompany.Namespace.MyType]$MyParameter )
# Create an instance of the type
PS> New-Object -TypeName MyCompany.Namespace.MyType -Property @{
PrimaryTarget = "String1";
SecondaryTarget = "String2";
Weight = 4;
Description = "This is a very long description"
}
PrimaryTarget SecondaryTarget Weight IsPrimary Description
------------- --------------- ------ --------- -----------
String1 String2 4 False This is a very long description
-------------------------- EXAMPLE 2 --------------------------
PS C:\\># Define a new CType Class
ConvertFrom-Csv @'
PropertyType,PropertyName,PropertyTableWidth,PropertyKeywords
string,Name,20
string,HostName,20
DateTime,WhenCreated,25
DateTime,WhenDeleted,25
'@ | Add-CType -TypeName MyCompany.MyNamespace.MyClass
# Create wrapped object
New-Object -Type MyCompany.MyNamespace.MyClass -Property @{
Name = "MyName"
WhenCreated = (get-date)
}
-------------------------- EXAMPLE 3 --------------------------
PS C:\\># Define a new SQL Wrapper Class
ConvertFrom-Csv @'
PropertyType,PropertyName,PropertyTableWidth,PropertyKeywords
string,Name,20,SQLWrapper
string,HostName,20,SQLWrapper
DateTime,WhenCreated,25,SQLWrapper
DateTime,WhenDeleted,25,SQLWrapper
'@ | Add-CType -TypeName MyCompany.MyNamespace.MyClass -SqlWrapper
# Create wrapped objects
$SqlWrappedObjects = $SqlDataRows | ConvertTo-CTypeSqlWrapper -TypeName MyCompany.MyNamespace.MyClass
RELATED LINKS
ConvertTo-CTypeSqlWrapper
Add-Type
Update-CTypeFormatData
Update-FormatData