Using GetValue() to access PropertyInfo values Options
VirtualRichard
Posted: Monday, October 06, 2008 2:04:48 PM

Rank: Fanatic

Joined: 9/17/2007
Posts: 265
Location: London, UK.
I know this is really a C# question but I'm thinking outside of the context of Umbraco, others might not get what I am doing.

I've almost finished a custom control called Mobile Browser Detection. It's very powerful but I've run into a problem with getting values of properties using reflection.

Here is my module:

Code:
using System;
using System.Web;
using Marg.Wurfl;

namespace MobileBrowserDetection
{
    class MobileBrowserModule : IHttpModule
    {
        public String ModuleName
        {
            get
            {
                return "MobileBrowserModule";
            }
        }
        public void Init(HttpApplication application)
        {
            application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
        }
        private void Application_BeginRequest(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;

            HttpContext context = application.Context;

            WurflMobileCapabilities capabilities = new WurflMobileCapabilities(context);

            context.Request.Browser = capabilities;
        }
        public void Dispose()
        {
            // Nothing to Dispose.
        }
    }
}

Here is my component:

Code:
using System;
using System.Collections;
using System.Reflection;
using System.Web;
using System.Web.Mobile;
using System.Web.UI;
using System.Web.UI.WebControls;
using Marg.Wurfl;

namespace MobileBrowserDetection
{
    class MobileBrowser : WebControl
    {
        private string browserCapabilityTests;
        private string output;

        protected override void Render(HtmlTextWriter output)
        {
            try
            {
                if (this.browserCapabilityTests != "")
                {
                    string propertyMatchValues = BrowserCapabilityTests;

                    string[] keyValues = propertyMatchValues.Split(';');

                    Hashtable keyValueHashTable = new Hashtable();
                   
                    foreach (string keyValuePair in keyValues)
                    {
                        string[] keyValue = keyValuePair.Split('=');

                        keyValueHashTable.Add(keyValue[0].Trim(), keyValue[1].Trim());
                    }
                    bool allPropertiesMatched = false;

                    PropertyInfo[] browserCapabilities = this.Page.Request.Browser.GetType().GetProperties();

                    foreach (PropertyInfo browserCapability in browserCapabilities)
                    {
                        output.Write(browserCapability.Name + " = " + browserCapability.GetValue(this.Page.Request.Browser, null) + "<br>");
                        /*
                         * This can very probably be collapsed somewhat!
                        if (keyValueHashTable.Contains(browserCapability.Name))
                        {
                            if (value matches)
                            {
                                allPropertiesMatched = true;
                            }
                            else
                            {
                                allPropertiesMatched = false;
                            }
                        }
                        else
                        {
                            allPropertiesMatched = false;
                        }
                        */
                    }
                    if (allPropertiesMatched)
                    {
                        output.Write(this.output.Replace("&amp;", "&").Replace("&quot;", "\"").Replace("&lt;", "<").Replace("&gt;", ">"));
                    }
                }
            }
            catch (Exception exception)
            {
                output.Write(exception.Message);
            }
        }
        public string Output
        {
            set
            {
                if ((value != null) && (value != ""))
                {
                    this.output = value;
                }
            }
        }
        public string BrowserCapabilityTests
        {
            get
            {
                return browserCapabilityTests;
            }
            set
            {
                browserCapabilityTests = value;
            }
        }
    }
}

Hopefully it's obvious that while I can get to the name, I can't get to a specific value for that name. It's very frustrating as this is the final step apart from comparing these values to any match in keyValueHashTable.

In the example above GetValue() results in an error message "Parameter count mismatch". Examples I see via Google don't anticipate that the object I want to refer (and an exotic one at that) is in the module and not in my class .

Can anyone help me?

Richard

2 * 3 * 3 * 37 : The prime factorisation of The Beast.
Users browsing this topic
Guest


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.