![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
| | #6 (permalink) |
| | 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 Specs![]() |
| | #7 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||