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

时间:2013-06-25 09:23:05

标签: c# asp.net

我指的是

Can I load a .NET assembly at runtime and instantiate a type knowing only the name?

尝试添加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();

以上代码有错误:

  

找不到类型或命名空间名称'ShellClass'(您是否缺少using指令或程序集引用?)

也有试用

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

错误:

  

无法找到类型或命名空间名称“ShellClass”

我真的不明白如何以编程方式添加引用。上面的代码有什么问题?

1 个答案:

答案 0 :(得分:0)

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

assembly.GetType("Shell32.ShellClass");