用类一般封装动作/函数

  • 本文关键字:函数 封装 c# .net generics
  • 更新时间 :
  • 英文 :


我想提供操作/方法的通用封装。我认为这在c#中应该是可能的,但我无法生成它,所以它编译…

下面简要地演示了我想要的。这有可能吗,也许可以通过一般化这个类?

要求是:

  • 我想执行的功能/动作(见方法类型)和'做一些事情'时,错误发生
  • 我想返回函数的值如果方法是一个函数(否则返回void如果可能的话)
  • 如果函数的返回类型是boolean,值是false,我想"做点什么"。

    public class Encapsulator {
        private Action _action;
        private Func<T> _function;
        private MethodType _type; //Action || Function
        public Encapsulator(Action action) {
            this._action = action;
            this._type = MethodType.Action;
        }
        public Encapsulator(Func<T> func) { //This is not accepted
            this._function = func;
            this._type = MethodType.Function;        
        }
        public void Execute() {
            try {
                this._action();
            }
            catch(Exception ex) {
                //do something
                throw;
            }
        }
        public T Execute<T>() {
            try {
                var r = this._function();
                if(typeof(r) == bool) {
                    if(!r)
                      //do something
                }
                return r;
            } catch(Exception ex) {
                //do something
                throw;
            }
        }
    }
    

您的第二个构造函数将无法编译,因为它们没有在更高级别应用于该类型的泛型:

public Encapsulator<T>
{  
    public Encapsulator(Func<T> func)
    { 
        this._function = func;
        this._type = MethodType.Function;  
    }      
}

本质上,我们需要指定这些东西在定义中是"可用的",而不是仅仅在方法的参数中引入新的东西。因此,例如,如果您试图添加一个特定的泛型方法,您可以像您所做的那样应用它,但相反需要这样做(您使用Execute方法演示的事情):

public void MyGenericMethod<T>(Func<T> func)
{ 
} 

注意第一个 T,我们指定T的存在性,就像这样。

这里的代码可能有更多的问题,但我相信,乍一看,这是问题的症结所在。

至于返回变量类型,您可能希望的最好结果是返回普通的旧object。或者,使用dynamic类型,然而,我不会认为这是要走的路,也不会推荐它;但是,您不能将返回类型从实际类型转换为void。

新方法:动作封装器现在将返回一个虚拟结果(总是true)。

public class Encapsulator
{
    public static Encapsulator<bool> CreateEncapsulator(Action action)
    {
        return new Encapsulator<bool>(() =>
            {
                action();
                return true;
            });
    }
    public static Encapsulator<T> CreateEncapsulator<T>(Func<T> func)
    {
        return new Encapsulator<T>(func);
    }
}
public class Encapsulator<T>
{
    private Func<T> _function;
    internal Encapsulator(Func<T> func)
    {
        this._function = func;
    }
    public T Execute()
    {
        try
        {
            object res = this._function();
            Nullable<bool> bres = res as Nullable<bool>;
            if (bres.HasValue)
            {
                if (!bres.Value)
                    Console.WriteLine("NOT TRUE!");
                //do something
            }
            return (T)res;
        }
        catch (Exception ex)
        {
            //do something
            throw;
        }
    }
}

调用代码:

var withDummyReturn = Encapsulator.CreateEncapsulator(() => Console.WriteLine("Great"));
withDummyReturn.Execute();
var withReturn = Encapsulator.CreateEncapsulator<bool>(() => true);
bool res = withReturn.Execute();

最新更新