简体   繁体   English

以编程方式添加shell32.dll作为参考

[英]programmatically add shell32.dll as reference

I refer to this 我指的是这个

Can I load a .NET assembly at runtime and instantiate a type knowing only the name? 我可以在运行时加载.NET程序集并实例化仅知道名称的类型吗?

trying to add shell32.dll as reference, here is the code 试图添加shell32.dll作为参考,这是代码

    string assemblyName = "Interop.Shell32.dll";
    string assemblyPath = HttpContext.Current.Server.MapPath(assemblyName);
    Assembly assembly = Assembly.LoadFrom(assemblyPath);
    Type T = assembly.GetType("ShellClass");
    ShellClass instance = (ShellClass)Activator.CreateInstance(T);
    ShellClass sh = new ShellClass();

The above code has errors: 上面的代码有错误:

The type or namespace name 'ShellClass' could not be found (are you missing a using directive or an assembly reference?) 找不到类型或名称空间名称“ ShellClass”(是否缺少using指令或程序集引用?)

Also have a trial 也有试用

Assembly assembly = Assembly.LoadFrom("Interop.Shell32.dll");
Type type = assembly.GetType("ShellClass");
object ShellClass = Activator.CreateInstance(type);

Error: 错误:

The type or namespace name 'ShellClass' could not be found 找不到类型或名称空间名称“ ShellClass”

I'm not really understand how to add reference programmatically. 我不太了解如何以编程方式添加参考。 What is the problem of above code? 上面的代码有什么问题?

您应该在GetType方法中包括该类的名称空间:

assembly.GetType("Shell32.ShellClass");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM