C# 减少变量名长度

C# 减少变量名长度,c#,.net,asp.net,C#,.net,Asp.net,我正在使用下面的代码,会话变量在公共类和SessionVariables结构下声明 会话变量名变得很长,你知道在我的情况下如何最小化变量名吗?我可以使用变量名,如SessionVariables.IsLogout,但不包括类名吗 会话[Common.SessionVariables.IsLogout] public class Common { public struct SessionVariables { public static string IsLogo

我正在使用下面的代码,会话变量在公共类和SessionVariables结构下声明

会话变量名变得很长,你知道在我的情况下如何最小化变量名吗?我可以使用变量名,如SessionVariables.IsLogout,但不包括类名吗

会话[Common.SessionVariables.IsLogout]

public class Common
{
    public struct SessionVariables
    {
        public static string IsLogout = "IsLogout";
    }
}

一种方法是从公共类继承类。然后可以直接将该变量引用为SessionVariables.IsLogout。

一种方法是从公共类继承类。然后您可以直接将该变量引用为SessionVariables.IsLogout。

您可以创建一个“快捷方式类”,如:

然后您可以执行
会话[SesVar.IsLogout]

但就我个人而言,我不会这么做,因为这对代码的可读性不好,而且IntelliSense会为您完成大部分键入工作。

您可以创建一个“快捷方式类”,如:

然后您可以执行
会话[SesVar.IsLogout]


但就我个人而言,我不会这么做,因为这对代码的可读性不好,而且IntelliSense会为您完成大部分键入工作。

这只是缩短代码的一种方法

 public static class Account
    {
        public static int UserID
        {
            get { return Session[SessionVariables.UserID]; }
            set { Session[SessionVariables.UserID] = value; }
        }

        // .... and so on
    }
您可以这样使用它们:

protected void Page_Load(object sender, EventArgs e)
{
     Response.Write(Account.UserID);
}
它比一直使用Sessionvariables.UserID要短得多


我的2美分,这只是缩短它的一种方法

 public static class Account
    {
        public static int UserID
        {
            get { return Session[SessionVariables.UserID]; }
            set { Session[SessionVariables.UserID] = value; }
        }

        // .... and so on
    }
您可以这样使用它们:

protected void Page_Load(object sender, EventArgs e)
{
     Response.Write(Account.UserID);
}
它比一直使用Sessionvariables.UserID要短得多


我的2美分

您可以创建一个具有以下属性的基类:

class PageWithProperties
{
    public bool IsLogout { get{ return (bool)Session["IsLogout"] }
                           set { Session["IsLogout"] = value; } }
}

class PageClass : PageWithProperties
{
     void PageClassMethod()
     {
         if(IsLogout)
         {

         }
     }
}

您可以创建具有以下属性的基类:

class PageWithProperties
{
    public bool IsLogout { get{ return (bool)Session["IsLogout"] }
                           set { Session["IsLogout"] = value; } }
}

class PageClass : PageWithProperties
{
     void PageClassMethod()
     {
         if(IsLogout)
         {

         }
     }
}

您可以使用
使用
。然后,您将能够访问变量
SV.IsLogout
,如本例所示:

namespace Foo
{
    using SV = Common.SessionVariables;

    public class Common
    {
        public struct SessionVariables
        {
            public static string IsLogout = "IsLogout";
        }
    }

    public class Example
    {
        public void Bar()
        {
            string test = SV.IsLogout;
        }
    }
}

您可以使用
使用
。然后,您将能够访问变量
SV.IsLogout
,如本例所示:

namespace Foo
{
    using SV = Common.SessionVariables;

    public class Common
    {
        public struct SessionVariables
        {
            public static string IsLogout = "IsLogout";
        }
    }

    public class Example
    {
        public void Bar()
        {
            string test = SV.IsLogout;
        }
    }
}

HttpSessionState
类创建扩展方法

public static class HttpSessionStateExtensions
{
    public static bool IsLoggedOut(this HttpSessionState instance)
    {
        return instance[Common.SessionVariables.IsLogout] == true.ToString();
    }
    public static bool SetIsLoggedOut(this HttpSessionState instance, bool value)
    {
        instance[Common.SessionVariables.IsLogout] = value.ToString();
    }
}
允许您使用(键入和所有内容):


HttpSessionState
类创建扩展方法

public static class HttpSessionStateExtensions
{
    public static bool IsLoggedOut(this HttpSessionState instance)
    {
        return instance[Common.SessionVariables.IsLogout] == true.ToString();
    }
    public static bool SetIsLoggedOut(this HttpSessionState instance, bool value)
    {
        instance[Common.SessionVariables.IsLogout] = value.ToString();
    }
}
允许您使用(键入和所有内容):


长会话变量可以被认为是一件好事!它们描述了它们的用途,并且是自文档化的,这比使用具有任意值的名称(例如s)要好,长会话变量可以被认为是一件好事!它们描述了它们的用途,并且是自我记录的,这比使用具有任意值(如s)的名称要好,您能演示如何为会话分配值吗?您的代码只允许从会话读取。您能演示如何为会话赋值吗?您的代码只允许读取会话。好主意,但我不喜欢使用SV=Common.SessionVariables;必须放在每个代码文件中。可以为project中的所有代码文件进行全局注册吗?@Tomas:不幸的是,不能。但我不认为这是一个交易破坏者,因为a)每个文件只做一次,b)即使你不做,你也可以做很长的路,所以它不依赖于开发人员必须知道的任何特殊技巧,c)这纯粹是opt inI,将你的答案标记为正确,因为这是最短的解决方案,不需要编写任何额外的代码,简单明了。好主意,但我不喜欢使用SV=Common.SessionVariables;必须放在每个代码文件中。可以为project中的所有代码文件进行全局注册吗?@Tomas:不幸的是,不能。但我不认为这是一个交易破坏者,因为a)每个文件只做一次,b)即使你不做,你也可以做很长的路,所以它不依赖于开发人员必须知道的任何特殊技巧,c)这纯粹是opt inI,将你的答案标记为正确,因为它是真正最短的解决方案,不需要编写任何额外的代码,简单明了。