我在ASP.net中有一些会话如下。在文件Test.aspx.cs中有一些会话
public class MySession
{
// private constructor
private MySession()
{
value_group = "Group";
//value_id = "Id";
//value_region = "Region";
}
// Gets the current session.
public static MySession Current
{
get
{
MySession session =(MySession)HttpContext.Current.Session["Group"];
if (session == null)
{
session = new MySession();
HttpContext.Current.Session["Group"] = session;
}
return session;
}
}
// **** add your session properties here, e.g like this:
public string value_group { get; set; }
//public string value_id { get; set; }
//public string value_region { get; set; }
}
然后我创建了一些课程,我想获得会话的价值。
首先,我创建了一个MySession.cs类
string value_group = MySession.Current.value_group.ToString();
//string value_id = MySession.Current.value_id.ToString();
//string value_region = MySession.Current.value_region.ToString();
其次,我创建了类Test.cs,我希望获得会话值
Object reference not set to an instance of an object
但是它得到了错误
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
--use IEEE.NUMERIC_STD.ALL;
-- Uncomment the following library declaration if instantiating
-- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;
entity ALU_8_bit is
Port ( A : in STD_LOGIC_VECTOR (3 downto 0);
B : in STD_LOGIC_VECTOR (3 downto 0);
CTRL : in STD_LOGIC_VECTOR (2 downto 0);
Y : out STD_LOGIC_VECTOR (3 downto 0);
OFL: out STD_LOGIC;
COUT: out STD_LOGIC
);
end ALU_8_bit;
architecture Behavioral of ALU_8_bit is
signal result: std_logic_vector(3 downto 0);
begin
process(A,B,CTRL) begin
if (CTRL ="000") then
result <= A;
elsif (CTRL = "001") then
result <= A + B;
elsif (CTRL = "010") then
result <= A - B;
elsif (CTRL = "011") then
result <= NOT A + 1;
elsif (CTRL = "100") then
result <= NOT A;
elsif (CTRL = "101") then
result <= A AND B;
elsif (CTRL = "110") then
result <= A OR B;
else --(CTRL = "111") then
result <= A XOR B;
end if;
end process;
Y <= result;
--process (A,B,result,cout)
--begin
-- if ((A(3) = '1') and (B(3) ='1') and (result(3) = '0')) then Cout <= '1';
-- elsif ((A(3) = '1') and (B(3) = '0') and (result(3) = '0')) then Cout <= '1';
-- elsif ((A(3) = '0') and (B(3) = '1') and (result(3) = '0')) then Cout <= '1';
-- else Cout <= '0';
-- end if;
--end process;
process(result,CTRL)
begin
if (CTRL = "001") then
--Y <= result;
COUT <= A(3) and B(3); --result(3);
OFL <= result(3) xor (B(2) and A(2));
else
OFL <= '0';
COUT <= '0';
end if;
end process;
end Behavioral;
谢谢你们。
答案 0 :(得分:0)
正如您在评论中所说,您需要一个适用于Session
的包装类。
以下是如何轻松实现它的方法。我试着遵循你最多写的代码:
public class MySession
{
private const string GroupKey = "Group";
private const string RegionKey = "Region";
private const string IDKey = "Id";
private static MySession _session;
public static MySession Current
{
get
{
return _session ?? (_session = new MySession());
}
}
public string Group
{
get
{
return HttpContext.Current.Session[GroupKey] as string;
}
set
{
HttpContext.Current.Session[GroupKey] = value;
}
}
public string Region
{
get
{
return HttpContext.Current.Session[RegionKey] as string;
}
set
{
HttpContext.Current.Session[RegionKey] = value;
}
}
public string ID
{
get
{
return HttpContext.Current.Session[IDKey] as string;
}
set
{
HttpContext.Current.Session[IDKey] = value;
}
}
}
但是,您可能不需要它。您只使用一个当前HttpContext.Current.Session
。这些值会动态更改,我猜想,您想获取实际数据。因此,您的包装类可以是静态包装类。
public static class SessionWrapper
{
private const string GroupKey = "Group";
private const string RegionKey = "Region";
private const string IDKey = "Id";
public static string Group
{
get
{
return HttpContext.Current.Session[GroupKey] as string;
}
set
{
HttpContext.Current.Session[GroupKey] = value;
}
}
public static string Region
{
get
{
return HttpContext.Current.Session[RegionKey] as string;
}
set
{
HttpContext.Current.Session[RegionKey] = value;
}
}
public static string ID
{
get
{
return HttpContext.Current.Session[IDKey] as string;
}
set
{
HttpContext.Current.Session[IDKey] = value;
}
}
}
此外,它不正确。我不会这样做,因为你要为每个会话属性描述一个键,getter和setter。如果您在会话中添加新属性(例如,&#39;语言&#39;),您将需要显着更改违反OCP原则的MySession
类。