- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
感谢 Jon Skeet 在 this 中的回答问题我有以下工作:
public delegate BaseItem GetItemDelegate(Guid itemID);
public static class Lists
{
public static GetItemDelegate GetItemDelegateForType(Type derivedType)
{
MethodInfo method = typeof(Lists).GetMethod("GetItem");
method = method.MakeGenericMethod(new Type[] { derivedType });
return (GetItemDelegate)Delegate.CreateDelegate(typeof(GetItemDelegate), method);
}
public static T GetItem<T>(Guid itemID) where T : class { // returns an item of type T ... }
}
public class DerivedItem : BaseItem { }
// I can call it like so:
GetItemDelegate getItem = Lists.GetItemDelegateForType(typeof(DerivedItem));
DerivedItem myItem = getItem(someID); // this works great
当我尝试将相同的东西应用到具有不同返回类型和重载的方法(这是我能想到的唯一区别)时,我得到一个烦人的“ArgumentException:绑定(bind)到目标方法时出错。”正在调用 CreateDelegate
.下面是一个出现错误的工作示例,只需将其复制/粘贴到控制台应用程序即可。
public delegate IEnumerable<BaseItem> GetListDelegate();
public class BaseItem { }
public class DerivedItem : BaseItem { }
public static class Lists
{
public static GetListDelegate GetListDelegateForType(Type itemType)
{
MethodInfo method = typeof(Lists).GetMethod("GetList", Type.EmptyTypes); // get the overload with no parameters
method = method.MakeGenericMethod(new Type[] { itemType });
return (GetListDelegate)Delegate.CreateDelegate(typeof(GetListDelegate), method);
}
// this is the one I want a delegate to, hence the Type.EmptyTypes above
public static IEnumerable<T> GetList<T>() where T : class { return new List<T>(0); }
// not the one I want a delegate to; included for illustration
public static IEnumerable<T> GetList<T>(int param) where T : class { return new List<T>(0); }
public static Type GetItemType()
{ // this could return any type derived from BaseItem
return typeof(DerivedItem);
}
}
class Program
{
static void Main(string[] args)
{
Type itemType = Lists.GetItemType();
GetListDelegate getList = Lists.GetListDelegateForType(itemType);
IEnumerable<BaseItem> myList = (IEnumerable<BaseItem>)getList();
}
}
如上所述,我能看到的唯一区别是:
T
有效,IEnumerable<T>
无效)[编辑:这是不对的,第一个版本使用 BaseItem
, 不是 T
;哎呀]GetItem
没有重载,GetList
有几个;我只需要没有参数的委托(delegate)给 GetList()
更新 1:Sam 帮助我查明了一些问题。如果委托(delegate)的返回类型是通用的(例如 IEnumerable<BaseItem>
),当我尝试交换基本类型/派生类型时它会令人窒息。有什么办法可以申报我的 GetList
方法如下?我需要能够指出 T
继承自 BaseItem
,但如果我可以的话,它对我来说会很好。
public static IEnumerable<BaseItem> GetList<T>() where T : class
另一种选择是“通用化”我的委托(delegate)声明。我能找到的所有示例都对参数使用泛型,而不是返回类型。我该怎么做(它会抛出一个编译器错误,因为 T
未定义,而且它不会让我使用 where
约束):
public delegate IEnumerable<T> GetListDelegate();
最佳答案
我通过将委托(delegate)声明为 IEnumerable
来实现这一点。这允许它创建委托(delegate)。剩下的就是基本的类型转换。以下更改修复了上面的第二个代码块。
// declare this as non-generic
public delegate IEnumerable GetListDelegate();
和
// do some cast-fu to get the list into a workable form
List<BaseItem> myList = getList().Cast<BaseItem>().ToList();
然后我可以执行 myList.Sort()
以及我在工作中尝试在我的系统中执行的所有其他操作。
关于c# - MethodInfo、CreateDelegate 和通用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2062084/
假设我有这样一个类: public class MyClass { public void Foo(T t) { } } 现在,假设我有一个 MyClass 的实例和一个 Me
我有一个类类型的方法的MethodInfo,它是该类实现的接口(interface)定义的一部分。 如何检索类实现的接口(interface)类型上方法的匹配 MethodInfo 对象? 最佳答案
我的问题是:如果我有一个方法的 MethodInfo 对象,它是从一个接口(interface)类型获得的,我还有一个实现这个接口(interface)的类的 Type 对象,但是它用一个显式实现,如
获取 MethodInfo 对于 Enumerable.OfType() 我们可以使用: typeof(System.Linq.Enumerable).GetMethod("OfType", new
我正在开发一个程序,该程序根据一组输入在运行时计算表达式,然后远程执行这些表达式。这需要动态创建调用不同辅助函数的表达式。 对于静态辅助函数,可以通过使用以下方式获取 MethodInfo 实例来保证
我正在开发一个程序,该程序根据一组输入在运行时计算表达式,然后远程执行这些表达式。这需要动态创建调用不同辅助函数的表达式。 对于静态辅助函数,可以通过使用以下方式获取 MethodInfo 实例来保证
我已经为日志库创建了包装器,我想在其中记录抛出异常的外部方法的参数: public void MethodA(string param1, int param2) { try { th
有没有一种方法可以调用方法并且返回类型是强类型的? 有代码示例 public static IQueryable FilterVersion(this Table t, IVersionIndexF
我想知道 MethodInfo 是否适合用作字典键。从我非常有限的测试来看,一切似乎都很好,但是这里有什么问题吗? 有人做过吗? 最佳答案 MethodInfo 是一个抽象类,因此您的问题不能直接回答
我正在尝试使用反射调用方法。 像这样: method.Invoke(instance, propValues.ToArray()) 问题是没有办法确保参数数组的顺序正确。有没有办法通过名称指定哪些值适
类似于我之前的一个问题,当我询问获取字段的 FieldInfo 时,How to get the FieldInfo of a field from the value ,根据那里的答案,我编译了这个
我有以下方法: public TResult Call(Expression> code) { var returnValue = default(TResult); // code
我正在尝试获取 MethodInfo方法的对象: Any(IEnumerable, Func) 我遇到的问题是如何为 Func 指定类型参数有点…… MethodInfo method = typeo
我有一个定义如下的类 interface ITest { List Find(int i); } class Test: ITest { public T List Fin
我想从具有仅在运行时已知的类型参数的泛型类中获取方法的 MethodInfo。 下面是我如何从非泛型类中获取泛型方法的 MethodInfo: class MyClass { public v
我有一个 IEnumerable , 我想调用 Enumerable.Contains 反射法。我只是在努力使语法正确。这是我目前拥有的: var containsMethod = typeof(En
构造函数如下所示: public NameAndValue(string name, string value) 我需要使用反射将其作为 MethodInfo 获取。它尝试了以下操作,但没有找到构造函
我无法像我怀疑的那样获取扩展方法的方法信息。怎么了? _toStringMethod = typeof(ObjectExtensions).GetMethod("TryToString", B
这个问题在这里已经有了答案: get methodinfo from a method reference C# (7 个答案) 关闭 9 年前。 是否可以从方法符号中获取 MethodInfo 对
我的这个类型包含一个泛型方法的两个重载。我喜欢使用反射检索其中一个重载(使用 Func 参数)。然而问题是我找不到正确的参数类型来提供 Type.GetMethod(string, Type[])方法
我是一名优秀的程序员,十分优秀!