首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态调用方法

动态调用方法
EN

Stack Overflow用户
提问于 2014-04-25 06:47:11
回答 2查看 93关注 0票数 0

我有一个WCF服务,它公开了一堆对各种类型的集合执行操作的[OperationContract]

现在,几乎每个实现都是这样的:

代码语言:javascript
复制
        public void PerformSomeOperation(List<SomeType> someCollection) {
        // store the exceptions that might be thrown during the operation
        var exceptions = new List<Exception>();

        foreach (var item in someCollection) {
            try {
                this.PerformSomeOperation(someParameters);
            }
            catch (Exception exception) {
                exceptions.Add(exception);
            }
        }

        // Throw the exceptions here after the loop completes. 
        if (exceptions.Count > 0) throw new AggregateException(exceptions);
    }

因此,虽然this.PerformSomeOperation(...)部分在实际实现之间发生变化,但框架的其余部分保持不变。因此,动态注入this.PerformSomeOperation(...)部件的想法。

最优雅的解决方案是什么?当然,我可以传入要作为字符串在外部方法的参数列表中执行的操作的方法名称,并使用反射调用该方法(.GetMethod(...)),但我希望得到一些优雅的Lambda或Delegate构造。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-25 07:05:40

使用辅助方法

代码语言:javascript
复制
public void YourServiceMethod()
{
    var collection = GetSomeDate();
    DoWork(collection, item => PerformSomeOperation(someParameters));
}

private void DoWork(List<SomeType> collection, Action<SomeType> itemProcessor)
{
   var exceptions = new List<Exception>();

    foreach (var item in collection) 
    {
        try 
        {
            itemProcessor(someParameters);
        }
        catch (Exception exception) 
        {
            exceptions.Add(exception);
        }
    }

    // Throw the exceptions here after the loop completes. 
    if (exceptions.Count > 0) throw new AggregateException(exceptions);
}

或者使用扩展方法:

代码语言:javascript
复制
public void YourServiceMethod()
{
    var collection = GetSomeDate();
    collection.DoWork(item => PerformSomeOperation(someParameters));
}

public class ListExtensions
{
    public void DoWork(this List<SomeType> collection, Action<SomeType> itemProcessor)
    {
       var exceptions = new List<Exception>();

        foreach (var item in collection) 
        {
            try 
            {
                itemProcessor(someParameters);
            }
            catch (Exception exception) 
            {
                exceptions.Add(exception);
            }
        }

        // Throw the exceptions here after the loop completes. 
        if (exceptions.Count > 0) throw new AggregateException(exceptions);
    }
}

加入一些泛型(以支持所有类型):

代码语言:javascript
复制
public void YourServiceMethod()
{
    var collection = GetSomeDate();
    collection.ProcessList(item => PerformSomeOperation(someParameters));
}

public class ListExtensions
{
    public void ProcessList<T>(this IEnumerable<T> collection, Action<T> itemProcessor)
    {
       var exceptions = new List<Exception>();

        foreach (var item in collection) 
        {
            try 
            {
                itemProcessor(someParameters);
            }
            catch (Exception exception) 
            {
                exceptions.Add(exception);
            }
        }

        // Throw the exceptions here after the loop completes. 
        if (exceptions.Count > 0) throw new AggregateException(exceptions);
    }
}
票数 1
EN

Stack Overflow用户

发布于 2014-04-25 06:54:46

为什么不使用代表们呢?这些基本上是函数指针。

因此,您有一个通用的DoWork方法和一个动作

代码语言:javascript
复制
  public void DoWork(Action<List<SomeType>> myDelegate)
  {
       var exceptions = new List<Exception>();

        foreach (var item in someCollection) 
        {
            try {
                myDelegate(someParameters);
            }
            catch (Exception exception) {
                exceptions.Add(exception);
            }
        }

        // Throw the exceptions here after the loop completes. 
        if (exceptions.Count > 0) throw new AggregateException(exceptions);
  }

然后用指向您自己的函数PerformSomeOperation的指针调用它。

代码语言:javascript
复制
DoWork(PerformSomeOperation);

现在您可以切换可以使用的方法了。

代码语言:javascript
复制
DoWork(PerformOtherOperationWithSameSignature)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23285995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档