简体   繁体   中英

Get Usercontrol which inherits Type X using reflection

Given this usercontrol ascx in a web application:

<%@ Control Language="C#" Inherits="TypeX" Codebehind="TypeX.ascx.cs" %>

Is it possible to use reflection to get the usercontrol when I start with:

Type targetType = typeof(TypeX);
... now what? to code to get the usercontrol

I have tried to use:

assembly.GetTypes().Where(t => t.IsSubclassOf(targetType))

but this does not give any result.

Any help appreciated

EXTRA INFO:

The codebehind (simplified) is:

 public partial class TypeX : UserControlBase
{
}

//we use this control:
<%@ Control Language="C#" Inherits="Loadcontrol " Codebehind="Loadcontrol .ascx.cs" %>
//with this codebehind
 public partial class Loadcontrol 
{
    OnPrerender()
    {
        string controlToLoad = "TypeX";
        //what to do here
    }

}

I hope it is more clear now

UPDATE: I have made an example webapplication to show the problem. The download can be found at: WebApplication1.zip or use: Download mirror

Type targetType = typeof(TypeX);
var control = Page.LoadControl(targetType, null);

You need to use this code in a page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM