Add-Type : Cannot add type. The type name ... already exists. - by johndog

Status : 

  Won't Fix<br /><br />
		Due to several factors the product team decided to focus its efforts on other items.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.


5
0
Sign in
to vote
ID 742760 Comments
Status Closed Workarounds
Type Suggestion Repros 0
Opened 5/19/2012 10:26:20 AM
Access Restriction Public

Description

If you try to add this code to a library script, and execute it more than once:

Add-Type -Language CSharp @"
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Runtime.InteropServices;
    
    public static class FILETIMEExtensions { 
        public static DateTime ToDateTime(this System.Runtime.InteropServices.ComTypes.FILETIME filetime ) { 
            long highBits = filetime.dwHighDateTime; 
            highBits = highBits << 32; 
            return DateTime.FromFileTimeUtc(highBits + (long)filetime.dwLowDateTime); 
        } 
    } 
"

You will often get this error:

Add-Type : Cannot add type. The type name 'FILETIMEExtensions' already exists.
At E:\Projects\powershell\pslib\general.ps1:34 char:1
+ Add-Type -Language CSharp @"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (FILETIMEExtensions:String) [Add-Type], Exception
    + FullyQualifiedErrorId : TYPE_ALREADY_EXISTS,Microsoft.PowerShell.Commands.AddTypeCommand
 
The only way around this is to kill and restart powershell.

This makes revision cycles very difficult.   It would be great if PSH could manage the types and namespaces in a way that support a revision cycle within a powershell instance.
Sign in to post a comment.
Posted by johndog on 2/13/2015 at 9:23 AM
-ErrorAction SilentlyContinue is only a partial workaround.

If you literally need to replace the type because you have a different definition, then there is no workaround other than restarting the process.
Posted by johndog on 2/13/2015 at 9:20 AM
-ErrorAction SilentlyContinue is only a partial workaround: If you literally need to replace the type because you have a different definition, then there is no workaround other than restarting the process.
Posted by Samurai Giblet on 2/13/2015 at 9:02 AM
You can work around this by using "-ErrorAction SilentlyContinue"

e.g.

Add-Type -ErrorAction SilentlyContinue @"

public enum VMState
{
            Unknown = 0
        , Running = 2
        , Stopped = 3
        , Paused = 32768
        , Suspended = 32769
        , Starting = 32770
        , Snapshotting = 32771
        , Saving = 32773
        , Stopping = 32774
        , Pausing = 32776
        , Resuming = 32777
}

public enum ReturnCode : int
{
                        OK = 0,
                JobStarted = 4096,
                    Failed = 32768,
             AccessDenied = 32769,
             NotSupported = 32770,
                 Unknown = 32771,
                 Timeout = 32772,
         InvalidParameter = 32773,
             SystemInUse = 32774,
InvalidStateForOperation = 32775,
         IncorrectDataType = 32776,
        SystemNotAvailable = 32777,
             OutOfMemory = 32778,
             FileNotFound = 32779
}
"@
Posted by Microsoft on 5/21/2012 at 2:46 PM
We cannot fix this due to limitations within the .NET Framework.