来自DLL的依赖对象/属性

时间:2011-11-07 13:54:26

标签: c# dll dependency-properties

这是我的代码:

public class MyObject : DependencyObject
{
    public int speedSimu
    {
        get { return (int)GetValue(speedSimuProperty); }
        set { SetValue(speedSimuProperty, value); }
    }

    public static readonly DependencyProperty speedSimuProperty =
        DependencyProperty.Register("speedSimu", typeof(int), typeof(MyObject), new PropertyMetadata(0));

    public int Value
    {
        get;
        set;
    }
}


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 timer"));

}

public class AnotherClass
{

}

public class Test
{
    MyObject q1 = new MyObject();
    MyObject q2 = new MyObject();
    MyObject q3 = new MyObject();
    MyObject q4 = new MyObject();

    Timer t1 = new Timer();
    Timer t2 = new Timer();
    Timer t3 = new Timer();

    AnotherClass a1 = new AnotherClass();
    AnotherClass a2 = new AnotherClass();
    AnotherClass a3 = new AnotherClass();
}

我从这段代码生成一个DLL,这就是我想做的事情: - 只获取实例(==忽略所有类但“测试”):好的 - 过滤DependencyObjects:好的 - 过滤DependencyProperties:不行 - 访问信息,以便我可以得到我想要的东西(我不想要更多):

Q1   int speedSimu:0 Q2   int speedSimu:0 Q3   int speedSimu:0 Q4   int speedSimu:0

T1   字符串描述:“这是一个计时器” T2   字符串描述:“这是一个计时器”

(注意比!dependecyProperties被忽略)

我目前的守则是:

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

foreach (Type type in types)
{
     var lib = Activator.CreateInstance(type);             
     Type myType = lib.GetType();

     //This filters my Class Test. Not elegant but not my main problem at the moment.
     if (myType.BaseType.ToString() == "System.Object")
     {
         FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Instance);

         foreach (FieldInfo field in fields)
         {
             var fieldtype = field.FieldType;

             if (typeof(DependencyObject).IsAssignableFrom(fieldtype))
             {
                  //Here I am !
             }
         }
     }             
 }

事情是,在这一点上,我认为我接近结束但在我看来像在“FieldInfos”中,关于MyObject和Timer的信息丢失了(我的意思是属性/字段,以及它们的值...... 。)但我不知道如何以不同的方式做到这一点...... 我真的在这里被封锁了,不知道接下来要做什么......

0 个答案:

没有答案