2011-07-15 10 views
0

正如主題所示,我在PropertyInfo.SetValue中遇到了一些問題。要到這一點,這是我的例子 - 我創造我自己的類和它的主要的是表現對象:.NET 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; 

我根本不知道該做些什麼,使這項工作...任何幫助深受讚賞。

爲了讓事情更有趣,物業已啓用的行爲與預期的吧...

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

難道是相關的事實,可見實際上是ToolStripDropDownItem基地的基礎對象的屬性,而啓用是ToolStripDropDownItem的'直接'屬性嗎?

回答

1

這本來是容易算出這個如果你事先說過這是什麼類,但現在我們知道它是ToolStripDropDownItem,我們可以推斷它意味着WinForms。

你看到的是ToolStripItem的Visible屬性的一個奇怪之處。它的設定器&吸氣劑不直接連在一起。 MSDN說

「可用的屬性是在 可從Visible屬性不同的指示是否顯示的ToolStripItem,同時可見 表示的ToolStripItem與其父是否顯示。設置 可用或可見的真或false將其他屬性 設置爲true或false。「

換句話說,你要利用現有的屬性,而不是Visible屬性

+0

謝謝,非常感謝,非常感謝!我只是將「可見」改爲「可用」,現在一切正常 - 噢,快樂:)現在,認真搜索哪個關鍵字來找到這個有趣的信息/概念? – NETFrameworkEnthusiast

+0

@NETFrameworkEnthusiast - 我去了MSDN頁面的ToolStripItem.Visible屬性的定義http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripitem.visible.aspx,並看到評論在最底層。我還在Reflector中查看了這個屬性,發現它並沒有以標準的對稱方式實現 –

1

檢查http://msdn.microsoft.com/en-us/library/system.web.ui.control.visible.aspx。也許這是造成你的問題。

有信息的非常重要的一塊:

如果此屬性爲false,服務器控件不呈現。組織頁面佈局時應考慮到這一點。如果未呈現容器控件,則即使將單個控件的Visible屬性設置爲true,也不會呈現其包含的任何控件。在這種情況下,即使您明確將其設置爲true,單個控件也會爲Visible屬性返回false。 (如果父控件的Visible屬性設置爲false時,,子控件繼承設置和設置優先於任何地方設置

+0

感謝的,但我們不會與Web控件在這裏處理。這是很好的舊ToolStripDropDownItem,我不認爲這篇文章是相關的... ... – NETFrameworkEnthusiast

+0

爲了使事情更有趣,屬性Enabled的行爲與預期的一樣... PropertyInfo pi = PresentationObject.GetType()。GetProperty(「Enabled」); 它可能與Visible實際上是ToolStripDropDownItem基本對象的屬性有關,而Enabled是ToolStripDropDownItem的'direct'屬性? – NETFrameworkEnthusiast