封装动作T和Func

发布于 2024-10-04 01:50:08 字数 862 浏览 8 评论 0原文

我正在尝试为某种 IExecutable 接口进行设计。我不会详细介绍,但重点是我有几个需要从基类执行的操作。它们可能采用不同的参数(没什么大不了的),并且它们可能/可能不返回值。

到目前为止,这是我的设计:

public abstract class ActionBase
{
    // ... snip ...
}

public abstract class ActionWithResultBase<T>: ActionBase
{
    public abstract T Execute();
}

public abstract class ActionWithoutResultBase: ActionBase
{
    public abstract void Execute();
}

到目前为止,我的每个具体操作都需要是 ActionWithResultBase 或 ActionWithoutResult 基的子级,但我真的不喜欢那样。 如果我可以将 Execute 的定义移至 ActionBase,考虑到具体类可能会或可能不会返回值,我将实现我的目标。

有人告诉我这可以通过使用 Func 和 Action 来完成,对此我完全同意,但我找不到一种方法将其放入一个类中,以便调用者知道该操作是否会返回值。

简介:我想做一些类似的事情:

// Action1.Execute() returns something.
var a = new Action1();
var result = a.Execute();

// Action2.Execute() returns nothing.
var b = new Action2();
b.Execute();

I'm trying to make a design for some sort of IExecutable interface. I will not get into details, but the point is that I have several Actions that need to be executed from a base class. They may take different parameters (no big deal), and they may/may not return a value.

So far, this is my design:

public abstract class ActionBase
{
    // ... snip ...
}

public abstract class ActionWithResultBase<T>: ActionBase
{
    public abstract T Execute();
}

public abstract class ActionWithoutResultBase: ActionBase
{
    public abstract void Execute();
}

So far, each of my concrete actions need to be a child from either ActionWithResultBase or ActionWithoutResult base, but I really don't like that. If I could move the definition of Execute to ActionBase, considering that the concrete class may or may not return a value, I will have achieved my goal.

Someone told me this could be done with using Func and Action, for which I totally agree, but I can't find a way to have that into one single class so that the caller would know if the action is going to return a value or not.

Brief: I want to do something like:

// Action1.Execute() returns something.
var a = new Action1();
var result = a.Execute();

// Action2.Execute() returns nothing.
var b = new Action2();
b.Execute();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

翻了热茶 2024-10-11 01:50:08

如果您想要一个轻量级的解决方案,那么最简单的选择是编写两个具体的类。一个将包含 Action 类型的属性,另一个包含 Func 类型的属性:

public class ActionWithResult<T> : ActionBase { 
  public Func<T> Action { get; set; } 
}

public class ActionWithoutResult : ActionBase {
  public Action Action { get; set; }
}

然后您可以像这样构造这两种类型:

var a1 = new ActionWithResult<int> { 
  CanExecute = true,
  Action = () => { 
    Console.WriteLine("hello!");
    return 10; 
  }
}

如果您不想要使 Action 属性读/写,那么您可以将操作委托作为参数传递给构造函数并使该属性只读。

C# 需要两个不同的委托来表示函数和操作,这一事实非常烦人。人们使用的一种解决方法是定义一个表示“无返回值”的类型 Unit 并使用它来代替 void。那么您的类型将只是 Func 并且您可以使用 Func 而不是 ActionUnit 类型可能如下所示:

public class Unit {
  public static Unit Value { get { return null; } }
}

要创建 Func 值,您将编写:

Func<Unit> f = () => { /* ... */ return Unit.Value; }

If you want a lightweight solution, then the easiest option would be to write two concrete classes. One will contain a property of type Action and the other a property of type Func<T>:

public class ActionWithResult<T> : ActionBase { 
  public Func<T> Action { get; set; } 
}

public class ActionWithoutResult : ActionBase {
  public Action Action { get; set; }
}

Then you can construct the two types like this:

var a1 = new ActionWithResult<int> { 
  CanExecute = true,
  Action = () => { 
    Console.WriteLine("hello!");
    return 10; 
  }
}

If you don't want to make Action property read/write, then you could pass the action delegate as an argument to the constructor and make the property readonly.

The fact that C# needs two different delegates to represent functions and actions is quite annoying. One workaround that people use is to define a type Unit that represents "no return value" and use it instead of void. Then your type would be just Func<T> and you could use Func<Unit> instead of Action. The Unit type could look like this:

public class Unit {
  public static Unit Value { get { return null; } }
}

To create a Func<Unit> value, you'll write:

Func<Unit> f = () => { /* ... */ return Unit.Value; }
蔚蓝源自深海 2024-10-11 01:50:08

以下接口应该可以解决问题——它本质上是复制 Nullable 模式

public interface IActionBase
{
       bool HasResult { get; }
       void Execute() { }
       object Result { get; }
}

public interface IActionBase<T> : IActionBase
{
       new T Result { get; }
}

public sealed class ActionWithReturnValue<T> : IActionBase<T>
{
       public ActionWithReturnValue(Func<T> action) {  _action = action; }
       private Func<T> _action;

       public bool HasResult { get; private set; }
       object IActionBase.Result { get { return this.Result; } }
       public T Result { get; private set; }
       public void Execute()
       {
            HasResult = false;
            Result = default(T);
            try 
            { 
                 Result = _action();
                 HasResult = true;
             }
            catch
            {
                HasResult = false;
                Result = default(T);   
            }  
       }

}

public sealed class ActionWithoutReturnValue : IActionBase
{
      public bool HasResult { get { return false; } }
      object IActionBase.Result { get { return null; } }
      public void Execute() { //... }
}

The following interfaces should do the trick -- it's essentially copying the Nullable pattern

public interface IActionBase
{
       bool HasResult { get; }
       void Execute() { }
       object Result { get; }
}

public interface IActionBase<T> : IActionBase
{
       new T Result { get; }
}

public sealed class ActionWithReturnValue<T> : IActionBase<T>
{
       public ActionWithReturnValue(Func<T> action) {  _action = action; }
       private Func<T> _action;

       public bool HasResult { get; private set; }
       object IActionBase.Result { get { return this.Result; } }
       public T Result { get; private set; }
       public void Execute()
       {
            HasResult = false;
            Result = default(T);
            try 
            { 
                 Result = _action();
                 HasResult = true;
             }
            catch
            {
                HasResult = false;
                Result = default(T);   
            }  
       }

}

public sealed class ActionWithoutReturnValue : IActionBase
{
      public bool HasResult { get { return false; } }
      object IActionBase.Result { get { return null; } }
      public void Execute() { //... }
}
寄风 2024-10-11 01:50:08

您知道可以忽略方法的返回值吗?您不必使用它。

You know that you can ignore the return value of a method right? You don't have to use it.

旧情别恋 2024-10-11 01:50:08

一些简单的事情怎么样:

public class ActionExecuter
{
    private MulticastDelegate del;
    public ActionExecuter(MulticastDelegate del)
    {
        this.del = del;
    }

    public object Execute(params object[] p)
    {
        return del.DynamicInvoke(p);
    }
}

what about something simple:

public class ActionExecuter
{
    private MulticastDelegate del;
    public ActionExecuter(MulticastDelegate del)
    {
        this.del = del;
    }

    public object Execute(params object[] p)
    {
        return del.DynamicInvoke(p);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文