【问题标题】:How to get public methods from Dll using C# when publickeytoken == null当publickeytoken == null时如何使用C#从Dll获取公共方法
【发布时间】:2013-01-04 10:02:38
【问题描述】:

我想在 publickeytoken == null 时从 dll 中找到公共用户定义函数

OpenFileDialog obj = new OpenFileDialog();
if (obj.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    Assembly ass = Assembly.LoadFrom(obj.FileName);

    // Here its getting Exception because Publickeytoken == null;
    Assembly a = Assembly.LoadWithPartialName(ass.FullName);

    Type[] types = a.GetTypes();
    foreach (Type type in types)
    {
        if (!type.IsPublic)
        {
            continue;
        }

        MemberInfo[] members = type.GetMembers(BindingFlags.Public
                                                | BindingFlags.Instance
                                                | BindingFlags.InvokeMethod);
        foreach (MemberInfo member in members)
        {
            Console.WriteLine(type.Name + "." + member.Name);
        }
    }
}

如何解决这个问题

即使我试过这个来查找它会重新运行的 publictokenkey 0 字节

           byte[] b = Assembly.GetExecutingAssembly().GetName().GetPublicKeyToken();

【问题讨论】:

  • 你的例外是什么?此外,LoadWithPartialName 已过时,您应该改用 Load。
  • 为什么不直接使用汇编对象“ass”呢?因为你刚刚从 dll 加载它?
  • 好的,告诉我一个从 Dll 中查找公共方法的解决方案

标签: c# dll assemblies publickeytoken


【解决方案1】:

您的绑定标志错误,您可以直接使用ass

OpenFileDialog obj = new OpenFileDialog();
if (obj.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    Assembly ass = Assembly.LoadFrom(obj.FileName);
    foreach(var type in ass.GetTypes())
    {
        MethodInfo[] members = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);

        foreach (MemberInfo member in members)
        {
            Console.WriteLine(type.Name + "." + member.Name);
        }
    }
}

【讨论】:

  • 对于某些 Dlls 它的工作...有些它不工作?我能做什么
  • 我收到此消息 请参阅此消息的末尾以了解有关调用即时 (JIT) 调试而不是此对话框的详细信息。
  • ************** 异常文本 ************** System.Reflection.ReflectionTypeLoadException:无法加载一个或多个请求的类型。检索 LoaderExceptions 属性以获取更多信息。在 System.Reflection.RuntimeModule.GetTypes(RuntimeModule 模块) 在 System.Reflection.RuntimeModule.GetTypes() 在 System.Reflection.Assembly.GetTypes()
  • 用 try catch(foreach(var type...) 语句)包围它,查看 loaderExceptions 以获取更多信息
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-19
  • 2016-05-09
  • 2022-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-22
相关资源
最近更新 更多