我有一個WPF控件是從一個通用的基礎派生。 例如:來自UpDownBase的NumericUpDown的來自CommonNumericUpDown的IntegerUpDown。如何在只有一個「對象」的情況下從泛型派生類實例中檢索靜態屬性?
UpDownBase具有靜態屬性聲明爲:
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(T), typeof(UpDownBase<T>), new FrameworkPropertyMetadata(default(T), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnValueChanged, OnCoerceValue, false, UpdateSourceTrigger.LostFocus));
我收到以下異常: TypeLoadException(GenericArguments [0], 'System.Nullable 1[T]', on 'System.Nullable
1 [T]' 違反類型參數的約束' T」)
當我嘗試(異常發生時,‘動態’是試圖讓‘ValueProperty’):
var frameWorkElement = e.OriginalSource as FrameworkElement;
while (frameWorkElement != null)
{
if (IsSubclassOfRawGeneric(typeof(UpDownBase<>), frameWorkElement.GetType())) // JaredPar StackOverfLow code
{
dynamic upDownT = frameWorkElement;
DependencyProperty dp = upDownT.ValueProperty; // TypeLoadException
if (dp != null)
{
be = frameWorkElement.GetBindingExpression(dp);
if (be != null)
{
be.UpdateSource();
}
}
break;
}
frameWorkElement = VisualTreeHelper.GetParent(frameWorkElement) as FrameworkElement;
}
爲什麼我收到異常以及如何ç我以通用的方式從我的對象中獲取適當的dependencyProperty(沒有切換案例或類似的)?
更新
我修改JaredPar代碼來獲取類型:
private static Type GetGenericBaseTypeOfType(Type generic, Type toCheck)
{
while (toCheck != null && toCheck != typeof(object))
{
var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
if (generic == cur)
{
return generic;
}
toCheck = toCheck.BaseType;
}
return null;
}
不過,我仍然不能得到的PropertyInfo查詢其價值。
Type genericbaseType = GetGenericBaseTypeOfType(typeof (UpDownBase<>), frameWorkElement.GetType()); // JaredPar StackOverfLow code
{
if (genericbaseType != null)
{
//UpDownBase<int>.ValueProperty ok
// pi3 is null
PropertyInfo pi3 = frameWorkElement.GetType().GetProperty("ValueProperty", BindingFlags.Static | BindingFlags.FlattenHierarchy | BindingFlags.Public);
// pi is null
PropertyInfo pi = genericbaseType.GetProperty("ValueProperty", BindingFlags.Static | BindingFlags.Public);
更新2
我接受瀆聖解決方案,因爲他解決了我最大的問題。我也認爲做我想做的事根本是不可能的。但不要試圖讓它工作,因爲它不。我無法從從maked GenericType,而且從任何普通的我覺得拿到的DependencyProperty的綁定表達式,請參閱下面的代碼和註釋:
Type upDownType = typeof(UpDownBase<>).MakeGenericType(frameWorkElement.GetType().BaseType.GenericTypeArguments[0]);
FieldInfo fi = upDownType.GetField("ValueProperty");
if (fi != null)
{
var dp = fi.GetValue(null) as DependencyProperty;
if (dp != null)
{
be = frameWorkElement.GetBindingExpression(dp); // Null
if (be != null)
{
be.UpdateSource();
}
else
{
// Next line be= null
be = frameWorkElement.GetBindingExpression(UpDownBase<int>.ValueProperty); // Null
// Next line be = correct binding expression
be = frameWorkElement.GetBindingExpression(IntegerUpDown.ValueProperty); // Ok valid bindingExpression
}
哪裏例外是從哪裏來的? – SLaks
嗨SLaks,它是寫在代碼評論。我會更新我的問題以更準確。謝謝! –
什麼是堆棧跟蹤? – SLaks