从 DLL/程序集中过滤 DependencyObject 和 DependencyProperties?

发布于 2024-12-12 12:43:04 字数 2113 浏览 7 评论 0原文

我得到了以下代码来生成 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
   }
}

在我尝试的第三行:

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

它没有说任何错误,但没有过滤任何内容......

以及关于过滤 DependencyProperties ,我只是不知道该怎么做...

提前感谢您对这两个问题的任何帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆流 2024-12-19 12:43:05

var libs = types.Where(t => t.IsSubclassOf(typeof(DependencyObject))); 应该可以工作。

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

愁杀 2024-12-19 12:43:04

它适用于我的电脑。

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

第二个问题怎么样..您应该遵守属性/字段的命名约定

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文