我会把我的回应置于评论中,因为我还没有答案,但我没有50个代表,所以我不能评论。
这是处理用户控件的一种非常有趣的方式。 我对你的问题是,你想用这个完成什么?我们需要知道,因为您的解决方案可能不涉及具有接受任何类型的用户控件 - 就像Stilgar所说。
你也许可以用一个枚举/属性组合来完成这个..这是我想出来的:
<uc1:MyGenericControl runat="server" id="MyGenericControl1" myControlType="DropDownList" />
public partial class MyGenericControl<T> : System.Web.UI.UserControl
{
public enum ucType : ushort
{
DropDownList = 1,
TextBox,
Button,
Etc
}
private ucType _controlType;
public ucType myControlType
{
get{return _controlType;}
set{ T = value; } /* Somehow set T to the value set in ASP.
In this example, value would be "1" so it would throw an error, but you get the idea. */
}
}
这更提示和发人深省的不是一个完整的答案,因为我不不知道是否可以动态设置类的类型(尤其是当前正在使用的类类型)。有一个Convert.ChangeType
方法,但我不认为这会在这里工作。
[Syntax for generic(ie)web user control](http://stackoverflow.com/questions/16316469/syntax-for-generic-iet-web-user-control) –
MethodMan
2014-09-22 15:43:28
因为我没有认为这是可能的,也许你可以描述你的用例,所以我们可以建议最好的解决方法。 – Stilgar 2014-09-22 15:44:44