问题
Here is the code I have :
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();
}
I generate a DLL from this code and here is what I wanna do : - only get instances (== ignore all Classes but "test") : OK - filter DependencyObjects : OK - filter DependencyProperties : not OK - access to informations so I can get EXACTLY what I want (I don't want anything more) :
#q1 int speedSimu : 0 q2 int speedSimu : 0 q3 int speedSimu : 0 q4 int speedSimu : 0
t1 string description : "This is a timer" t2 string description : "This is a timer"
#(Note than !dependecyProperties are ignored)
The Code I have for the moment is :
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 !
}
}
}
}
The thing is that at this point, I think I'm close to the end but it seems to me like in "FieldInfos", informations about MyObject and Timer are lost (I mean properties/fields, and their values...) but I don't know how to do it in a different way... I'm really blocked here and don't know what to do next...
来源:https://stackoverflow.com/questions/8037391/dependency-objects-properties-from-a-dll