如何在publickeytoken == null时使用C#从Dll获取公共方法

时间:2013-01-04 10:02:39

标签: c# dll assemblies publickeytoken

我希望在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);
        }
    }
}

如何解决这个问题

即使我尝试查找公共令牌密钥,它返回0字节

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

1 个答案:

答案 0 :(得分: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);
        }
    }
}