.NET PropertyInfo.SetValue 似乎忽略了我的命令

发布于 2024-11-23 15:20:32 字数 2231 浏览 1 评论 0原文

正如主题所示,我对 PropertyInfo.SetValue 有一些问题。为了说明这一点,这是我的例子 - 我创建了自己的类,它的主要内容是演示对象:

using System;
using System.Reflection;

namespace TestingSetValue
{
public class Link
{
    private object presentationObject = null;
    private string captionInternal = string.Empty;

    public Link (string caption)
    {
        captionInternal = caption;
    }

    public string CaptionInternal
    {
        get { return captionInternal; }
        set { captionInternal = value; }
    }

    public bool Visible
    {
        get 
        {
            if (PresentationObject != null)
            {
                PropertyInfo pi = PresentationObject.GetType().GetProperty("Visible");

                if (pi != null)
                {
                    return Convert.ToBoolean(pi.GetValue(PresentationObject, null));
                }
            }

            return true;
        }

        set
        {
            if (PresentationObject != null)
            {
                PropertyInfo pi = PresentationObject.GetType().GetProperty("Visible");

                if (pi != null)
                {
                    pi.SetValue(PresentationObject, (bool)value, null);
                }
            }
        }
    }

    public object PresentationObject
    {
        get { return presentationObject; }
        set { presentationObject = value; }
    }

}

}

然后,我这样做:

private void btnShowLink_Click(object sender, EventArgs e)
    {
        Link link = new Link("Here I am!");

        this.contextMenu.Items.Clear();
        this.contextMenu.Items.Add(link.CaptionInternal);

        link.PresentationObject = this.contextMenu.Items[0];
        link.Visible = true;

        lblCurrentVisibility.Text = link.Visible.ToString();
    }

现在,我可以想象这看起来不太合乎逻辑/经济,但这显示了我真正问题的本质。也就是说,为什么在我调用之后,演示对象的可见性(以及 link.Visible 的值)没有改变:

link.Visible = true;

我根本不知道还能做什么来完成这项工作......非常感谢任何帮助。

为了使事情变得更有趣,属性 Enabled 的行为符合预期...

PropertyInfo pi = PresentationObject.GetType().GetProperty("Enabled");

这是否与 Visible 实际上是 ToolStripDropDownItem 基础对象的属性,而 Enabled 是 ToolStripDropDownItem 的“直接”属性这一事实有关?

As the topic suggests I have some problems with PropertyInfo.SetValue. To get to the point, here is my example - I have created my own class and the main thing about it is the presentation object:

using System;
using System.Reflection;

namespace TestingSetValue
{
public class Link
{
    private object presentationObject = null;
    private string captionInternal = string.Empty;

    public Link (string caption)
    {
        captionInternal = caption;
    }

    public string CaptionInternal
    {
        get { return captionInternal; }
        set { captionInternal = value; }
    }

    public bool Visible
    {
        get 
        {
            if (PresentationObject != null)
            {
                PropertyInfo pi = PresentationObject.GetType().GetProperty("Visible");

                if (pi != null)
                {
                    return Convert.ToBoolean(pi.GetValue(PresentationObject, null));
                }
            }

            return true;
        }

        set
        {
            if (PresentationObject != null)
            {
                PropertyInfo pi = PresentationObject.GetType().GetProperty("Visible");

                if (pi != null)
                {
                    pi.SetValue(PresentationObject, (bool)value, null);
                }
            }
        }
    }

    public object PresentationObject
    {
        get { return presentationObject; }
        set { presentationObject = value; }
    }

}

}

Then, I do this:

private void btnShowLink_Click(object sender, EventArgs e)
    {
        Link link = new Link("Here I am!");

        this.contextMenu.Items.Clear();
        this.contextMenu.Items.Add(link.CaptionInternal);

        link.PresentationObject = this.contextMenu.Items[0];
        link.Visible = true;

        lblCurrentVisibility.Text = link.Visible.ToString();
    }

Now, I can imagine this doesn't look too logical/ economical, but it shows the essence of my real problem. Namely, why doesn't the visibility of presentation object (and the value of link.Visible) change, after I call:

link.Visible = true;

I simply do not know what else to do to make this work... Any help is deeply appreciated.

To make things even more interesting, the property Enabled behaves as expected of it...

PropertyInfo pi = PresentationObject.GetType().GetProperty("Enabled");

Could it be related to the fact that Visible is actually a property of ToolStripDropDownItem base base object, whereas Enabled is 'direct' property of ToolStripDropDownItem ?

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

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

发布评论

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

评论(2

网白 2024-11-30 15:20:32

如果您预先说明这是什么类,那么会更容易弄清楚它是什么,但现在我们知道它是 ToolStripDropDownItem,我们可以推断它意味着 WinForms。

您所看到的是 ToolStripItem 的 Visible 属性的奇怪之处。它是二传手和getter 不直接绑定在一起。 MSDN 说

“Available 属性与 Visible 属性的不同之处在于
available表示ToolStripItem是否显示,而Visible
指示是否显示 ToolStripItem 及其父项。环境
“Available”或“Visible”为 true 或 false 设置其他属性
真或假。”

换句话说,您想要使用“Available”属性而不是“Visible”属性

It would have been easier to figure this out if you said upfront what class this is but now we know it is ToolStripDropDownItem which we can infer means WinForms.

What you are seeing is an oddity with the ToolStripItem's Visible property. It's setter & getter are not tied directly together. MSDN says

"The Available property is different from the Visible property in that
Available indicates whether the ToolStripItem is shown, while Visible
indicates whether the ToolStripItem and its parent are shown. Setting
either Available or Visible to true or false sets the other property
to true or false."

In other words, you want to use the Available property instead of the Visible property

浪荡不羁 2024-11-30 15:20:32

检查 http://msdn.microsoft.com/ en-us/library/system.web.ui.control.visible.aspx。也许这就是导致你的问题的原因。

有一个非常重要的信息:

如果此属性为 false,则不会呈现服务器控件。在组织页面布局时应考虑到这一点。如果未呈现容器控件,则即使将单个控件的 Visible 属性设置为 true,也不会呈现其包含的任何控件。在这种情况下,即使您已将 Visible 属性显式设置为 true,单个控件也会返回 false。 (也就是说,如果父控件的 Visible 属性设置为 false,子控件将继承该设置,并且该设置优先于任何本地设置。)

Check http://msdn.microsoft.com/en-us/library/system.web.ui.control.visible.aspx. Maybe this is causing your problem.

There is very important piece of info:

If this property is false, the server control is not rendered. You should take this into account when organizing the layout of your page. If a container control is not rendered, any controls that it contains will not be rendered even if you set the Visible property of an individual control to true. In that case, the individual control returns false for the Visible property even if you have explicitly set it to true. (That is, if the Visible property of the parent control is set to false, the child control inherits that setting and the setting takes precedence over any local setting.)

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