簡體   English   中英

獲取使用反射繼承Type X的Usercontrol

[英]Get Usercontrol which inherits Type X using reflection

在Web應用程序中給定此usercontrol ascx:

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

當我開始時是否可以使用反射來獲取用戶控件:

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

我嘗試使用:

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

但這不會產生任何結果。

任何幫助表示贊賞

額外信息:

后面的代碼(簡化)為:

 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
    }

}

我希望現在更清楚

更新:我已經制作了一個示例Web應用程序來顯示該問題。 可以從以下位置找到下載文件: WebApplication1.zip或使用: 下載鏡像

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

您需要在頁面中使用此代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM