我正在玩字典和字典中新的花哨的 4.0 动态类型。
我有一本字典:
Dictionary<dynamic, dynamic> dynamicDic
我这样填充它:
dynamicDic.Add("First", new Class1());
dynamicDic.Add("Second", new Class2());
为了测试/练习 Class1 和 Class2 非常简单:
public class Class1
{
public string Element { get; set; }
public List<Class2> Class2 { get; set; }
}
public class Class2
{
public string Property { get; set; }
public string Field;
}
我创建了另外两个映射 class1 和 class2 的类,它们实际上是相同的,因此 ClassMap1 和 ClassMap2。我将只包括 CalssMap1:
public class ClassMap1: BaseClassMap1
{
public ClassMap1()
{
var r = new Class1();
Children = new Dictionary<string, dynamic>
{
{"Element", r.GetType().GetProperty("Element")},
{"Class1", r.GetType().GetProperty("Class1")}
};
Name = "Root";
ObjectType = typeof (Class1);
Parent = "RootElement";
HasParent = false;
HasChildren = true;
IsClass = r.GetType().IsClass;
}
}
我创建了一个基类: BaseClass1()
public class BaseClass1
{
private String _Name;
public String Name
{
get { return _Name; }
set { _Name = value; }
}
private Type _ObjectType;
public Type ObjectType
{
get { return _ObjectType; }
set { _ObjectType = value; }
}
private String _Parent;
public String Parent
{
get { return _Parent; }
set { _Parent = value; }
}
private Dictionary<string, dynamic> _Children;
public Dictionary<string, dynamic> Children
{
get { return _Children; }
set { _Children = value; }
}
private bool _HasParent;
public bool HasParent
{
get { return _HasParent; }
set { _HasParent = value; }
}
private bool _HasChildren;
public bool HasChildren
{
get { return _HasChildren; }
set { _HasChildren = value; }
}
private bool _IsClass;
public bool IsClass
{
get { return _IsClass; }
set { _IsClass = value; }
}
}
我用数据填充类,什么数据并不重要:)
然而,当我尝试通过 Linq 语句访问这些值时:
var a = _classObjects.SingleOrDefault(x => x.Key == node.Name).Value;
a 只给我:-
a.Equals(), a.GetType(), a.GetEnumerator() or a.ToString()
我希望能够让它这样做(使用智能感知)......
a.Children
a.Name
a.HasParent
ETC...
有人知道我哪里出错了吗?
哎呀完全错了......对不起:| 以上编辑...