5

次のように Web コントロールを作成したとします。

public class TestControl<T> : WebControl
{
    ...
}

コードを使用せずにそのコントロールを .aspx ページに配置する方法はありますか? 私は本当に次のようなことができるようになりたいです:

<controls:TestControl<int> runat="server" />

しかし、私が知る限り、ジェネリック パラメータを渡す方法はありません。私はウェブで検索してみましたが、これを見つけましたhttp://forums.asp.net/t/1309629.aspx、これはまさに私が求めているもののようですが、誰もその男が何を望んでいるのかを理解していないようで、 StackOverflow で同様のものを見つけることができません。

4

3 に答える 3

5

いいえ、それを行う方法はありません。

于 2013-05-01T10:29:25.447 に答える
0

ジェネリック型を抽象化し、具体的な型を継承してページに配置できます。一方で、これはもう少しコードが多くなりますが、基本コンストラクターを呼び出すことで型をカスタマイズすることもできます。

public abstract class MyGenericControl<T> : WebControl {
   ...

   public T SomeStronglyTypedProperty { get; set; }

   protected MyGenericControl(...) {
      ...
   }

   ...
}

public sealed class MyConcreteControl : MyGenericControl<SomeType> {
   public MyConcreteControl()
   : base(
      ...
   ) {
   }
}

あなたのマークアップで:

<%@ Page ... %>
<%@ Register assembly="MyAssembly" namespace="MyNamespace" tagPrefix="abc" %>
<asp:Content ...>
   <abc:MyConcreteControl id="myConcreteControl" runat="server" />
</asp:Content>

そして、コードビハインドで:

...
SomeType value = GetAValue();
myConcreteControl.SomeStronglyTypedProperty = value;
...
于 2015-03-06T18:12:05.097 に答える