我目前还在开发一个在 Compact 和 Full 框架上运行并且是模块化构建的应用程序。
我实现它的方式是它扫描 dll 的位置并枚举每种类型,并查看它们是否定义了“ScreenInfoAttribute”或“WidgetInfoAttribute”,其中包含有关该类的有用信息。
这是一个片段,它包含 3.5 的代码,但那是因为我们最近从 2.0 进行了切换,但原理在 2.0 中有效
public void Analyze(FileInfo file) {
Assembly asm = Assembly.LoadFrom(file.FullName);
List<Data.AnyPlugin> types = GetPluginTypes(asm.GetTypes());
if (types.Count > 0) {
types.ForEach(x => x.AssemblyPath = file.FullName);
if (_plugins.ContainsKey(file.FullName)) {
_plugins[file.FullName].Plugins.AddRange(types);
} else {
AssemblyPlugin asp = new AssemblyPlugin();
asp.Ass = asm;
asp.Plugins = types;
_plugins.Add(file.FullName, asp);
}
}
}
private List<Data.AnyPlugin> GetPluginTypes(Type[] types) {
List<Data.AnyPlugin> returnTypes = new List<AnyPlugin>();
foreach (Type t in types) {
Data.AnyPlugin st = GetPluginType(t);
if (st != null) returnTypes.Add(st);
}
return returnTypes;
}
private Data.AnyPlugin GetPluginType(Type type) {
if (type.IsSubclassOf(typeof(Screens.bScreen<T>))) {
Screens.ScreenInfoAttribute s = GetScreenAttrib(type);
if (s != null) {
return new Data.ScreenPlugin("", type, s);
}
} else if (type.IsSubclassOf(typeof(Widgets.bWidget<T>))) {
Widgets.WidgetInfoAttribute w = GetWidgetAttrib(type);
if (w != null) return new Data.WidgetPlugin("", type, w);
}
return null;
}
private Screens.ScreenInfoAttribute GetScreenAttrib(Type t) {
Attribute a = Attribute.GetCustomAttribute(t, typeof(Screens.ScreenInfoAttribute));
return (Screens.ScreenInfoAttribute)a;
}