/// <summary>
        /// 根据类名创建代理,过程如下:
        /// 利用反射机制,创建ClassName的对象Obj
        /// Obj的构造函数需要类成员对象m_addin.application作为参数传入
        /// Obj的Exectution方法作为代理返回
        /// </summary>
        /// <param name="ClassName"></param>
        /// <returns></returns>
        private _CommandBarButtonEvents_ClickEventHandler createAction(String ClassName)
        {
            Assembly ass = System.Reflection.Assembly.GetEntryAssembly();
            Type type = ass.GetType(ClassName);
            Object obj = ass.CreateInstance(ClassName);
            MethodInfo method = type.GetMethod("Exectution");
            return Delegate.CreateDelegate(type, method);
        }

解决方案 »

  1.   

    需要强制类型转换为_CommandBarButtonEvents_ClickEventHandler 类型。
      

  2.   

    我在以下地方出现调试的错误,无法通过。        /// <summary>
            /// 根据类名创建代理,过程如下:
            /// 利用反射机制,创建ClassName的对象Obj
            /// Obj的构造函数需要类成员对象m_addin.application作为参数传入
            /// Obj的Exectution方法作为代理返回
            /// </summary>
            /// <param name="ClassName"></param>
            /// <returns></returns>
            private _CommandBarButtonEvents_ClickEventHandler createAction(String ClassName)
            {
                Type type = Type.GetType("DCES.Actions.IOChannelValid");
            }
        namespace DCES.Actions
        {
            class IOChannelValid
            {
            }
        }
      

  3.   

    已经搞定了。代码如下:        /// <summary>        /// 根据类名创建代理,过程如下:        /// 利用反射机制,创建ClassName的对象Obj        /// Obj的构造函数需要类成员对象m_addin.application作为参数传入        /// Obj的MethodName方法作为代理返回        /// </summary>        /// <param name="ClassName"></param>        /// <returns></returns>        private Delegate  createAction(String ClassName, String MethodName, Type DelegateType)        {            Type type = Type.GetType(ClassName);            Type[] constructTypes = { typeof(Microsoft.Office.Interop.Excel.Application)};                         ConstructorInfo constructor = type.GetConstructor(constructTypes);            Object[] parameters = { m_AddIn.Application  };            Object obj = constructor.Invoke(parameters);            MethodInfo method = type.GetMethod(MethodName);            Delegate dl = Delegate.CreateDelegate(DelegateType, obj, "Action_For_Menu", true);            return dl;        }