Question:
I have a DLL file that I am importing into the PS session. This creates a new .NET class. At the start of the function I want to test if this class exists, and if it doesn’t import the DLL file.
Currently I am trying to call the class. This works but I think it’s causing problems with the Do {} Until () loop since I have to run the script twice.
my code. note the Do {} Until ()
loop isn’t working.
https://gist.github.com/TheRealNoob/f07e0d981a3e079db13d16fe00116a9a
I have found the [System.Type]::GetType()
Method but when I run it against any kind of string, valid or invalid class, it doesn’t do anything.
Answer:
In .Net it exists something called Reflexion which allow you deal with everything in your code.
1 2 |
$type = [System.AppDomain]::CurrentDomain.GetAssemblies() | % { $_.GetTypes() | where {$_.Name -eq 'String'}} |
Here I look for the type String
, but you can look for your type or better the version of the Assembly, you can even find if one method exists with the correct arguments. Have a look to C# – How to check if namespace, class or method exists in C#?.
@Timmerman comments ; he went with :
1 2 |
[System.AppDomain]::CurrentDomain.GetAssemblies() | % { $_.GetTypes() | where {$_.Name -like "SQLiteConnection"}} |
or
1 2 |
[System.AppDomain]::CurrentDomain.GetAssemblies() | % { $_.GetTypes() | where {$_.AssemblyQualifiedName -like 'Assembly Qualified Name'}} |