1

我不确定如何最好地描述我想要的东西,所以我将从高层次开始,然后回顾我对实施的想法。

使用 c# 我正在尝试创建一个具有通用返回类型的方法,并将服务引用中的方法作为参数。

这个通用方法将新建服务引用,调用我传入的服务引用的方法,执行服务引用所需的所有错误处理和检查,然后关闭或中止它并返回调用结果。

这种伪代码:

public T CallServiceReference<T>(method serviceRefMethod) {
  T result = null;
  ServiceReference svcClient = new ServiceReference.Client();
    try {
      result = svcClient.serviceRefMethod;
      svcClient.Close();   
    } catch (ExceptionType ex) {
      // log error message
      svcClient.Abort();
      throw;
    }
  return result;
}

这在c#中可能吗?我正在寻找泛型和代表。我的主要问题之一是在没有实例化服务引用的情况下将服务引用的方法委托给一个。如果我必须实例化服务引用,我想我还不如为每个方法调用放置所有的关闭、中止和错误处理。

我正在研究不同的设计模式,虽然这有点困难,因为我不知道我正在寻找的那个的名字,或者它是否存在。

让我知道我是否可以提供任何其他信息或澄清。

更新(第 2 部分):现在我正在尝试创建一个使用它调用的方法封装变量的委托。

public delegate T Del<T>();
public static IEnumerable<String> GetDataFromService(String username) {
    ServiceReference.ServiceClient client = new ServiceReference.ServiceClient();
    // the method I'm going to call returns a string array
    Del<String[]> safeClientCall = new Del<String[]>(client.DataCall);
    // the above call is what I need to use so the C# compiles, but I want to do this
    // the below code throws an error...
    Del<String[]> safeClientCall = new Del<String[]>(client.DataCall(username));
    var result = DataCallHandlerMethod(ref client, safeClientCall);
    return result;
}

基本上从我的调用方法传递用户名参数,并且该用户名参数已经定义。我不想在调用委托时定义它。有没有办法使用 c# 来做到这一点?

4

1 回答 1

1

一般来说,你的答案中的一切都是可能的,除了这一行:

result = svcClient.serviceRefMethod;

这显然是一个至关重要的调用......为了在对象上动态调用函数,您可以做一些事情。一个简单的方法是将您的函数签名更改为:

public T CallServiceReference<T>(ServiceReference svcClient, method serviceRefMethod)

但随后调用代码需要更新ServiceReference并传入svcClient.[desiredFunction]serviceRefMethod.

另一种方法是将您的签名更改为:

public T CallServiceReference<T>(string serviceRefMethodName)

然后使用反射找到方法并调用它。你不会得到编译时验证(所以如果你有错字,它会在运行时崩溃),但你会得到动态调用。例如:

svcClient.GetType().InvokeMember(
   methodName, /* what you want to call */

   /* 
      Specifies what kinds of actions you are going to do and where / how 
      to look for the member that you are going to invoke 
    */
   System.Reflection.BindingFlags.Public | 
     System.Reflection.BindingFlags.NonPublic |
     System.Reflection.BindingFlags.Instance | 
     System.Reflection.BindingFlags.InvokeMethod, 

   null,      /* Binder that is used for binding */
   svcClient, /* the object to call the method on */
   null       /* argument list */
 );

基于您的更新的额外信息(PS 这可能是一个单独的问题)

您现在不仅要传入一个方法,还要传入该方法的调用。由于并非每个方法都以相同的方式被调用,因此您尝试在调用站点执行此操作,但那是在您真正想要调用该方法之前。从本质上讲,您要做的是穿梭于稍后才会执行的代码(在 的上下文中GetDataFromService)。

您可以使用反射路由(在这种情况下,您可以传入一个传递给调用object[]的参数,或者查看它允许您创建一些在调用时运行的代码。例如:InvokeMemberFuncFunc

 GetDataFromService(new Func<object>(() => { return client.DataCall(username); }));
于 2013-06-19T03:46:25.063 回答