1

I have a Powershell script where I would like to create a generic List of a type that is defined in a custom (C++/CLI) assembly.

If I run new-object My.Namespace.MyClass, I can create objects all day. However, if I run new-genericobject System.Collections.Generic.List -Of My.Namespace.MyClass, I get the below error:

New-GenericObject : Could not construct Closed Type using the provided arguments.
At line:1 char:18
+ new-genericobject <<<<  System.Collections.Generic.List -Of My.Namespace.MyClass
    + CategoryInfo          : NotSpecified: (:) [New-GenericObject], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException,Pscx.Commands.NewGenericObjectCommand

I can create lists of strings, so the basic functionality of new-genericobject is fine. Does anyone have ideas on what this error means and how it can be resolved?

2 Answers 2

2

Can you try this instead:

$list = new-object 'collections.generic.list[my.namespace.myclass]'

btw, what version of powershell are you using? what operating system?

Update

This might be a long running bug where if the element type is in a third party assembly and the collection is not in the same assembly, you must fully qualify the element type. Try this:

$list = new-object ("collections.generic.list[[{0}]]" `
    -f [MyClass].AssemblyQualifiedName)
Sign up to request clarification or add additional context in comments.

3 Comments

That results in the below error: New-Object : Cannot find type [system.collections.generic.list[My.Namespace.MyClass]]: make sure the assembly containing this type is loaded. At line:1 char:11 + new-object <<<< 'system.collections.generic.list[My.Namespace.MyClass]' + CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand This is using Powershell version 2.0 on WinXP SP3.
Ah, I think this is a long existing bug in powershell where if the element type is in a different assembly than the collection and it is not part of the BCL, you must use a fully qualified type name. I've updated the answer.
Hmm, I can't seem to get that to work no matter what. I tried several variations of that approach (your approach, using it without the formatted string, etc) and I always get the above error about making sure the assembly is loaded. After doing some Googling, I found the below approach which does work but is rather verbose: function CreateGenericList { $dummy = New-Object My.Namespace.MyClass $generic = [System.Collections.Generic.List``1] $gt = $base.MakeGenericType(@($dummy.GetType())) ,[Activator]::CreateInstance($gt) }
1

I don't really like this solution, but it is the only thing I've found to work:

function CreateGenericList
{
    $dummy = New-Object My.Namespace.MyClass
    $generic = [System.Collections.Generic.List``1]
    $gt = $generic.MakeGenericType(@($dummy.GetType()))
    ,[Activator]::CreateInstance($gt)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.