文章出處
文章列表
https://www.devexpress.com/Support/Center/Question/Details/Q487000/xpodatamodel-and-model-interfaces
代碼來自于上面的網址,這個代碼功能是xpo解析類型信息時,可以有個中轉.
這也成了xaf中dc機制的一個關鍵點,即,接口信息需要有一個真實的對應類型信息才行.
也說是說,接口類型只是一個公開給程序員的信息,ta必須對應一個真實的classinfo,這個classinfo用于xpo解析真實的表\字段\表達式等其他元數據時使用.
public class InterfaceAsForcedAliasHelper { public InterfaceAsForcedAliasHelper() { } public InterfaceAsForcedAliasHelper(ReflectionDictionary dictionary) : this() { Help(dictionary); } public void Help(ReflectionDictionary dictionary) { dictionary.CanGetClassInfoByTypeHandler += new EventHandler<CanGetClassInfoByTypeEventArgs>(canGet); dictionary.ResolveClassInfoByTypeHandler += new EventHandler<ResolveClassInfoByTypeEventArgs>(resolve); } Dictionary<Type, Type> map = new Dictionary<Type, Type>(); public void Map(Type interfaceType, Type actualClassInfoType) { map.Add(interfaceType, actualClassInfoType); } void canGet(object sender, CanGetClassInfoByTypeEventArgs e) { if(e.CanGetClassInfo.HasValue) return; if(map.ContainsKey(e.ClassType)) e.CanGetClassInfo = true; } void resolve(object sender, ResolveClassInfoByTypeEventArgs e) { if(e.ClassInfo != null) return; Type mapType; if(map.TryGetValue(e.ClassType, out mapType)) { if(mapType != null && mapType != e.ClassType) { e.ClassInfo = e.Dictionary.QueryClassInfo(mapType); } } } }
ReflectionDictionary myDictionary = new ReflectionDictionary(); InterfaceAsForcedAliasHelper helper = new InterfaceAsForcedAliasHelper(myDictionary); helper.Map(typeof(IInterfaceAsForcedAliasTestPerson), typeof(InterfaceAsForcedAliasTestPerson)); IDataLayer dl = new SimpleDataLayer(myDictionary, new InMemoryDataStore());
文章列表
全站熱搜