Skip to content

Add-Type failed on use of System.ConsoleColor #3264

Closed

Description

@rkeithhill

Steps to reproduce

Execute this line at the PowerShell Core prompt (I tried this on Window 10 AU with PS Alpha 16):

Add-Type "public class Util { public static int GetVal(System.ConsoleColor color) { return (int)color; }}"

Expected behavior

The type is added and is usable.

Actual behavior

I get two errors and the type is not added:

Add-Type : (1) : The type or namespace name 'ConsoleColor' does not exist in the namespace 'System' (are you missing
an assembly reference?)
(1) : >>> public class Util { public static int GetVal(System.ConsoleColor color) { return (int)color; }}
At line:1 char:1
+ Add-Type "public class Util { public static int GetVal(System.Console ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Except
   ion
    + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand

Add-Type : Cannot add type. Compilation errors occurred.
At line:1 char:1
+ Add-Type "public class Util { public static int GetVal(System.Console ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Add-Type], InvalidOperationException
    + FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand

[System.ConsoleColor] is available at the prompt so I'm not sure why Add-Type is choking.

Environment data

Name                           Value
----                           -----
BuildVersion                   3.0.0.0
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSEdition                      Core
PSVersion                      6.0.0-alpha
PSRemotingProtocolVersion      2.3
CLRVersion
WSManStackVersion              3.0
GitCommitId                    v6.0.0-alpha.16
SerializationVersion           1.1.0.1

Activity

  1. rkeithhill commented on Mar 5, 2017

    @rkeithhill
    CollaboratorAuthor

    OK, found a workaround (perhaps the solution). This does compile:

    Add-Type "public class Util { public static int GetVal(System.ConsoleColor color) { return (int)color; }}" `
        -ReferencedAssemblies C:\Users\Keith\Downloads\powershell-6.0.0-alpha.16-win10-x64\System.Console.dll
  2. added
    Issue-Discussion
    on Mar 6, 2017
  3. iSazonov commented on Mar 6, 2017

    @iSazonov
    Collaborator

    Keith Hill (@rkeithhill) It seems duplicate #1616 Do you agree?

  4. rkeithhill commented on Mar 6, 2017

    @rkeithhill
    CollaboratorAuthor

    Yes, this is duplicate of that issue.

  5. added
    Resolution-Duplicate
    and removed
    Issue-Discussion
    on Mar 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution-DuplicateWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions