【问题标题】:Difficulties with using static functions and variables in C#在 C# 中使用静态函数和变量的困难
【发布时间】:2013-10-04 06:51:26
【问题描述】:

我有这样的课

#region Properties
    private static string inputURL;
    public static string InputURL
    {
        get { return inputURL; }
        set { inputURL = value; }
    }
    private static string outputURL;
    private static string ffBaseURL = "format=xml&";
    public static string FFBaseURL
    {
        get { return ffBaseURL; }
        set { ffBaseURL = value; }
    }
    private static string excludeParam = "fullurl,log";
    public static string ExcludeParam
    {
        get { return excludeParam; }
        set { excludeParam = value; }
    }
    private static string currentCategoryID = "234";
    public static string CurrentCategoryID
    {
        get { return currentCategoryID; }
        set { currentCategoryID = value; }
    }
    private static string navigationParameters = "query=*&log=navigation&filterCategoryId=" + currentCategoryID;
    public static string NavigationParameters
    {
        get { return navigationParameters; }
        set { navigationParameters = value; }
    } 
    #endregion

    #region Methods
    public static string NavigationCall()
    {

        List<string> excludeParams = new List<string>(excludeParam.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
        foreach (string key in HttpContext.Current.Request.QueryString.Keys)
        {
            if (!excludeParams.Contains(key))
            {
                FFBaseURL += key + "=" + HttpContext.Current.Request[key] + "&";
            }
        }
        FFBaseURL += NavigationParameters;


        if (Common.IsInternalIP())
        {
            FFBaseURL += "&log=internal";
        }
        outputURL = ffBaseURL;
        return outputURL;
    } 
    #endregion

如您所见,我有一个名为 NavigationCall() 的静态函数,该函数必须保持静态。当我从我的网站调用此函数时,由于静态属性 i,该函数在每个函数调用中返回错误值声明。我们都知道静态属性在程序执行后会保留它们的值。

所以说当我第一次调用这些函数时我得到一个结果“tesresult1”,第二次当我重新加载我的网页时它给我一个结果“testresult1testresult1”。我认为你现在遇到了问题。

  1. 我已经尝试通过再次声明静态变量值来解决这个问题,但它看起来不是一个很好的编程方式。

  2. 我尝试将属性设为非静态。但它返回错误,因为 NavigationCall() 是静态函数,我无法在其中调用非静态属性。

现在我正在寻找解决这个问题的正确方法,我认为这个问题是由于对 OOPS 概念的错误理解而出现的。任何人都可以在这里帮忙解决这个问题,或者如果问题很重要到一些我可以理解如何找到解决方案的资源?

【问题讨论】:

  • 如果您在函数中重置(读作“重新分配”基值)FFBaseURL 参数会有所帮助,但我完全同意@Szymon 的回答。

标签: c# asp.net oop


【解决方案1】:

您可以将所有参数传递给您的静态方法,而不是使用静态属性。

public static string NavigationCall(
     string inputURL,
     string ffBaseURL,
     string excludeParam,
     string currentCategoryID,
     string navigationParameters
)
{
     // the body of your method
}

【讨论】:

    【解决方案2】:

    您还可以将所有属性捆绑到自定义对象中并将其传递给方法。此外,对于任何解决方案,您都必须使 NavigationCall 线程安全。 Are static methods thread safe ?

    public static string NavigationCall(CustomNavigation objCustomNavigation)
    
    //Custom object.
    public class CustomNavigation
    {
      public string InputURL {get;set;}
      public string FBaseURL{get;set;}
      public string ExcludeParam{get;set;}
      public string CurrentCategoryID {get;set;}
      public string NavigationParameters{get;set;}
    }
    

    【讨论】:

    • 我更喜欢这个而不是@Szymon 的建议,因为在这里您已经将调用的概念封装在一个不错的parameter object 中,您以后可以在其中实现进一步的逻辑,如基本验证、默认值......您可能希望将 setter 设为私有并通过构造函数设置值以提高清晰度和安全性。
    • @EagleBeak 如果您可以通过合适的示例将其发布为答案,那就太好了
    【解决方案3】:

    我建议introduce a parameter object(正如@mit 建议的那样)并利用这个机会在那里封装你的一些逻辑。这应该会立即简化您的方法。也许您可以将其中一些属性设为私有,因为它们只在封装在参数对象中的逻辑中需要。

        //Your static method
        public static string NavigationCall(CustomNavigation customNavigation)
    
        //Disclaimer: I have no idea, whether this is an appropriate name. 
        //It really depends on what you want to do with his class
        class CustomNavigation
        {
            public string InputURL { get; private set; }
            public string FFBaseURL { get; private set; }
            public IEnumerable<string> ExcludeParams { get; private set; }
            public string CurrentCategoryID { get; private set; }
            public string NavigationParameters { get; private set; }
    
            public CustomNavigation(string inputUrl, string excludeParam, string fBaseUrl, string currentCategoryID, string navigationParameters)
            {
                // various guard clauses here...
    
                NavigationParameters = navigationParameters;
                CurrentCategoryID = currentCategoryID;
                FFBaseURL = fBaseUrl;
                InputURL = inputUrl;
    
                // Parse string here -> Makes your method simpler
                ExcludeParams = new List<string>(excludeParam.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
            }
    
            //Example for encapsulating logic in param object
            public void AddKeys(HttpContext currentContext)
            {
                var keys = currentContext.Request.QueryString.Keys
                            .Cast<string>()
                            .Where(key => !ExcludeParams.Contains(key));
    
                foreach (var key in keys)
                    FFBaseURL += key + "=" + currentContext.Request[key] + "&";
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-27
      • 1970-01-01
      • 2016-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-15
      • 1970-01-01
      相关资源
      最近更新 更多