【问题标题】:Is this an Indexed Property, yea or nea?这是索引属性,是的还是nea?
【发布时间】:2011-09-20 20:16:09
【问题描述】:

我正在尝试获取用于加密/解密我的 ViewState 等的当前 machineKey,以尝试调试另一个问题。 (我的应用程序位于服务器场中,并且在每个服务器和应用程序的 machine.config 和 web.config 中设置了机器密钥,因此试图调试某些资源未正确解密的问题。我正在尝试看看哪个用于加密。)这是我的代码sn-p:

Line 1:  Type machineKeySection = typeof(MachineKeySection);
Line 2:  PropertyInfo machineKey = machineKeySection.GetProperty("ValidationKey");
Line 3:  Object validationKey = machineKey.GetValue(machineKeySection, null);
Line 4:  Response.Write(String.Format("Value: {1}", validationKey.ToString()));

照原样,第 3 行抛出“未设置对象实例的对象引用”。这意味着我可能没有正确设置第二个空参数(属性必须被索引,对吧?)。

但是 machineKey 的 ValidationKey 属性的 ParameterInfo 返回长度为零(所以该属性没有被索引,对吧?)。

ParameterInfo[] paramInfo = machineKey.GetIndexParameters();
Response.Write(paramInfo.Length);

http://msdn.microsoft.com/en-us/library/b05d59ty(v=VS.90).aspx

很明显,我在这里忽略了一些东西,并且希望有第二双眼睛来看看这个。有什么建议吗?

【问题讨论】:

    标签: c# reflection .net-3.5 propertyinfo machinekey


    【解决方案1】:

    当您应该传入 MachineKeySection 的实例时,您正在传入 typeof(MachineKeySection)。

    Type machineKeySection = typeof(MachineKeySection);
    Object validationKey = machineKey.GetValue(machineKeySection, null);
    

    需要类似于(取自here):

    MachineKeySection machineKeySection = (MachineKeySection)config.GetSection("system.web/machineKey");
    Object validationKey = machineKey.GetValue(machineKeySection, null);
    

    所以要回答您的问题,不,它不是索引属性。您可以查看文档here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-02
      • 1970-01-01
      • 1970-01-01
      • 2018-10-04
      • 2020-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多