【问题标题】:Is it possible to have method only accessible after certain conditions are met?是否有可能只有在满足某些条件后才能访问方法?
【发布时间】:2020-07-30 16:11:44
【问题描述】:

我正在尝试使方法 MethodA 仅在 bool 可执行文件为 true 时才可访问。否则,可以访问其他方法 MethodB。例如:

private bool executable = true;

public int MethodA(); <-- // Is accessible from outside of the class because executable is true

public string MethodB() <-- // Is not accessible because executable is true

我尝试这样做的主要原因是这 2 种方法返回 2 种不同的类型。所以我的问题是,这甚至可能吗?

【问题讨论】:

  • Conditional compilation 可以作为答案。
  • 你如何使用这些方法/你在什么环境下使用它?
  • 这听起来像是 XY 问题的形成。这里的总体目标是什么?也就是说,您尝试这样做是为了解决什么潜在问题?
  • 如果条件在编译时已知:是,使用预处理器语句,否则:否
  • 使用类型安全可能可以实现您想要完成的工作,但是如果不了解您期望如何调用这些方法,就很难提供答案。

标签: c# methods


【解决方案1】:

选项#1

您可以使用多态性和泛型获得您想要的东西。如果需要,这还允许您添加其他方法策略。

public interface IMethodStrategy<out T>
{
    T DoSomething();
}

public class MethodOneStrategy : IMethodStrategy<string>
{
    public string DoSomething()
    {
        return "This strategy returns a string";
    }
}

public class MethodTwoStrategy : IMethodStrategy<int>
{
    public int DoSomething()
    {
        return 100; // this strategy returns an int
    }
}

// And you would use it like so...
static void Main(string[] args)
{
    bool executable = true; 
    object result = null;
    if (executable)
    {
        MethodOneStrategy methodA = new MethodOneStrategy();
        result = methodA.DoSomething();
    }
    else
    {
        MethodTwoStrategy methodB = new MethodTwoStrategy();
        result = methodB.DoSomething();
    }
}

选项#2

另一种选择是包装工作方法的简单代理方法。

// proxy class to wrap actual method call with proxy call
public class MethodProxy
{
    public object DoMethodWork(bool executable)
    {
        if (executable)
        {
            return MethodA();
        }
        else
        {
            return MethodB();
        }
    }

    private int MethodA()
    {
        return 100; // returns int type
    }

    private string MethodB()
    {
        return "this method returns a string";
    }
}

// used like so
 static void Main(string[] args)
{
    var methodProxy = new MethodProxy();
    object result = methodProxy.DoMethodWork(true);
}

【讨论】:

    【解决方案2】:

    为此使用conditional compilation

    #if RELEASE
    
    public string MethodB() ...
    
    #endif
    

    虽然我怀疑你是否需要这个。你的理由没有多大意义。

    您可以使用不同的Build Configurations 来管理您的条件编译符号。

    【讨论】:

      【解决方案3】:
      if(executable)
       MethodA();
      else
       MethodB();
      

      if(executable)
       MethodA();
      
      MethodB();
      

      【讨论】:

      • 这不会改变类方法的可访问性
      【解决方案4】:

      不完全确定您要做什么,但这可能是一种方式,可能不是最有效的方式,但取决于您要做什么?

      public int MethodA(executable)
      {
          if(executable = true)
          {
              //do stuff
          }
          else
          {
              return -1;
          }
      }
      
      public String MethodB(executable)
      {
          if(executable = false)
          {
              //do stuff
          }
          else
          {
              String error = "MethodB cannot be used right now";
              return error;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-12-21
        • 2013-06-10
        • 1970-01-01
        • 1970-01-01
        • 2020-10-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多