private static T CreateDelegate<T>(MethodInfo method)
        {
            return (T) Delegate.CreateDelegate(typeof(T), method);
        }无法将类型“System.Delegate”转换为“T”
运行不过去,这个怎么转换啊
        //
        // 摘要:
        //     创建指定类型的委托以表示指定的静态方法。
        //
        // 参数:
        //   type:
        //     要创建的委托的 System.Type。
        //
        //   method:
        //     描述委托要表示的静态或实例方法的 System.Reflection.MethodInfo。.NET Framework 1.0 和 1.1 版中仅支持静态方法。
        //
        // 返回结果:
        //     表示指定静态方法的指定类型的委托。
        //
        // 异常:
        //   System.ArgumentNullException:
        //     type 为 null。- 或 - method 为 null。
        //
        //   System.ArgumentException:
        //     type 不继承 System.MulticastDelegate。- 或 -type 不是 RuntimeType。请参见反射中的运行库类型。-
        //     或 - method 不是静态方法,并且 .NET Framework 的版本为 1.0 或 1.1。- 或 - 不能绑定 method。- 或
        //     -method 不是 RuntimeMethodInfo。请参见反射中的运行库类型。
        //
        //   System.MissingMethodException:
        //     未找到 type 的 Invoke 方法。
        //
        //   System.MethodAccessException:
        //     调用方无权访问 method。
        public static Delegate CreateDelegate(Type type, MethodInfo method);

解决方案 »

  1.   

    CreateDelegate这个到底是你自己写的,还是Delegate中的
      

  2.   

    不是Delegate中的哦  private static T CreateDelegate<T>(MethodInfo method)
      {
      return (T) Delegate.CreateDelegate(typeof(T), method);
      }
      

  3.   

    你没有加约束,T当然只能同object发生关系了:
    改成return (T)(object)Delegate.CreateDelegate(typeof(T), method);
      

  4.   


        public class ConfigurationUtils
        {
            private static SetConfigurationSystemHandler setConfigurationSystem = CreateDelegate<SetConfigurationSystemHandler>(typeof(ConfigurationManager).GetMethod("SetConfigurationSystem", BindingFlags.NonPublic | BindingFlags.Static));        public static Exception CreateConfigurationException()
            {
                return CreateConfigurationException(null, (Exception) null);
            }        public static Exception CreateConfigurationException(string message)
            {
                return CreateConfigurationException(message, (Exception) null);
            }        public static Exception CreateConfigurationException(string message, Exception inner)
            {
                return new ConfigurationErrorsException(message, inner);
            }        public static Exception CreateConfigurationException(string message, XmlNode node)
            {
                return CreateConfigurationException(message, null, node);
            }        public static Exception CreateConfigurationException(string message, Exception inner, XmlNode node)
            {
                return new ConfigurationErrorsException(message, inner, node);
            }        public static Exception CreateConfigurationException(string message, string fileName, int line)
            {
                return CreateConfigurationException(message, null, fileName, line);
            }        public static Exception CreateConfigurationException(string message, Exception inner, string fileName, int line)
            {
                return new ConfigurationErrorsException(message, inner, fileName, line);
            }        private static T CreateDelegate<T>(MethodInfo method)
            {
                return (T) Delegate.CreateDelegate(typeof(T), method);
            }        public static string GetFileName(XmlNode node)
            {
                if (node is ITextPosition)
                {
                    return ((ITextPosition) node).Filename;
                }
                return ConfigurationErrorsException.GetFilename(node);
            }        public static int GetLineNumber(XmlNode node)
            {
                if (node is ITextPosition)
                {
                    return ((ITextPosition) node).LineNumber;
                }
                return ConfigurationErrorsException.GetLineNumber(node);
            }        public static object GetSection(string sectionName)
            {
                return ConfigurationManager.GetSection(sectionName.TrimEnd(new char[] { '/' }));
            }        public static bool IsConfigurationException(Exception exception)
            {
                return (exception is ConfigurationErrorsException);
            }        public static void RefreshSection(string sectionName)
            {
                ConfigurationManager.RefreshSection(sectionName);
            }        public static IInternalConfigSystem SetConfigurationSystem(IInternalConfigSystem configSystem, bool enforce)
            {
                FieldInfo field = typeof(ConfigurationManager).GetField("s_configSystem", BindingFlags.NonPublic | BindingFlags.Static);
                IInternalConfigSystem innerConfigSystem = (IInternalConfigSystem) field.GetValue(null);
                if (configSystem is IChainableConfigSystem)
                {
                    ((IChainableConfigSystem) configSystem).SetInnerConfigurationSystem(innerConfigSystem);
                }
                try
                {
                    setConfigurationSystem(configSystem, true);
                }
                catch (InvalidOperationException)
                {
                    if (!enforce)
                    {
                        throw;
                    }
                    field.SetValue(null, configSystem);
                }
                return innerConfigSystem;
            }        private delegate void SetConfigurationSystemHandler(IInternalConfigSystem configSystem, bool setComplete);
        }
      

  5.   

      private static Delegate CreateDelegate<T>(MethodInfo method)
                {
                    return Delegate.CreateDelegate(typeof(T), method);
                }CreateDelegate<T>(...).DynamicInvoke(...)