无法将类型“System.Delegate”转换为“T”

zwb521 2011-12-06 04:18:07

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);
...全文
232 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
黄亮 2011-12-06
  • 打赏
  • 举报
回复
  private static Delegate CreateDelegate<T>(MethodInfo method)
{
return Delegate.CreateDelegate(typeof(T), method);
}

CreateDelegate<T>(...).DynamicInvoke(...)
zwb521 2011-12-06
  • 打赏
  • 举报
回复
哦,这样,灰常感谢:)
zwb521 2011-12-06
  • 打赏
  • 举报
回复

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);
}
dalmeeme 2011-12-06
  • 打赏
  • 举报
回复
你没有加约束,T当然只能同object发生关系了:
改成return (T)(object)Delegate.CreateDelegate(typeof(T), method);
zwb521 2011-12-06
  • 打赏
  • 举报
回复
不是Delegate中的哦



private static T CreateDelegate<T>(MethodInfo method)
{
return (T) Delegate.CreateDelegate(typeof(T), method);
}
bdmh 2011-12-06
  • 打赏
  • 举报
回复
CreateDelegate这个到底是你自己写的,还是Delegate中的

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧