在运行时投射对象

发布于 2024-12-07 09:08:51 字数 1161 浏览 3 评论 0原文

我想使用反射执行以下代码行。

IWshRuntimeLibrary.IWshShortcut desktopShortCut = (IWshRuntimeLibrary.IWshShortcut)WshShell.CreateShortcut(Environment.SpecialFolder.Desktop.ToString()+"\\Max Y+Y.lnk");

我已经成功地得到了表达的正确部分。

WshShell.CreateShortcut(....)

通过使用

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,我想将其转换为类型 IWshRuntimeLibrary.IWshShortcut 结果的对象,并将其分配给上面的情况。

IWshRuntimeLibrary.IWshShortcut desktopShortCut,

这怎么可能?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哭泣的笑容 2024-12-14 09:08:51

如果 WshShellClass.CreateShortcut 返回一个 IWshRuntimeLibrary.IWshShortcut 那么你可以说

IWshRuntimeLibrary.IWshShortcut desktopShortCut = (IWshRuntimeLibrary.IWshShortcut) result

我错过了什么吗?

If WshShellClass.CreateShortcut returns a IWshRuntimeLibrary.IWshShortcut then you could just say

IWshRuntimeLibrary.IWshShortcut desktopShortCut = (IWshRuntimeLibrary.IWshShortcut) result

Am I missing something?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文