Link to home
Start Free TrialLog in
Avatar of MRS
MRSFlag for United States of America

asked on

Asp.net User Controls and reflection

I am trying to create a plug in type architecture in a asp.net webforms (C#) application.  The idea is to create usercontrols to represent difference views and then allow them to be discover-able via reflection.  I ahve done something similiar in window forms, but reflection does seem to be able to find the user controls.

I have tried implementing this 2 ways.  The first was to create a base abstract class that derived from System.Web.UI.UserControl and then derive my other user controls from that. (below)

public abstract class AccountControl: System.Web.UI.UserControl
{

    public abstract string displayName { get; }
    public abstract int displayOrder { get; }
    public abstract long accountID { get; set; }
}

Open in new window


But when i user reflection to try and find my child controls, the query i would normally use finds nothing?
var pages = System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
                        .Where(t => t.IsSubclassOf(typeof(AccountPage)) && !t.IsAbstract)
                        .Select(t => (AccountPage)Activator.CreateInstance(t));

Open in new window


I have also tried the approach of using a custom attribute on the user controls with the below reflection query, but it also returns nothing.
var Controls = System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
                            .Where(t => t.IsDefined(typeof(AccountAttribute), false))
                            .Select(t => (AccountControl)Activator.CreateInstance(t));

Open in new window


I am starting to think that user controls are not compiled in the same way as normal objects.  What am i missing here?  How can i implement this kind of thing in asp.net webforms?
ASKER CERTIFIED SOLUTION
Avatar of MRS
MRS
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Paul MacDonald
Thank you for posting your solution!