繁体   English   中英

在代理上使用反射的c#统一调用方法

[英]c# unity calling method using reflection on proxy

我陷入一个问题。 我有这样的一段代码:

ITest test= ObtainObject(); //ObtainObjectimplementation omitted
Type type = test.GetType();
MethodInfo method = type.GetMethod("Display");
method.Invoke(test, new object[] {"hi"});

interface ITest {
    [LoggerAspect]
    Display(String msg);
}

class Test : ITest 
{
    public void Display(String msg) 
    {
        MessageBox.Show(msg);   
    }
}

问题是当我更换

method.Invoke(test, new object[] {"hi"});

test.Display("Hi")

一切,包括方面,都可以正常工作。 该测试是一个代理对象,如果我在该代理上调用了调用,它将抛出我

TargetException: Object does not match target type

问题是我需要使用反射,你们有没有遇到过这样的问题? 感谢您的建议

为什么界面中的Display方法没有定义任何返回类型? 我认为那不应该首先编译。

您还应该考虑显示ObtainObject代码,因此我们可以确切地知道如何获取接口。 除此之外,我想我做不到。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM