How to get a static property from an instance of a generic derived class with only an “object” in hand?

I have a WPF control that comes from shared databases. For example: IntegerUpDown from CommonNumericUpDown from NumericUpDown from UpDownBase.

UpDownBase has a static property declared as:

public static readonly DependencyProperty ValueProperty = DependencyProperty.Register( "Value", typeof( T ), typeof( UpDownBase<T> ), new FrameworkPropertyMetadata( default( T ), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnValueChanged, OnCoerceValue, false, UpdateSourceTrigger.LostFocus ) );

I get the following exception: TypeLoadException (GenericArguments [0], 'System.Nullable 1[T]', on 'System.Nullable1 [T]' violates the restriction of the type parameter 'T'.)
When I try to do this (exception occurs when "dynamic" tries to get "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;
}

Why am I getting this exception and how can I get the corresponding dependencyProperty from my object in general (without wiring closets or the like)?

Update

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

Sacrilege, . , , . , . dependencyProperty, maked GenericType, , , , , . :

    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
            }
+3
2

, . (, ). , : "FlattenHierarchy".

:

    var frameWorkElement = e.OriginalSource as FrameworkElement;
    while (frameWorkElement != null)
    {
        Type genericbaseType = GetGenericBaseTypeOfType(typeof(UpDownBase<>), frameWorkElement.GetType()); // JaredPar StackOverfLow code
        {
            if (genericbaseType != null)
            {
                FieldInfo fi = frameWorkElement.GetType().GetField("ValueProperty", BindingFlags.GetField | BindingFlags.Default | BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.SetField);
                if (fi != null)
                {
                    var dp = fi.GetValue(null) as DependencyProperty;
                    if (dp != null)
                    {
                        be = frameWorkElement.GetBindingExpression(dp); // Null if done like the other answer.
                        if (be != null)
                        {
                            be.UpdateSource();
                        }
                    }
                }
                break;
            }
        }

        frameWorkElement = VisualTreeHelper.GetParent(frameWorkElement) as FrameworkElement;
0

. #.

, T , UpDownBase<T>.ValueProperty upDownT.ValueProperty. , T, .

, dp, T .

Type upDownType = typeof(UpDownBase<>).MakeGenericType(upDownT.GetType().BaseType.GenericTypeArguments[0]);
DependencyProperty dp = (DependencyProperty)upDownType.GetField("ValueProperty").GetValue(null);

3

, . depedndecy , :

DependencyProperty dp = (DependencyProperty)frameWorkElement.GetType().GetField("ValueProperty").GetValue(null);
+2

All Articles