当在编译时未知类型参数但是在运行时动态获取时,调用泛型方法的最佳方法是什么?
考虑以下示例代码 - 在Example()方法内部,GenericMethod<T>()使用Type存储在myType变量中调用的最简洁方法是什么?
public class Sample
{
public void Example(string typeName)
{
Type myType = FindType(typeName);
// What goes here to call GenericMethod<T>()?
GenericMethod<myType>(); // This doesn't work
// What changes to call StaticMethod<T>()?
Sample.StaticMethod<myType>(); // This also doesn't work
}
public void GenericMethod<T>()
{
// ...
}
public static void StaticMethod<T>()
{
//...
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试为Enumerable类型的Where方法检索MethodInfo:
typeof (Enumerable).GetMethod("Where", new Type[] {
typeof(IEnumerable<>),
typeof(Func<,>)
})
Run Code Online (Sandbox Code Playgroud)
但得到null.我究竟做错了什么?
它们可以如下使用:
FieldInfo field = fieldof(string.Empty);
MethodInfo method1 = methodof(int.ToString);
MethodInfo method2 = methodof(int.ToString(IFormatProvider));
Run Code Online (Sandbox Code Playgroud)
fieldof 可以编译为IL为:
ldtoken <field>
call FieldInfo.GetFieldFromHandle
Run Code Online (Sandbox Code Playgroud)
methodof 可以编译为IL为:
ldtoken <method>
call MethodBase.GetMethodFromHandle
Run Code Online (Sandbox Code Playgroud)
无论何时使用typeof运算符,您都可以获得完美的查找所有引用结果.不幸的是,一旦你去了田野或方法,你最终会遇到令人讨厌的黑客攻击.我想你可以做以下事情......或者你可以回去按名字命名.
public static FieldInfo fieldof<T>(Expression<Func<T>> expression)
{
MemberExpression body = (MemberExpression)expression.Body;
return (FieldInfo)body.Member;
}
public static MethodInfo methodof<T>(Expression<Func<T>> expression)
{
MethodCallExpression body = (MethodCallExpression)expression.Body;
return body.Method;
}
public static MethodInfo methodof(Expression<Action> expression)
{
MethodCallExpression body = (MethodCallExpression)expression.Body;
return body.Method;
}
public static void Test()
{
FieldInfo field = fieldof(() => string.Empty);
MethodInfo method1 …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个表示以下内容的表达式树:
myObject.childObjectCollection.Any(i => i.Name == "name");
Run Code Online (Sandbox Code Playgroud)
为清楚起见,我有以下内容:
//'myObject.childObjectCollection' is represented here by 'propertyExp'
//'i => i.Name == "name"' is represented here by 'predicateExp'
//but I am struggling with the Any() method reference - if I make the parent method
//non-generic Expression.Call() fails but, as per below, if i use <T> the
//MethodInfo object is always null - I can't get a reference to it
private static MethodCallExpression GetAnyExpression<T>(MemberExpression propertyExp, Expression predicateExp)
{
MethodInfo method = typeof(Enumerable).GetMethod("Any", new[]{ typeof(Func<IEnumerable<T>, Boolean>)});
return …Run Code Online (Sandbox Code Playgroud) 目前我不得不做这样的事情来在运行时构建一个Type定义传递给我的IOC来解决.简化:
Type t = Type.GetType(
"System.Collections.Generic.List`1[[ConsoleApplication2.Program+Person");
Run Code Online (Sandbox Code Playgroud)
我只知道运行时的泛型类型参数.
有什么东西可以让我做这样的事情(假代码):
Type t = Type.GetTypeWithGenericTypeArguments(
typeof(List)
, passInType.GetType());
Run Code Online (Sandbox Code Playgroud)
或者我只是坚持我的黑客,passInType.GetType()转换为字符串,构建泛型字符串..感觉很脏
要在Moq中模拟受保护的虚拟(非泛型)方法很简单:
public class MyClass
{
....
protected virtual int MyMethod(Data data){..}
}
Run Code Online (Sandbox Code Playgroud)
并嘲笑它:
myMock.Protected().Setup<int>("MyMethod", ItExpr.Is<Data>( ...
Run Code Online (Sandbox Code Playgroud)
如果受保护的方法是通用的,我无法找到使用相同技术的方法,例如:
protected virtual int MyMethod<T>(T data)
Run Code Online (Sandbox Code Playgroud)
除了使用包装类来覆盖该方法之外,任何想法都是如此,我们非常感激.
我正在编写一个列表排序扩展方法.我的输入是列表和带有属性名称和排序方向的字符串.这个字符串可以有多个属性,如:"Name ASC,Date DESC"等.
我已经实现了字符串解析并使用了反射来从字符串中获取属性本身,但我现在所困扰的是如何动态链接orderby方法.
像:
_list.orderBy(x=>x.prop1).thenBy(x=>x.prop2)等等
有没有办法动态构建它?
这可能听起来愚蠢的,但我不能得到MethodInfo的Queryable.Join(...).我想得到它,因为如何在泛型方法调用中使用Type变量(C#)
它有2个可用的方法签名和我想没有的IEqualityComparer的一个,所以我需要指定Type[]在GetMethod.
我写了类似的东西
MethodInfo joinMethod = typeof( Queryable ).GetMethod( "Join", new Type[] { typeof(IEnumerable<>), typeof(Expression<Func<>>), typeof(Expression<Func<>>), typeof(Expression<Func<>>)});
Run Code Online (Sandbox Code Playgroud)
但它不起作用.我无法在上面指定泛型中的类型,因为它们是Type从外部传递的(这就是我需要这种反射的原因).
谁能告诉我怎么样?谢谢!
如何获得MethodInfo的Array.IndexOf<string>(string[], string)?
我尝试使用此代码,但不起作用.
typeof(Array).GetMethod("IndexOf",
BindingFlags.Public | BindingFlags.Static, null,
new Type[] { typeof(string[]), typeof(string) }, null);
Run Code Online (Sandbox Code Playgroud)