我有一个关于MethodInfo.Invoke(o,null)的try/catch,并且VS2010被设置为永远不会在Exceptions上中断,但不幸的是调试器继续在Invoked方法内部中断.该方法是静态的,我已经安装了Phone Developer Beta.
这是错误还是开发人员错误?
谢谢!!
我有MethodInfo
一个关于类类型的方法,该方法是该类实现的接口定义的一部分。
如何MethodInfo
在类实现的接口类型上检索方法的匹配对象?
我是DRY编码的忠实粉丝,我喜欢尽可能避免使用锅炉板代码.因此,我将所有WCF频道faff重构为AOP类,该类处理WCF频道的生命周期.
我也是async-await的忠实粉丝,特别是对于WCF,因为它理论上可以释放一个通常在睡眠中等待响应的线程.
所以我在fluentAOP lib中创建了一个拦截器
private static object InvokeOnChannel(IMethodInvocation methodInvocation)
{
var proxy = _factory.CreateChannel();
var channel = (IChannel) proxy;
try
{
channel.Open();
var ret = methodInvocation.Method.Invoke(proxy, methodInvocation.Arguments);
channel.Close();
return ret;
}
catch (FaultException ex)
{
if (ex.InnerException != null)
throw ex.InnerException;
throw;
}
catch(Exception)
{
channel.Abort();
throw;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,在考虑解决方案时,我注意到在表格的WCF合同的情况下
[ServiceContract]
public interface IFoo
{
[OperationContract]
Task<int> GetInt();
}
Run Code Online (Sandbox Code Playgroud)
GetInt会有意想不到的结果.首先,catchException将无效.其次,我会在请求返回之前关闭通道.如果返回类型是Task,我理论上可以切换到另一个代码路径.但我无法弄清楚如何等待任务<>的结果然后返回等待.
这当然是特别困难的,因为运行时AOP我无法访问能够使用返回类型的泛型(没有整个反射).
任何想法如何实现这个功能作为一个等待,它关闭完整的通道和捕获/编组调用线程的异常?
您好我如何使用带有线程的参数调用System.Reflection.MethodInfo.Invoke().
例如..
假设我有一个方法允许你传入一个表示类名的字符串并动态调用相应的类方法,现在我想用线程调用这个Methodinfo.invoke,我不知道怎么做,因为我调用了invoke与paramter.代码片段给出了meblow.谢谢您的帮助
Type classType = objAssembly.GetType("MyClassName");
object obj = Activator.CreateInstance(classType)
bject[] _objval = new object[3];
object[] parameters = new object[] { _objval };
MethodInfo mi = classType.GetMethod("MyMethod");
mi.Invoke(obj, parameters); // <---**How do i call this with threads.. ????**
Run Code Online (Sandbox Code Playgroud) 我正在构建一些Linq Expression并尝试获取MethodInfo IEnumerable.DefaultIfEmpty
(http://msdn.microsoft.com/en-us/library/bb360179.aspx).什么似乎是一个简单的任务,但我不知道它为什么不工作.
typeof(Enumerable).GetMethod("DefaultIfEmpty", new[] { typeof(IEnumerable<>) });
typeof(Enumerable).GetMethod("DefaultIfEmpty", new[] { typeof(IEnumerable<>).MakeGenericType(typeof(WorkitemListModel)) });
Run Code Online (Sandbox Code Playgroud) 我正在尝试将方法名称传递给Action委托.这就是我所拥有的:
private static void DoAction(params Action<Group>[] actions)
{
foreach (Action<Group> action in actions)
{
Console.WriteLine(action.Method.Name);
}
}
Run Code Online (Sandbox Code Playgroud)
在主要情况下,这就是它被调用的方式:
DoAction(y => y.DoBar(), z => z.DoFoo());
Run Code Online (Sandbox Code Playgroud)
在执行DoAction()方法之后,我希望在屏幕上看到"DoFoo"和"DoBar",但我看到< Main>b__0
和<Main>b__1
.我只是想知道是否有办法从动作委托中获取目标方法的实际名称?任何帮助表示赞赏.
这是此问题的后续内容:Lambda表达式未返回预期的MemberInfo
class Human
{
public string name { get; set; }
}
class Man : Human
{
}
var m1 = typeof(Human).GetProperty("name");
var m2 = typeof(Man).GetProperty("name");
//m1 != m2 why?
Run Code Online (Sandbox Code Playgroud)
这同样适用于MethodInfo
秒.
我可以理解,必须有一个差时Human
是一个接口,或当name
的Human
是抽象/虚拟的.但为什么密封型呢?不是name
的Man
确切name
的Human
?
澄清:乔恩说他们ReflectedType
的不同.ReflectedType
在决定接口成员或被覆盖成员的相等性时,平等应该会派上用场,因为它们是不同的.但我不认为应该考虑决定上述简单案件的平等性.可能是设计团队希望保持一致.只是想知道是什么原因促使框架设计者ReflectedType
在决定跨越多个类的同一成员的相等性时考虑属性.
我创建了一个MethodInfo的实例:
MethodInfo theMethod = typeof(Reciever).GetMethod("methodName", parameterTypes);
Run Code Online (Sandbox Code Playgroud)
现在我想知道theMethod的返回类型是否为void.怎么样?
我的问题是这样的:如果我有MethodInfo对象,对于一个方法,从一个接口类型得到的,我也有一个实现此接口的类的类型的对象,但它有一个明确的实施实现了该方法,如何为该类中的实现方法正确获取相应的MethodInfo对象?
我需要这样做的原因是实现方法可以有一些属性应用于它们,我需要通过反射找到它们,但是需要找到这些属性的类只有实现类的对象引用,以及类型对象(+对应的MethodInfo对象)用于接口.
所以,我们假设我有以下程序:
using System;
using System.Reflection;
namespace ConsoleApplication8
{
public interface ITest
{
void Test();
}
public class Test : ITest
{
void ITest.Test()
{
throw new NotImplementedException();
}
}
class Program
{
static void Main(string[] args)
{
Type interfaceType = typeof(ITest);
Type classType = typeof(Test);
MethodInfo testMethodViaInterface =
interfaceType.GetMethods()[0];
MethodInfo implementingMethod =
classType.GetMethod(/* ??? */"Test");
Console.Out.WriteLine("interface: " +
testMethodViaInterface.Name);
if (implementingMethod != null)
Console.Out.WriteLine("class: " +
implementingMethod.Name);
else
Console.Out.WriteLine("class: unable to locate");
Console.Out.Write("Press enter to exit...");
Console.In.ReadLine();
} …
Run Code Online (Sandbox Code Playgroud) sealed public class HMethod
{
public int Calc(string Method, int X1, int X2, int Y1, int Y2)
{
MethodInfo HMethodInfo = this.GetType().GetMethod(Method);
return (int)HMethodInfo.Invoke(
this,
new object[4] { X1, X2, Y1, Y2 }
);
}
int ManhattanH(int X1, int X2, int Y1, int Y2)
{
//Blah
}
int LineH(int X1, int X2, int Y1, int Y2)
{
//Blah
}
//Other Heuristics
}
Run Code Online (Sandbox Code Playgroud)
调用new HMethod().Calc("ManhattanH". X1, X2, Y1, Y2)
HMethodInfo时为null.创建一个空引用Exception.它应该调用通过文本传递的方法(从文本文件中获取)
已解决:方法是私有的.
methodinfo ×10
c# ×9
reflection ×6
.net ×3
aop ×1
async-await ×1
delegates ×1
fieldinfo ×1
inheritance ×1
interface ×1
invoke ×1
propertyinfo ×1
void ×1