Фильтровать DependencyObject и DependencyProperties из DLL / сборки? - PullRequest
1 голос
/ 28 октября 2011

Я получил следующий код для генерации DLL:

public class QtObject : DependencyObject
{
    public int speedSimu
    {
        get { return (int)GetValue(speedSimuProperty); }
        set { SetValue(speedSimuProperty, value); }
    }
    public static readonly DependencyProperty speedSimuProperty =
        DependencyProperty.Register("speedSimu", typeof(int), typeof(QtObject), new PropertyMetadata(0));

    public int rpmSimu
    {
        get { return (int)GetValue(rpmSimuProperty); }
        set { SetValue(rpmSimuProperty, value); }
    }
    public static readonly DependencyProperty rpmSimuProperty =
        DependencyProperty.Register("rpmSimu", typeof(int), typeof(QtObject), new PropertyMetadata(0));

    public int nbSimu;
}

public class Timer : DependencyObject
{
    public string description
    {
        get { return (string)GetValue(descriptionProperty); }
        set { SetValue(descriptionProperty, value); }
    }
    public static readonly DependencyProperty descriptionProperty =
        DependencyProperty.Register("description", typeof(string), typeof(Timer), new PropertyMetadata("This is a time"));

    public bool isActive
    {
        get { return (bool)GetValue(isActiveProperty); }
        set { SetValue(isActiveProperty, value); }
    }
    public static readonly DependencyProperty isActiveProperty =
        DependencyProperty.Register("isActive", typeof(bool), typeof(Timer), new PropertyMetadata(true));
}

public class AnotherClass
    {
        //blaaa
    }

Теперь я хотел бы получить ТОЛЬКО DependencyObject / Properties.(т.е. без свойства "nbSimu" и без объекта "AnotherClass")

Вот код, который у меня есть:

var library = Assembly.LoadFrom(libraryPath);
IEnumerable<Type> types = library.GetTypes();
var libs = types.Where(t => true);

foreach (Type type in libs)
{
    foreach (PropertyInfo property in type.GetProperties())
    {
        //TODO
   }
}

В 3-й строке я попытался:

var libs = types.Where(t => t.BaseType == typeof(DependencyObject));

Он не говорит об ошибке, но ничего не фильтрует ...

А насчет фильтрации DependencyProperties, я просто не понял, как это сделать ...

Заранее спасибо за любую помощь по обеим проблемам.

Ответы [ 2 ]

0 голосов
/ 28 октября 2011

Это работает на моем компе.

public class A : DependencyObject
    {
      public string Name { get; set; }

      public int speedSimu
      {
        get { return (int)GetValue(speedSimuProperty); }
        set { SetValue(speedSimuProperty, value); }
      }
      public static readonly DependencyProperty speedSimuProperty =
          DependencyProperty.Register("speedSimu", typeof(int), typeof(A), new PropertyMetadata(0));

      public int rpmSimu
      {
        get { return (int)GetValue(rpmSimuProperty); }
        set { SetValue(rpmSimuProperty, value); }
      }
      public static readonly DependencyProperty rpmSimuProperty =
          DependencyProperty.Register("rpmSimu", typeof(int), typeof(A), new PropertyMetadata(0));

    }

    public class B : A
    {
      public int Age { get; set; }
    }

    private static void Main(string[] args)
    {
      var assembly = Assembly.GetExecutingAssembly();
      var types = assembly.GetTypes();

      var filterTypes = types.Where(t => typeof (DependencyObject).IsAssignableFrom(t)).ToList(); // A and B

      Func<string, string> mapFieldToProperty =
        field => field.EndsWith("Property") ? field.Remove(field.IndexOf("Property")) : field;


      foreach (var type in filterTypes)
      {
        var depFields =
          type.GetFields(BindingFlags.Public | BindingFlags.Static).Where(
            f => typeof (DependencyProperty).IsAssignableFrom(f.FieldType)).ToList(); // speedSimuProperty and rpmSimuProperty
        var depPropertyNames = depFields.ToLookup(f => mapFieldToProperty(f.Name)); 

        var depProperties =
          type.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(
            prop => depPropertyNames.Contains(prop.Name)).ToList(); // speedSimu and rpmSimu

        foreach (var property in depProperties)
        {
          // TODO
        }

      }
      return;
    }

Как насчет второй проблемы .. Вы должны придерживаться соглашения об именах ваших свойств / полей

0 голосов
/ 28 октября 2011

var libs = types.Where(t => t.IsSubclassOf(typeof(DependencyObject))); должно работать.

...