【问题标题】:List all session variables including nested nodes列出所有会话变量,包括嵌套节点
【发布时间】:2013-05-07 23:00:09
【问题描述】:

我过得很糟糕,我知道如何用 6 种其他语言做到这一点,但无法让它发挥作用

我想查看 .NET 应用程序中的所有会话变量,包括嵌套节点

我使用了这里的代码List all session info

我能够使用

获得所有会话变量的第一级
<% 
for (int i = 0; i < Session.Count; i++)
{
    var crntSession = Session.Keys[i];
    Response.Write(string.Concat(crntSession, "=", Session[crntSession]) + "<br />");
}
%>

这是我输出的一部分

Mode=M
TreeRefresh=
AdvUser=TheName.WebFramework.Security.AdvanceUser

如何遍历 AdvUser 并获取其值?

我试过这个,但它返回错误 CS1061: 'object' does not contain a definition for 'Count'

for (int i = 0; i < Session["AdvUser"].Count; i++)

我也试过了,报错了

CS1579:foreach 语句不能对类型变量进行操作 'System.Type' 因为 'System.Type' 不包含公共 'GetEnumerator' 的定义

foreach (var crntSession in Session["AdvUser"].GetType())

我只是不知道如何获取该嵌套节点的值,它不必在 C# 中可以在 VB 中

【问题讨论】:

  • 所以您基本上是想枚举会话中N 的类型数量并获取它们的属性?
  • 是的,这将是我的基本问题,以及关于为什么我尝试静态获取该嵌套节点的任何知识都不起作用

标签: c# asp.net session


【解决方案1】:

您尝试做的事情实际上没有意义,因为 AdvUser 似乎是 TheName.WebFramework.Security.AdvanceUser 类的一个实例。这个类有属性,但除非你使用反射,否则你不能真正像数组一样循环它们。

类似这样的:http://msdn.microsoft.com/en-us/library/k2w5ey1e.aspx

MyClass MyObject = new MyClass();
     MemberInfo [] myMemberInfo; 

     // Get the type of the class 'MyClass'.
     Type myType = MyObject.GetType(); 

     // Get the public instance members of the class 'MyClass'. 
     myMemberInfo = myType.GetMembers(BindingFlags.Public|BindingFlags.Instance);

     Console.WriteLine( "\nThe public instance members of class '{0}' are : \n", myType); 
     for (int i =0 ; i < myMemberInfo.Length ; i++)
     {
        // Display name and type of the member of 'MyClass'.
        Console.WriteLine( "'{0}' is a {1}", myMemberInfo[i].Name, myMemberInfo[i].MemberType);
     }

【讨论】:

  • 所以我可以像这样直接打印会话的节点(因为我知道它存在) ,那仍然适用你的答案,还是改变它?
猜你喜欢
  • 2014-02-03
  • 1970-01-01
  • 1970-01-01
  • 2017-07-04
  • 2020-12-20
  • 2014-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多