1

I got the following code to generate a 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
    }

I now would like to ONLY get DependencyObject/Properties. (ie without property "nbSimu" and without object "AnotherClass")

Here is the code I have :

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
   }
}

On the 3rd line I tried :

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

It doesn't say any error, but doesn't filter anything...

And about filtering the DependencyProperties, I just got no idead about how to do it...

Thanks in advance for any help on it, on both problems.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
Guillaume Slashy
  • 3,554
  • 8
  • 43
  • 68

2 Answers2

0

var libs = types.Where(t => t.IsSubclassOf(typeof(DependencyObject))); should work.

Fischermaen
  • 12,238
  • 2
  • 39
  • 56
  • doesnt work :( after filtering, the list is empty To help, in debug I got this for my 2 DependencyObject (when reading from DLL) : BaseType = {Name = "DependencyObject" FullName = "System.Windows.DependencyObject"} And for the last Class I got this : BaseType = {Name = "Object" FullName = "System.Object"} – Guillaume Slashy Oct 28 '11 at 08:42
  • @GuillaumeCogranne: Now, it should work. I've correct the sample. Sorry for the wrong hint at first. – Fischermaen Oct 28 '11 at 08:49
  • That is the problem. Many obect in WPF are a subclass of DependencyObject. So it will be hard to filter that in this way :-( One solution can be do decorate the types you are looking for with a special attribute and to look for that. – Fischermaen Oct 28 '11 at 08:57
  • For the use I have to do, it's OK but the fact is that my DependencyObject are not filtered ! When I read you, I think that you mean that I'll got too much items after filtering but the fact is that, now, me DependencyObject doesn't return "true" in the filter with your codes :( – Guillaume Slashy Oct 28 '11 at 09:02
0

It works on my comp.

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;
    }

What about second problem.. you should keep to naming convention of your properties/fields

Ivan Bianko
  • 1,749
  • 15
  • 22
  • ok, get it for the DependencyProperties, I just thought there were a field that we could use to determine if its a DependencyProperty or a "general property". About filtering the DependencyObject, your code filters the 2nd class ? your filter everything when I try it... the list is empty after the filter operation... – Guillaume Slashy Oct 28 '11 at 09:17
  • I don't know how it can filter everything.. [Look at my snapshot](http://clip2net.com/s/1h2cJ) – Ivan Bianko Oct 28 '11 at 09:32
  • In my case, I got 3 types when I enter my method (QtObject, Timer and AnotherClass) and I got 0 after filtering – Guillaume Slashy Oct 28 '11 at 09:48
  • @GuillaumeCogranne, what version of .net framework do you use? – Ivan Bianko Oct 28 '11 at 09:52
  • @GuillaumeCogranne look at this [question](http://stackoverflow.com/questions/3623358/two-types-not-equal-that-should-be), maybe you have the same problem – Ivan Bianko Oct 28 '11 at 10:16