C# 访问 VSTO 插件中的数据
C# Access Data in VSTO Addin
我正在创建一个 VSTO 插件。我想在 outlook 启动时创建一个字典,然后我可以从 OutlookRibbon class 中的方法访问它。创建这样一个字典的最佳实践或正确方法是什么?我目前有一种方法,在使用它的方法中创建字典,这是非常低效的,因为它每次都被调用。这是代码:
public partial class OutlookRibbon
{
private void OutlookRibbon_Load(object sender, RibbonUIEventArgs e)
{
genMyDict();
}
private void button1_Click(object sender, RibbonControlEventArgs e)
{
Archive();
}
void genMyDict()
{
Dictionary<string, string> myDict= new Dictionary<string, string>();
myDict.Add("@x.com", "x");
// many lines of this
}
void Archive()
{
if (myDict.ContainsKey("@x.com")) { // run code }
}
显然这会引发错误 myDict 在 Archive() 的当前上下文中不存在
我应该如何构造它,使字典只创建一次,但仍然可以从 OutlookRibbon class 中的其他方法访问?我似乎无法让它发挥作用。有没有更好的方法来创建在 VSTO outlook 插件中像这样使用的字典?
myDict does not exist in the current context
更改字典的范围,使其成为 OutlookRibbon
class 的 属性。这会将其范围从方法 genMyDict
的 本地化堆栈 .
扩展开来
public Dictionary<string, string> MyDictionary { get; set; }
void genMyDict()
{
MyDictionary = new Dictionary<string, string>();
MyDictionary.Add("@x.com", "x");
...
}
void Archive()
{
if (MyDictionary.ContainsKey("@x.com")) { // run code }
}
这将允许所有内容访问它。因为范围的改变允许从一个方法访问整个 class.
我正在创建一个 VSTO 插件。我想在 outlook 启动时创建一个字典,然后我可以从 OutlookRibbon class 中的方法访问它。创建这样一个字典的最佳实践或正确方法是什么?我目前有一种方法,在使用它的方法中创建字典,这是非常低效的,因为它每次都被调用。这是代码:
public partial class OutlookRibbon
{
private void OutlookRibbon_Load(object sender, RibbonUIEventArgs e)
{
genMyDict();
}
private void button1_Click(object sender, RibbonControlEventArgs e)
{
Archive();
}
void genMyDict()
{
Dictionary<string, string> myDict= new Dictionary<string, string>();
myDict.Add("@x.com", "x");
// many lines of this
}
void Archive()
{
if (myDict.ContainsKey("@x.com")) { // run code }
}
显然这会引发错误 myDict 在 Archive() 的当前上下文中不存在
我应该如何构造它,使字典只创建一次,但仍然可以从 OutlookRibbon class 中的其他方法访问?我似乎无法让它发挥作用。有没有更好的方法来创建在 VSTO outlook 插件中像这样使用的字典?
myDict does not exist in the current context
更改字典的范围,使其成为 OutlookRibbon
class 的 属性。这会将其范围从方法 genMyDict
的 本地化堆栈 .
public Dictionary<string, string> MyDictionary { get; set; }
void genMyDict()
{
MyDictionary = new Dictionary<string, string>();
MyDictionary.Add("@x.com", "x");
...
}
void Archive()
{
if (MyDictionary.ContainsKey("@x.com")) { // run code }
}
这将允许所有内容访问它。因为范围的改变允许从一个方法访问整个 class.