单例类
public class SnappingClass : ISnappingEnvironment, IExtension { // private static readonly SnappingClass instance = null; static SnappingClass() { instance = new SnappingClass(); } private SnappingClass() { } public static SnappingClass Instance { get { return instance; } }
动态创建:
void IExtensionManagerAdmin.StartupExtensions(string componentCategory, string jitCategory, ref object initializationData) { var typeInfos = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IExtension)))).ToArray(); foreach (var typeInfo in typeInfos) { //Console.WriteLine(typeInfo.FullName); //IExtension pExtension = typeInfo.Assembly.CreateInstance(v.FullName) as IExtension; //MethodInfo methodInfo = typeInfo.GetMethod("Instance", BindingFlags.NonPublic | BindingFlags.Static, null, new Type[] { }, null); //IExtension pExtension = methodInfo.Invoke(null, null) as IExtension; // PropertyInfo propertyInfo = typeInfo.GetProperty("Instance", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); // //PropertyInfo[] propertyInfos = typeInfo.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); //foreach (PropertyInfo propertyInfo in propertyInfos) //{ // Console.WriteLine(propertyInfo.Name); //} IExtension pExtension = propertyInfo.GetValue(null, null) as IExtension; // pExtension.Startup(ref initializationData); this.pExtensionList.Add(new KeyValuePair<string, IExtension>(pExtension.Name, pExtension)); } }
-----------------------------------------------------------------------------------------