Use System.Activator.CreateInstance

本文介绍了一个用于创建IMessageDetail实例的工厂方法CreateMessageDetailHandler。该方法接收一个包含完整命名空间的类名作为参数,并返回相应的IMessageDetail实现类的实例。文章详细展示了如何加载指定的程序集并获取类型,进而通过Activator创建实例。

/// <summary>
  /// 得到给定一个IMessageDetail的实现类的实例的工厂
  /// </summary>
  /// <param name="className">IMessageDetail的实现类的类名,此类名包括完整的域名空间</param>
  /// <returns>IMessageDetail的一个实例</returns>
  public static IMessageDetail CreateMessageDetailHandler(string className)
  {
   try
   {
    string assemblyName = MessageConfig.GetAssemblyLocation() + "/" + GetAssemblyName(className);

    // load assembly
    Assembly targetAssembly = null;

    if(assemblyName != string.Empty)
    {
     targetAssembly = Assembly.LoadFrom(assemblyName + ".dll");
    }

    // create AMessageMgr insatance
    if(className == null || className == string.Empty)
    {
     return null;
    }
    else
    {
     // get class type
     System.Type type;

     try
     {
      if(targetAssembly == null)
      {
       type = System.Type.GetType(className);
      }
      else
      {
       type = targetAssembly.GetType(className);
      }
     }
     catch
     {
      type = null;
     }

     // get AMessageMgr instance
     if(type == null)
     {
      return null;
     }
     else
     {
      object messageHandler = System.Activator.CreateInstance(type);
      return (IMessageDetail)messageHandler;
     }
    }
   }
   catch
   {
    return null;
   }
  }

 
OfficeOpenXml.LicenseContextPropertyObsoleteException HResult=0x80131500 Message=Please use the static 'ExcelPackage.License' property to set the required license information from EPPlus 8 and later versions. For more info see http://epplussoftware.com/developers/licensenotsetexception. Source=EPPlus StackTrace: at OfficeOpenXml.ExcelPackage.set_LicenseContext(Nullable`1 value) at TrainTimetableProcessor.LoadDataFromExcel() in E:\工作\信息管理系统9.26\信息管理系统\Common\TrainTimetableProcessor.cs:line 17 at 信息管理系统.MainWindow.DaoRuTrainTimetableButton_Click(Object sender, RoutedEventArgs e) in E:\工作\信息管理系统9.26\信息管理系统\MainWindow.xaml.cs:line 145 at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e) at System.Windows.Controls.Primitives.ButtonBase.OnClick() at System.Windows.Controls.Button.OnClick() at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e) at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e) at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent) at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e) at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args) at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted) at System.Windows.Input.InputManager.ProcessStagingArea() at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input) at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport) at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel) at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) at System.Windows.Application.RunDispatcher(Object ignore) at System.Windows.Application.RunInternal(Window window) at System.Windows.Application.Run(Window window) at System.Windows.Application.Run() at 信息管理系统.App.Main()
最新发布
10-14
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值