0
爲WPF綁定問題,我試圖找出我怎樣才能在蘭巴表達式被傳遞給一個對象實例的引用。如何獲取通過lambda表達式傳遞的對象實例的引用?
我已經創建了新的類別「CustomBinding」」 thta可以採取一個lambda表達式作爲參數,從中提取的源和路徑。這種運作良好,如果是 正在傳遞的拉姆達有它的參考同一呼叫者類中的屬性:
public class CustomBinding : Binding
{
public CustomBinding(Expression<Func<object>> expr) : base()
{
Source = (expr.Compile().Target as Closure).Constants[0];
Path = (expr.Body as MemberExpression).Member.Name;
}
}
public class A
{
public string MyProperty { get;set;}
public void MyMethod()
{
var workingbinding = new CustomBinding(()=>MyProperty);
.....
}
}
但是,如果lambda表達式使用另一個對象的屬性是什麼。我如何獲得該對象的引用?
像這樣:
public class B
{
public void MyOtherMethod()
{
var otherObject = new A();
var nonworkingbinding = new CustomBinding(() => otherObject.MyProperty)
//this fails, the Source is not set correct
}
}
我怎樣才能提取lambda表達式的 「otherObject」 實例的引用?
如果有什麼在使用中沒有對象?調用'CustomBinding'就像這樣'CustomBinding(()=> new object())'是合法的。在這種情況下,你會期望'引用傳遞對象'嗎? – Michael