[英]Reflection - Why can't I access this HttpRequest property?
我使用以下方法輸出具有其屬性的對象。 它適用於大多數對象,但在傳遞HttpRequest對象時會拋出。
public static string ConvertToXML(object obj)
{
if (!obj.GetType().IsPrimitive && obj.GetType() != typeof(String) && obj.GetType() != typeof(Decimal))
{
List<string> properties = new List<string>();
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(obj))
{
string name = descriptor.Name;
object value = descriptor.GetValue(obj);
properties.Add(xmlify(name, value));
}
if (properties.Count == 0)
return obj.ToString();
else
return xmlify(obj, string.Concat(properties));
}
else
return obj.ToString();
}
它在這一行引發錯誤:
descriptor.GetValue(obj);
錯誤(對不起,只有德語版本:/):
Der Eigenschaftenaccessor HttpChannelBinding für das System.Web.HttpRequest-Objekt hat folgende Ausnahme verursacht: Die Operation wird auf dieser Plattform nicht unterstützt.
它表示HTTPChannelBinding屬性的Property訪問器不支持此平台上的操作。
這是為什么?
RTFM ;-) MSDN聲明 :
PlatformNotSupportedException
- 當前的HttpWorkerRequest
對象不是System.Web.Hosting.IIS7WorkerRequest
對象或System.Web.Hosting.ISAPIWorkerRequestInProc對象。
您不應該假設讀取一般任何屬性的值都不能拋出異常。
我認為MSDN為您提供了更多信息:
如果當前HttpWorkerRequest對象不是System.Web.Hosting.IIS7WorkerRequest對象或System.Web.Hosting.ISAPIWorkerRequestInProc對象,則拋出PlatformNotSupportedException。
它應該適用於Windows Vista(SP1)/ Windows 7或Windwods 2008 Server(核心除外)。 這可能是你的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.