习惯型组件pathes组件、习惯、pathes

2023-09-02 11:54:33 作者:时差情书’

我需要一个方法,需要一个类型,并返回在类型中使用的所有组件的pathes。 我写这样的:

I need a method that takes a Type and returns the pathes of all assemblies that used in the type. I wrote this:

public static IEnumerable<string> GetReferencesAssembliesPaths(this Type type)
{   
 yield return type.Assembly.Location;

 foreach (AssemblyName assemblyName in type.Assembly.GetReferencedAssemblies())
 {
  yield return Assembly.Load(assemblyName).Location;
 }
}

通常,这种方法做的工作,但有一些缺点:

Generally this method do the job, but have some disadvantages:

我没有找到如何让引用的程序集/从类型本身的类型,所以我用type.Assembly.GetReferencedAssemblies(),并得到了整个总成的相关的参考资料,不只是那些类型。

I didn't found how to get the referenced assemblies/types from the type itself, so i used type.Assembly.GetReferencedAssemblies() and got the references of the whole assembly, not just those that related to the type.

type.Assembly.GetReferencedAssemblies()返回的AssemblyName,没有位置/路径/文件路径属性。要获得位置属性,我第一次使用的Assembly.Load(),然后使用的位置属性。我不想加载组件,以获得他们的路径,因为他们没有必要使用,因为的Assembly.Load()可以用FileNotFoundException异常或BadImageFormatException失败。

type.Assembly.GetReferencedAssemblies() returns AssemblyName and has no location/path/filepath property. To get the location property, i first used Assembly.Load() and then used the location property. I dont want load assemblies to get their path, because they not necessary used, and because Assembly.Load() can fail with FileNotFoundException or BadImageFormatException.

有什么建议?

推荐答案

我想我被它替换到Assembly.ReflectionOnlyLoad()。

I think i solved the Assembly.Load() problem by replacing it to Assembly.ReflectionOnlyLoad().

现在这是我的方法是这样的:

now this is how my method looks like:

public static IEnumerable<string> GetReferencesAssembliesPaths(this Type type)
{           
    yield return type.Assembly.Location;

    foreach (AssemblyName assemblyName in type.Assembly.GetReferencedAssemblies())
    {
        yield return Assembly.ReflectionOnlyLoad(assemblyName.FullName).Location;
    }
}

现在唯一留下的问题是type.Assembly.GetReferencedAssemblies(),我如何得到引用的程序集的类型,而不是从组件?

now the only left problem is the type.Assembly.GetReferencedAssemblies(), how do i get referenced assemblies from the type rather than from the assembly?