如何从C#中的DLL获取类型列表?

Jos*_*ons 4 c# reflection assemblies

我想将一个C#应用程序指向DLL并获取该DLL中定义的类型列表.到目前为止我所看到的表面看起来是正确的,但是给出了下面指出的错误.

using System.Reflection;

...

static void Main(string[] args)
{
    Assembly SampleAssembly;
    SampleAssembly = Assembly.LoadFrom("C:\\MyAssembly.dll"); //error happens here

    foreach (Type tp in SampleAssembly.GetTypes())
    {
        Console.WriteLine(tp.Name);
    }
}

/*
This will give me:
Unable to load one or more of the requested types.
Retrieve the LoaderExceptions property for more information.

I wish it would give me something like this:
MyClass1
MyClass2
MyClass3
*/
Run Code Online (Sandbox Code Playgroud)

小智 5

使用ReflectionOnlyLoad而不是直接加载来阻止运行时尝试运行目标程序集中的任何代码