Consider the following PowerShell snippet:
$csharpString = @\"
using System;
public sealed class MyClass
{
public MyClass() { }
public override stri
This technique works well for me:
if (-not ([System.Management.Automation.PSTypeName]'MyClass').Type)
{
Add-Type -TypeDefinition 'public class MyClass { }'
}
Internally, the PSTypeName class calls the LanguagePrimitives.ConvertStringToType() method which handles the heavy lifting. It caches the lookup string when successful, so additional lookups are faster.
I have not confirmed whether or not any exceptions are thrown internally as mentioned by x0n and Justin D.