思考 - 为什么我不能访问这个 HttpRequest 属性? [英] Reflection - Why can't I access this HttpRequest property?

查看:63
本文介绍了思考 - 为什么我不能访问这个 HttpRequest 属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下方法输出具有属性的对象.它适用于大多数对象,但在我传递 HttpRequest 对象时抛出.

I am using the following method to output objects with their properties. It works great with most objects, but throws when I pass a HttpRequest object.

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();
}

它在这一行抛出一个错误:

It throws an error in this line:

descriptor.GetValue(obj);

错误(抱歉,只有德语版本:/):

The error (sorry, only have the german version :/):

Der Eigenschaftenaccessor HttpChannelBinding für das System.Web.HttpRequest-Objekt hat folgende Ausnahme verursacht:Die Operation wird auf dieser Plattform nicht unterstützt.

它说 HTTPChannelBinding 属性的属性访问器不支持在此平台上的操作.

It says that the Property accessor for the HTTPChannelBinding property does not support the operation on this platform.

这是为什么?

推荐答案

RTFM ;-) MSDN 声明:

PlatformNotSupportedException — 当前的 HttpWorkerRequest 对象不是 System.Web.Hosting.IIS7WorkerRequest 对象或 System.Web.Hosting.ISAPIWorkerRequestInProc对象.

PlatformNotSupportedException — The current HttpWorkerRequest object is not a System.Web.Hosting.IIS7WorkerRequest object or a System.Web.Hosting.ISAPIWorkerRequestInProc object.

您不应假设读取任何属性的值都不会引发异常.

You should not assume that reading a value of generally any property can't throw an exception.

这篇关于思考 - 为什么我不能访问这个 HttpRequest 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆