gpt4 book ai didi

c# - Where() with Replace() in Dictionary.Where(p => p.Key is T)

转载 作者:行者123 更新时间:2023-11-30 19:03:50 24 4
gpt4 key购买 nike

我有一个 System.Collections.Generic.Dictionary<System.Web.UI.Control, object>其中所有键都可以是 System.Web.UI.WebControls.HyperLink 的任一类型或类型 System.Web.UI.WebControls.Label .

我要改Text每个控件的属性。因为超链接没有实现(为什么?!)ITextControl ,我需要显式地转换标签或超链接:

Dictionary<Control,object> dic = ..

dic
.Where(p => p.Key is HyperLink)
.ForEach(c => ((HyperLink)c).Text = "something")

dic
.Where(p => p.Key is Label)
.ForEach(c => ((Label)c).Text = "something")

有没有办法解决这种方法?

最佳答案

稍微优雅一些​​,但保留问题:

foreach (HyperLink c in dic.Keys.OfType<HyperLink>())
{
c.Text = "something";
}

foreach (Label c in dic.Keys.OfType<Label>())
{
c.Text = "something";
}

关于c# - Where() with Replace() in Dictionary.Where(p => p.Key is T),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2859517/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com