我想构建一个DLL来将一些数据存储在静态字典变量(pdfDictionary)中,并提供一个方法公共静态字符串GetItemValue(字符串sectionName,字符串itemName)来获取指定值。当我添加DLL作为对控制台应用程序的引用,它运行良好。但在Windows窗体应用程序中,我无法通过使用GetItemValue获得价值。任何帮助都将受到赞赏。
定义词典:
private static Dictionary<string, Dictionary<string, string>> pdfDictionary = new Dictionary<string, Dictionary<string, string>>();
//private static Dictionary<string, Dictionary<string, string>> pdfDictionary ;
public static Dictionary<string, Dictionary<string, string>> PdfDictionary
{
get { return AdapterUtil.pdfDictionary; }
set { AdapterUtil.pdfDictionary = value; }
}
写字典:
public static void WriteDictionary(string pdfFile)
{
string strResult = ExtractSpecifyAreaText(pdfFile, 0, 110, 907, 620);
string strContent = RemoveWaterMark(strResult);
Dictionary<string, Dictionary<string, string>> pdfDict = new Dictionary<string, Dictionary<string, string>>();
// some code
AdapterUtil.pdfDictionary = pdfDict;
}
获取物品价值方法:
public static string GetItemValue(string sectionName, string itemName)
{
switch (sectionName)
{
case "Section2":
if (pdfDictionary.ContainsKey(sectionName))
{
Dictionary<string, string> dic2 = pdfDictionary[sectionName];
if (dic2.ContainsKey(itemName))
{
return dic2[itemName];
}
else
{
return @"No value";
}
}
else
{
return @"No value";
}
//other cases
default:
return @"No value";
}
}
控制台应用程序中的呼叫方法,我可以获取电话号码:
string pdfFilePath = @"D:\test.pdf";
PDFAdapter.AdapterUtil.WriteDictionary(pdfFilePath);
//Get phone number
string keyVal = PDFAdapter.AdapterUtil.GetItemValue(@"Section4", @"Phone Number");
Console.WriteLine("Phone Number is {0}", keyVal);
Console.ReadKey();
Windows窗体应用程序中的调用方法,我得到了什么,但也没有例外:
private void btnBrowseSOF_Click(object sender, EventArgs e)
{
if (this.ofdSOF.ShowDialog() == DialogResult.OK) {
string pdfFilePath = this.ofdSOF.FileName;
this.txtSOF.Text = this.ofdSOF.FileName;
PDFAdapter.AdapterUtil.WriteDictionary(pdfFilePath);
//Get Nothing
string keyVal = PDFAdapter.AdapterUtil.GetItemValue(@"Section4", @"Phone Number");
}
}