tags:

views:

343

answers:

2

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

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

Type type = obj.GetType();

obj is of type HelloWorldServiceClient.

type.GetMethods() has 14 MethodInfo results. The first one is {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.

type.ContainsGenericParameters = false.

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

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

+2  A: 

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:

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.

Marc Gravell
+1  A: 

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.