How to access Application property in VSTO Outlook add-in outside of ThisAddIn class?

后端 未结 2 838
半阙折子戏
半阙折子戏 2021-01-04 01:07

ThisAddIn class created with new Outlook VSTO C# project has a Application property that you can use to among other things get access to Outlook folders and ite

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-04 01:20

    It is actually bad practice to place static variables in the ThisAddIn in order to reference from around your code.

    According to this answer https://stackoverflow.com/a/46493968/2068626, the Outlook Application object is a singleton so it would be preferred to do your own second suggestion

    var app = new Outlook.Application();
    

    Since this is a Outlook VSTO add-in there is zero risk of starting Outlook since it by definition will be running. If you use this method from within another Office application you would start Outlook only if Outlook is not currently running.

    For completeness sake, all other shared models should use a form of Dependency Injection so there is no strong coupling in your code.

    Using these two approaches it would also be easier (possible) for you to refactor your code and other classes out into a separate library for easier reuse across your VSTO projects or even a Nuget Package.

提交回复
热议问题