Could someone开发者_开发问答 please explain to me what the following lines of code do?
dynamic shellApplication = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));
string path = System.IO.Path.GetDirectoryName(filePath);
string fileName = System.IO.Path.GetFileName(filePath);
dynamic directory = shellApplication.NameSpace(path);
dynamic link = directory.ParseName(fileName);
dynamic verbs = link.Verbs();
I've searched the msdn library, but couldn't really understand what it did.
This isn't the full code, but I undertand the rest, it is just this part that I'm struggling with.
Looks like it is retrieving the shell actions that a particular program is associated with. For example Open, Print, Edit, etc.
Open regedit and navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Classes\textfile
Expand it out and look at the Shell key. The code should be returning verbs similar to that.
This creates "Shell.Application"
COM object and then uses dynamic
to call methods on it.
It gets all the verbs that can be called on a file.
This is basically scripting
. See here and here for a sample.
To expand on Aliostad's answer, the dynamic
keyword in C# allows you to call members and methods on an unknown type. This means using a dynamic
variable you won't get intellisense since the compiler has no clue what members or methods the variable actually has. This is all figured out at runtime.
Here is a good explanation.
精彩评论