MissingMethodException但我不明白为什么

时间:2022-04-15 01:40:58

I'm creating an assembly via reflection, and then using it to create an instance of a WCF service client.

我正在通过反射创建一个程序集,然后使用它来创建一个WCF服务客户端的实例。

object obj = 
   assembly.CreateInstance(
       serviceName, true, 
       BindingFlags.CreateInstance,null,createArgs, null, null);

Type type = obj.GetType();

obj is of type HelloWorldServiceClient.

obj的类型为HelloWorldServiceClient。

type.GetMethods() has 14 MethodInfo results. The first one is {Acme.TestService.HelloWorldResponse HelloWorld(Acme.TestService.HelloWorldRequest)}

type.GetMethods()有14个MethodInfo结果。第一个是{Acme.TestService.HelloWorldResponse HelloWorld(Acme.TestService.HelloWorldRequest)}

But when I do

但是,当我这样做

return (T)type.InvokeMember(
    "HelloWorld", BindingFlags.InvokeMethod, null, obj, args);

I get a MissingMethodException.

我得到一个MissingMethodException。

type.ContainsGenericParameters = false.

type.ContainsGenericParameters = false。

args is object[1] and contains a single {Acme.TestService.HelloWorldRequest}.

args是object [1]并包含一个{Acme.TestService.HelloWorldRequest}。

I'm dreadfully confused. Can anyone help me out?

我非常困惑。谁能帮我吗?

2 个解决方案

#1


You can also use GetMethod(methodName) and than Invoke it. I would advice those two steps if you dynamically creating an assembly. In this way, you can locate that method exist first, and than call it.

您也可以使用GetMethod(methodName)而不是Invoke它。如果动态创建程序集,我会建议这两个步骤。通过这种方式,您可以首先找到该方法,然后再调用它。

#2


You say you are creating the assembly via reflection... but WCF internally also does type generation; I wonder if there isn't some duplication here? You can get the WCF-generated service type via something like:

你说你是通过反射创建程序集......但是WCF内部也会进行类型生成;我想知道这里是否有一些重复?您可以通过以下方式获取WCF生成的服务类型:

public sealed class WcfClient<T: System.ServiceModel.ClientBase<T>
      where T : class
{
    public T Service { get { return base.Channel; } }
}

However - re the question; if you are using TypeBuilder, I wonder if you haven't used DefineMethodOverride to associate the actual method with the interface. This might be implicit for C#, but it needs to be explicit in the IL.

然而 - 问题;如果你使用TypeBuilder,我想知道你是否还没有使用DefineMethodOverride将实际方法与接口相关联。这可能是C#的隐含,但它需要在IL中明确。

#1


You can also use GetMethod(methodName) and than Invoke it. I would advice those two steps if you dynamically creating an assembly. In this way, you can locate that method exist first, and than call it.

您也可以使用GetMethod(methodName)而不是Invoke它。如果动态创建程序集,我会建议这两个步骤。通过这种方式,您可以首先找到该方法,然后再调用它。

#2


You say you are creating the assembly via reflection... but WCF internally also does type generation; I wonder if there isn't some duplication here? You can get the WCF-generated service type via something like:

你说你是通过反射创建程序集......但是WCF内部也会进行类型生成;我想知道这里是否有一些重复?您可以通过以下方式获取WCF生成的服务类型:

public sealed class WcfClient<T: System.ServiceModel.ClientBase<T>
      where T : class
{
    public T Service { get { return base.Channel; } }
}

However - re the question; if you are using TypeBuilder, I wonder if you haven't used DefineMethodOverride to associate the actual method with the interface. This might be implicit for C#, but it needs to be explicit in the IL.

然而 - 问题;如果你使用TypeBuilder,我想知道你是否还没有使用DefineMethodOverride将实际方法与接口相关联。这可能是C#的隐含,但它需要在IL中明确。