Cast object at runtime
I want to perform the following line of code using reflection.
IWshRuntimeLibrary.IWshShortcut desktopShortCut = (IWshRuntimeLibrary.IWshShortcut)WshShell.CreateShortcut(Environment.SpecialFolder.Desktop.ToString()+"\\Max Y+Y.lnk");
I have successfully get the right part of expression.
WshShell.CreateShortcut(....)
By using
this.assembly = Assembly.LoadFrom(Environment.CurrentDirectory + "\\Interop.IWshRuntimeLibrary.dll");
AppDomain.CurrentDomain.Load(assembly.GetName());
this.WshShellClass = 开发者_如何学运维assembly.GetType("IWshRuntimeLibrary.WshShellClass");
object classInstance = Activator.CreateInstance(this.WshShellClass, null);
object[] parameters = new object[1];
parameters[0] = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\Max Y+Y.lnk";
MethodInfo methodInfo = this.WshShellClass.GetMethod("CreateShortcut");
object result = methodInfo.Invoke(classInstance, parameters);
Now I want to cast it to object of Type IWshRuntimeLibrary.IWshShortcut
result in above case and assign it to.
IWshRuntimeLibrary.IWshShortcut desktopShortCut,
How is this possible?
If WshShellClass.CreateShortcut
returns a IWshRuntimeLibrary.IWshShortcut
then you could just say
IWshRuntimeLibrary.IWshShortcut desktopShortCut = (IWshRuntimeLibrary.IWshShortcut) result
Am I missing something?
精彩评论