一个.dll文件内容:
namespace PubFunction
{
  public class PubFunction
  {
    //****************程序退出*********************
    public static void ExitSys()
   {
     Application.Exit();
   }
  }
}

现在我的c#程序test.cs中添加引用了这个.dll之后,
PubFunction.PubFunction a = new PubFunction.PubFunction();
Type type = a.GetType();
MethodInfo method = type.GetMethod( "ExitSys");
method.Invoke(a , null);
报参数计数不匹配.而把
public static void ExitSys()
   {
     Application.Exit();
   }
放到程序test.cs的类中则没有错误,参数请问到底是怎么回事呀?

解决方案 »

  1.   

    //动态调用dll,c#-demo
    //using System.Reflection;
    //生成一个MyDll的项目,里面包含一个Form1,编译为dll
    //mydll.dll放到当前目录下
    Assembly asm = AppDomain.CurrentDomain.Load("MyDll");
    Type tp = asm.GetType("MyDll.Form1");
    object o = Activator.CreateInstance(tp);
    MethodInfo mi = tp.GetMethod("Show");
    mi.Invoke(o,null);
      

  2.   

    但我的mydll已经在"项目"->"添加引用"->"项目" 中引用了。而且执行时报的是“参数计数不匹配”,如果调用的是随便一个abcd不存在的方法,则报的是"未将对象引用设置到对象的实例",说明程序还是找到了ExitSys了是吗?
    我刚刚学c#呀,很多基本的东西还处于模糊状态,特别c#跟以前语言有些本质上的概念区别,好难啊。
      

  3.   

    ok,现在我
    Assembly assem = Assembly.LoadFrom("f:\\PubLib.dll");
    Type type = assem.GetType("PubFunction.PubFunction");
    MethodInfo method = type.GetMethod( "ExitSys");
    Object obj = Activator.CreateInstance(type); 
    method.Invoke(obj,null);PubLib.dll中ExitSys是这样的,没有任务参数。
    public static void ExitSys()
    {
      Application.Exit();
    }但执行时还是报:参数计数不匹配出错行是:method.Invoke(obj,null);救命啊!!!!我搞了整整2天了都。  :(
      

  4.   

    终于发现问题了。
    原因在于:Application.Exit();
    如果ExitSys是:
    public static void ExitSys()
    {
      MessageBox.Show("hi");
    }
    可以正常的运行。当初建立PubLib.dll时,项目类型是windows控件库,那么运行Application.Exit()时,可能会找不到exit哪个form吧。
    那么如果一定要用.dll的话,这个问题如何解决呀?
      

  5.   

    我用Application.Exit();也没问题啊
      

  6.   

    jinjazz大哥,能否把源码和环境贴一下呀。还有.dll创建的项目类型是什么?windows控件库还是windows应用程序呀?
    另外一个新的问题:
    如果我的ExitSys()要带参数那该怎么办呀??
      

  7.   

    System.Drawing.Image img_gif = Image.FromFile("f:\\girl.gif");System.Drawing.Imaging.FrameDimension dimension = new System.Drawing.Imaging.FrameDimension(img_gif.FrameDimensionsList[0]);
    int iCount = img_gif.GetFrameCount(dimension);
    int iFrameIndex = 3;//你想看的某祯画面
    img_gif.SelectActiveFrame(dimension,iFrameIndex );
    img_gif.Save("f:\\aa1.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);System.Drawing.Image img = new Bitmap("f:\\aa1.jpg");
    System.Drawing.Graphics gp = Graphics.FromImage(img);Font myFont = new Font("黑体",16,FontStyle.Bold);
    SolidBrush mySolidBrush = new SolidBrush(Color.Red);
    PointF myPointF = new PointF(50,160);
    gp.SmoothingMode = SmoothingMode.AntiAlias;
    gp.DrawString("MY IMAGE",myFont,mySolidBrush,myPointF);//img.Save(Response.OutputStream,ImageFormat.Gif);img.Save("f:\\girl_pic.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);img_gif.FrameDimensionsList
    gp.Dispose();
    img.Dispose();
    ----------------------------------------------------------------------
    img_gif.FrameDimensionsList 这里代码不全
      

  8.   

    D:\wwwroot\Photo\WebForm1.aspx.cs(77): 只有 assignment、call、increment、decrement 和 new 对象表达式可用作语句
      

  9.   

    在我机子上是可以的阿
    //1.cs
    using System;
    using System.Windows.Forms;
    namespace PubFunction
    {
      public class PubFunction
      {
        public static void ExitSys()
       {
         Application.Exit();
       }
      }
    }//test.cs
    using System;
    using System.Reflection;namespace test
    {
    /// <summary>
    /// e 的摘要说明。
    /// </summary>
    public class e
    {
    public static void Main(string[] args)
    {
    Assembly assem = Assembly.LoadFrom("1.dll");
    Type type = assem.GetType("PubFunction.PubFunction");
    MethodInfo method = type.GetMethod( "ExitSys");
    Object obj = Activator.CreateInstance(type);
    Console.ReadLine();
    method.Invoke(obj,null);
    }
    }
    }
      

  10.   

    请问1.cs是什么项目类型的?
    我用记事本写的1.cs用csc 1.cs编译不能通过呀。????