Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Checking whether a type is loaded _efficiently_?

Reply
 
Old 07-31-2006   #1 (permalink)
Alex K. Angelopoulos [MVP]


 
 

Checking whether a type is loaded _efficiently_?

Does anyone have any thoughts on how to test whether a type is available
without attempting to create an instance of it?



My System SpecsSystem Spec
Old 07-31-2006   #2 (permalink)
Jouko Kynsijärvi


 
 

Re: Checking whether a type is loaded _efficiently_?

Alex K. Angelopoulos [MVP] wrote:
> Does anyone have any thoughts on how to test whether a type is
> available without attempting to create an instance of it?


This is pretty fast:

function IsTypeLoaded([string]$typeName, [string]$assemblyName) {
foreach ($asm in [AppDomain]::CurrentDomain.GetAssemblies()) {
if ($asm.GetName().Name -eq $assemblyName) {
if ($asm.GetType($typeName) -ne $null) {
return $true
}
}
}
return $false
}

IsTypeLoaded 'System.Data.DataSet' 'System.Data'


My System SpecsSystem Spec
Old 07-31-2006   #3 (permalink)
Jacques Barathon [MS]


 
 

Re: Checking whether a type is loaded _efficiently_?

You can do this:

$typename = "system.datetime"
[appdomain]::currentdomain.getassemblies() | % {$_.gettypes()} | ?
{$_.fullname -match $typename}| ft fullname, module -a

If you want an exact match, use "-like" instead of "-match".

Hope that helps,
Jacques

"Alex K. Angelopoulos [MVP]" <aka@online.mvps.org> wrote in message
news:%23XUOTVMtGHA.1956@TK2MSFTNGP05.phx.gbl...
> Does anyone have any thoughts on how to test whether a type is available
> without attempting to create an instance of it?
>
>



My System SpecsSystem Spec
Old 07-31-2006   #4 (permalink)
Jeffrey Snover [MSFT]


 
 

Re: Checking whether a type is loaded _efficiently_?

Just get the type info by putting the name of the type in []s

PS> [System.Management.Automation.Host.BufferCell]

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False BufferCell System.ValueType

PS> [Microsoft.Build.BuildEngine.BuildItem]
Unable to find type [Microsoft.Build.BuildEngine.BuildItem]: make sure that
the assembly containing this type is loaded
..
At line:1 char:39
+ [Microsoft.Build.BuildEngine.BuildItem] <<<<
PS>

--
Jeffrey Snover [MSFT]
Windows PowerShell/Aspen Architect
Microsoft Corporation
This posting is provided "AS IS" with no warranties, no confers rights.
Visit the Windows PowerShell Team blog at:
http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:
http://www.microsoft.com/technet/scr.../hubs/msh.mspx

"Alex K. Angelopoulos [MVP]" <aka@online.mvps.org> wrote in message
news:%23XUOTVMtGHA.1956@TK2MSFTNGP05.phx.gbl...
> Does anyone have any thoughts on how to test whether a type is available
> without attempting to create an instance of it?
>
>



My System SpecsSystem Spec
Old 08-01-2006   #5 (permalink)
Alex K. Angelopoulos [MVP]


 
 

Re: Checking whether a type is loaded _efficiently_?

"Jeffrey Snover [MSFT]" <jsnover@microsoft.com> wrote in message
news:uHh3XFPtGHA.2260@TK2MSFTNGP03.phx.gbl...
> Just get the type info by putting the name of the type in []s


.....

> PS> [Microsoft.Build.BuildEngine.BuildItem]
> Unable to find type [Microsoft.Build.BuildEngine.BuildItem]: make sure
> that the assembly containing this type is loaded
> .
> At line:1 char:39
> + [Microsoft.Build.BuildEngine.BuildItem] <<<<
> PS>


I rather like this approach, but I've noticed it can be hard to control the
error output from it. For example, if I want to test for type availability
and work around it myself, I would expect that I could do something like
this:

Invoke-Expression {[Microsoft.Build.BuildEngine.BuildItem]} -ev x -ea
SilentlyContinue

Unfortunately, that doesn't suppress error output or populate $x with the
error results. This is kind of tangential to the problem, but it might be a
nice topic for a blog entry as well - why doesn't this work as I expected,
and what is the best way to capture the error and suppress the error output
when dealing with expressions or script blocks where ErrorVariable and
ErrorAction aren't available?




My System SpecsSystem Spec
Old 08-01-2006   #6 (permalink)
Alex K. Angelopoulos [MVP]


 
 

Re: Checking whether a type is loaded _efficiently_?

Interesting. This - or, a little closer, Jacques' variation - can also be
used to enumerate all available types.

Some of the type names returned seem to be bogus, possibly due to oddities
in .NET itself. For example, I get back System.DateTimeFormat as a full name
for a type, and it cannot be created or referenced. However, if I look up
DateTimeFormat in the .NET docs, I get a hit for
System.Globalization.DateTimeFormatInfo, which actually CAN be referenced.

"Jouko Kynsijärvi" <jouko.kynsijarvi@nospam.nospam> wrote in message
news:OWyqDlOtGHA.3912@TK2MSFTNGP03.phx.gbl...
> Alex K. Angelopoulos [MVP] wrote:
>> Does anyone have any thoughts on how to test whether a type is
>> available without attempting to create an instance of it?

>
> This is pretty fast:
>
> function IsTypeLoaded([string]$typeName, [string]$assemblyName) {
> foreach ($asm in [AppDomain]::CurrentDomain.GetAssemblies()) {
> if ($asm.GetName().Name -eq $assemblyName) {
> if ($asm.GetType($typeName) -ne $null) {
> return $true
> }
> }
> }
> return $false
> }
>
> IsTypeLoaded 'System.Data.DataSet' 'System.Data'
>
>



My System SpecsSystem Spec
Old 08-02-2006   #7 (permalink)
=?Utf-8?B?ZGFuY2UyZGll?=


 
 

Re: Checking whether a type is loaded _efficiently_?

"Alex K. Angelopoulos [MVP]" wrote:
>
> Invoke-Expression {[Microsoft.Build.BuildEngine.BuildItem]} -ev x -ea
> SilentlyContinue
>
> Unfortunately, that doesn't suppress error output or populate $x with the
> error results.


I am not sure why Erroraction preference for the cmdlet was not honored or
not.
So i have set the error action preference in another scope to suppress the
error message and set the error message(returned by "trap" statement) to the
variable named "$err"

==============================
[^_^]PS[38]>& { $ErrorActionPreference = "SilentlyContinue";
[Microsoft.Build.BuildEngine.BuildItem]; trap { $_; } } | Set-Variable err
[^_^]PS[39]>$err
Unable to find type [Microsoft.Build.BuildEngine.BuildItem]: make sure that
the assembly containing this type is loaded
..
At line:1 char:89
+ & { $ErrorActionPreference = "SilentlyContinue";
[Microsoft.Build.BuildEngine.BuildItem]; <<<< trap { $_; } } | Set
-Variable err
==============================

I could have used automatic variable "$error" to check if an error was
generated but with $error, i couldn't tell if the last error is actually an
error generated by the last statement or not since "$?" always returns true
for "& { $ErrorActionPreference = "SilentlyContinue";
[Microsoft.Build.BuildEngine.BuildItem] }" since i am suppressing the error.


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
ASP namespace strong type access for dynamically loaded user control .NET General
Exchange 2007: checking if a custom proxy address type exists PowerShell
Display actual file type, not description of the type Vista file management
Display the actual type, rather than the description of the type Vista General
Unable to find type [Drawing.Image]: make sure that the assembly containing this type is loaded PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46