VB.net 2010 视频教程 VB.net 2010 视频教程 VB.net 2010 视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > C#编程 >
  • C#教程之C#动态创建单例类的对象

  • 2019-09-23 14:53 来源:未知

单例类

复制代码
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));
            }
        }
复制代码

-----------------------------------------------------------------------------------------

相关教程