现在小弟做一个小程序 想获取DLL中的方法列表。但下面的程序在运行中出现了以下异常,思考许久不得其解。还望有好心大哥 帮帮忙这点一二。
出现异常:
未能加载文件或程序集“f:\\BHData.dll”或它的某一个依赖项。给定程序集名称或基本代码无效。 (异常来自 HRESULT:0x80131047)
代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;namespace OutPutDLLInformation
{
    class Program
    {
        static void Main(string[] args)
        {
            Assembly asm = Assembly.Load(@"f:\BHData.dll");
            Type myType = asm.GetType();
            // 获取公共方法
            MethodInfo[] myArrayMethodInfo = myType.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            Program p=new Program();
           Console.WriteLine( p.getMethodInfo(myArrayMethodInfo));
        }
        /// <summary>
        /// get Method informations from MethodInfo[] Array:
        /// </summary>
        /// <param name="myArrayMethodInfo"></param>
        /// <returns></returns>
        public string getMethodInfo(MethodInfo[] myArrayMethodInfo)
        {
            string methodStr = "";
            ///
            ///getinformation for all methods.
            for (int i = 0; i < myArrayMethodInfo.Length; i++)
            {
                MethodInfo myMethodInfo = (MethodInfo)myArrayMethodInfo[i];
                methodStr += "Method " + i + " :" + myMethodInfo.Name + Environment.NewLine;
            }
            return methodStr;
        }
        /// <summary>
        /// get properties information from PropertyInfo[] Array:
        /// </summary>
        /// <param name="myPropertyInfo"></param>
        /// <returns></returns>
        public string getPropertyInfo(PropertyInfo[] myPropertyInfo)
        {
            string propStr = "";
            // Display information for all properties.
            for (int i = 0; i < myPropertyInfo.Length; i++)
            {
                PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo[i];
                propStr += "property " + i + ":" + myPropInfo.Name + " type:" + myPropInfo.PropertyType + Environment.NewLine;
            }
            return propStr;
        }    }
}

解决方案 »

  1.   

    DLL是.NET的DLL还是非托管的DLL?
      

  2.   

    你应使用如下的方法读取一个托管的DLL文件:
    .NET Framework 类库  
    Assembly.LoadFrom 方法 (String)  
      

  3.   

    是小弟用C#自写的dll.
    hbxtlhx大哥 怎么解决
      

  4.   

    示例
    下面的示例通过给定程序集文件名或路径加载程序集。
    Assembly SampleAssembly;
    SampleAssembly = Assembly.LoadFrom("c:\\Sample.Assembly.dll");
    //获取一个已知的方法.
    MethodInfo Method = SampleAssembly.GetTypes()[0].GetMethod("Method1");
    // 获取这个方法的参数列表.
    ParameterInfo[] Params = Method.GetParameters();
    // 显示参数的信息
    foreach (ParameterInfo Param in Params)
    {
        Console.WriteLine("Param=" + Param.Name.ToString());
        Console.WriteLine("  Type=" + Param.ParameterType.ToString());
        Console.WriteLine("  Position=" + Param.Position.ToString());
        Console.WriteLine("  Optional=" + Param.IsOptional.ToString());
    }
      

  5.   

    Load里面的参数必须是一个程序集的名称~~~
      

  6.   

    你写的这个应该有问题吧
                Assembly asm = Assembly.Load(@"f:\BHData.dll");
                Type myType = asm.GetType();
    这边的BHData应该只是namespace吧,那样的话MyType根本得不到这个namespace里面具体的类
    所以Type MyTpe =asm.GetType(//加上个类名称吧);
    呵呵,错了不要骂我
      

  7.   

    hbxtlhx 大哥 我也是Assembly asm = Assembly.Load(@"f:\BHData.dll");但是报错 异常了 
    出现异常:
    未能加载文件或程序集“f:\\BHData.dll”或它的某一个依赖项。给定程序集名称或基本代码无效。 (异常来自 HRESULT:0x80131047)
      

  8.   

    因为你使用的是文件名,所以不能使用Load方法,而应该使用LoadFrom方法.
      

  9.   

    哎呀 hbxtlhx 大哥 感谢 解决了 真神啊 小弟太大意了。呵呵