开发者

Cast object at runtime

开发者 https://www.devze.com 2023-04-09 18:26 出处:网络
I want to perform the following line of code using reflection. IWshRuntimeLibrary.IWshShortcut desktopShortCut = (IWshRuntimeLibrary.IWshShortcut)WshShell.CreateShortcut(Environment.SpecialFolder.Des

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号