トピックが示唆するように、私はPropertyInfo.SetValueにいくつかの問題があります。ポイントを取得するには、ここに私の例である - 私は自分自身のクラスを作成しているし、それについての主なものは、プレゼンテーションオブジェクトです:.NET PropertyInfo.SetValueは一見私のコマンドを無視します。
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();
}
:私はこれを行う、その後
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; }
}
}
}
これはあまりにも論理的/経済的ではないと思われますが、実際の問題の本質を示しています。つまり、なぜプレゼンテーションオブジェクトの可視性(およびlink.Visibleの値)の変化は、私が呼び出した後、しません:私は単にこの作品を作るために行うには他に何を知らない
link.Visible = true;
...どれ助けを深く感謝します。
それを予想通り、物事はさらに興味深い性質を有効に振る舞うを作るために...
PropertyInfo pi = PresentationObject.GetType().GetProperty("Enabled");
は、それが有効一方見えるが、実際にToolStripDropDownItemベースベースオブジェクトのプロパティであるという事実に関連している可能性がToolStripDropDownItemの '直接'プロパティですか?
ありがとう、ありがとう、ありがとう!私はちょうど "可視"を "利用可能"に変更しましたが、今はすべてがうまくいきます:)今、真剣に、これを見つけるためにどのキーワードを探しましたか? – NETFrameworkEnthusiast
@NETFrameworkEnthusiast - MSDNページのToolStripItem.Visibleプロパティの定義についてはhttp://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripitem.visible.aspxを参照してコメントを見ました一番下に私はReflectorのこのプロパティを見て、それが標準的な対称的な方法で実装されていないことを確認しました –