DropDownList에서 상속 된 클래스의 Items.add 절차를 과부하시키는 방법이 있습니까?
-
13-09-2020 - |
문제
드롭 다운리스트에서 상속받은 내 자신의 맞춤 클래스가 있습니다.items.add 절차를 재정의 할 수있는 방법이 있습니까?방법?
해결책
이 클래스에서 인터페이스 에이 클래스를 제공하고있는 것처럼이 클래스에서 상속 받고 싶어하는 특정 이유가 있습니까?대답이 아니면 상속에 대한 구성을 정말로 사용해야합니다.그런 다음 문제가 존재하지 않습니다.
다른 팁
기본 기능 추가 기능을 무시할 수 없습니다. ListItemCollection
가 밀봉되어 있지만 ListItemCollection
클래스를 확장하는 확장 메소드를 작성할 수 있습니다.예제는 다음과 같습니다.
namespace Truthseeker.Extensions
{
using System.Web.UI.WebControls;
public static class ListItemCollectionExtensions
{
public static void Add(
this ListItemCollection collection,
ListItem item,
string extraParameter)
{
if (extraParameter.Contains("Add"))
collection.Add(item);
}
}
}
.
다음과 같은 것을 쓸 수 있습니다.
DropDownList ddl = new DropDownList();
ddl.Items.Add(new ListItem("text", "value"), "Add");
. 제휴하지 않습니다 StackOverflow