PowerShell – Check if .NET class exists

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.

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 :

or

Source:

PowerShell – Check if .NET class exists by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply