当我不知道类型时,我可以在仅在实例类型上声明的对象上设置属性吗?

发布于 2024-08-10 06:19:48 字数 554 浏览 4 评论 0原文

让我解释一下。我有一个列表,我要在其中添加各种 ASP.NET 控件。 然后,我希望循环遍历列表并设置 CssClass,但并非每个 Control 都支持属性 CssClass。

我想做的是测试底层实例类型是否支持 CssClass 属性并设置它,但我不确定如何在设置属性之前进行转换,因为我不知道每个 Control 对象的类型。

我知道我可以使用 typeof 或 x.GetType(),但我不确定如何使用它们将控件转换回实例类型,以便测试并设置属性。


实际上我似乎已经解决了这个问题,所以我想我会将代码发布在这里供其他人使用。

foreach (Control c in controlList) {
    PropertyInfo pi = c.GetType().GetProperty("CssClass");
    if (pi != null) pi.SetValue(c, "desired_css_class", null);
}

我希望这对其他人有帮助,因为我花了几个小时来研究这两行代码。

干杯

史蒂夫

Let me explain. I have a List into which I am adding various ASP.NET controls.
I then wish to loop through the list and set a CssClass, however not every Control supports the property CssClass.

What I would like to do is test if the underlying instance type supports the CssClass property and set it, but I'm not sure how to do the conversion prior to setting the property since I don't know the type of each Control object.

I know that I can use typeof or x.GetType(), but I'm not sure how to use these to convert the controls back to the instance type in order to test for and then set the property.


Actually I seem to have solved this, so I thought that I would post the code here for others.

foreach (Control c in controlList) {
    PropertyInfo pi = c.GetType().GetProperty("CssClass");
    if (pi != null) pi.SetValue(c, "desired_css_class", null);
}

I hope that this helps someone else as I has taken me hours to research these 2 lines of code.

Cheers

Steve

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

几度春秋 2024-08-17 06:19:48

您可能需要考虑重新设计这些类,使它们实现接口 ICssControl 或提供 CssClass 属性的类似内容。

interface ICssControl

{
字符串 CssClass { 获取;放;}
然后,

您可以将逻辑实现为:

string desiredCssClass = "desired_css_class";
foreach (var control in controlList)
{
    var cssObj = control as ICssControl;
    if (cssObj != null)
        cssObj.CssClass = desiredCssClass;
}

这使您根本不需要使用反射,并且应该使以后的重构变得更加容易。

编辑

在重新阅读您的问题时,也许您已经有了这个界面并询问如何将其转换回该界面?

You may want to think about re-designing these classes making them implement an interface ICssControl or something along the lines which provides the CssClass property.

interface ICssControl

{
string CssClass { get; set;}
}

You can then implement your logic as:

string desiredCssClass = "desired_css_class";
foreach (var control in controlList)
{
    var cssObj = control as ICssControl;
    if (cssObj != null)
        cssObj.CssClass = desiredCssClass;
}

This gets you away from needing to use reflection at all and should make for easier refactoring later on.

Edit

On re-reading you question, maybe you already have this interface and are asking how to cast it back to that interface?

長街聽風 2024-08-17 06:19:48

只是为了遵循普雷特的建议,我也将其发布为答案。

foreach (Control c in controlList) {
    PropertyInfo pi = c.GetType().GetProperty("CssClass");
    if (pi != null) pi.SetValue(c, "desired_css_class", null);
}

我希望这对将来的人有帮助(可能是我当我再次谷歌时)。

Just to follow Preet's advice I have also posted this as an answer.

foreach (Control c in controlList) {
    PropertyInfo pi = c.GetType().GetProperty("CssClass");
    if (pi != null) pi.SetValue(c, "desired_css_class", null);
}

I hope that this helps someone in the future (probably me when I google it again).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文