简体   繁体   English

项目之间的类中的对象持久性

[英]Object persistence in class between projects

I know I shouldn't have to ask this but whatever it is I am missing is driving me nuts! 我知道我不必问这个,但是我所缺少的一切都让我发疯! I have done this many times before and I can only put it down to old age and slight senility. 我以前做过很多次,只能说到年老和衰老。

I have a class with two objects that get initialized in the constructor... 我有一个带有两个在构造函数中初始化的对象的类...

public class EbayFunctions
{
    private static ApiContext apiContext = null;
    private static List<StoreCategoriesFlattened> storeCategories =  new List<StoreCategoriesFlattened>();

    public EbayFunctions()
    {
        ApiContext apiContext = GetApiContext();
        List<StoreCategoriesFlattened> storeCategories = GetFlattenedStoreCategories();
    }
    public string GetStoreCategoryIdForItem(string category)
    {
       var result = storeCategories.Find(x => x.CCDatabaseMatch == category);
       return ""; //Ignore will return a value
    }
}

then I have a forms app (test harness) that makes use of the class and on button click I call a method... 然后我有一个利用该类的表单应用程序(测试工具),然后单击按钮,我调用一个方法...

namespace EbayTestHarness
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void cmdGetEbayStoreCatID_Click(object sender, EventArgs e)
        {
            EbayFunctions ebf = new EbayFunctions();
            string ddd = ebf.GetStoreCategoryIdForItem("Motors > Bikes");

        }
    }
}

However apiContext persists between calls but storeCategories gets populated on EbayFunctions ebf = new EbayFunctions(); 但是apiContextapiContext调用之间仍然存在,但是storeCategoriesEbayFunctions ebf = new EbayFunctions();EbayFunctions ebf = new EbayFunctions(); and is null when string ddd = ebf.GetStoreCategoryIdForItem("Motors > Bikes"); string ddd = ebf.GetStoreCategoryIdForItem("Motors > Bikes");时为null string ddd = ebf.GetStoreCategoryIdForItem("Motors > Bikes"); is called. 叫做。

I know its something stupid but what am I missing? 我知道它有些愚蠢,但是我想念的是什么?

Your problem is here: 您的问题在这里:

private static ApiContext apiContext = null;
private static List<StoreCategoriesFlattened> storeCategories =  new List<StoreCategoriesFlattened>();

public EbayFunctions()
{
    ApiContext apiContext = GetApiContext();  // local!!
    List<StoreCategoriesFlattened> storeCategories = GetFlattenedStoreCategories();  // local!!
}

You're not setting the static fields - you're introducing local variables that then go out of scope and are (eventually) garbage collected. 您不是在设置静态字段,而是在引入局部变量 ,然后这些变量超出范围并(最终)被垃圾回收。 Take out the type indicators to set the static fields: 取出类型指示器以设置静态字段:

public EbayFunctions()
{
    apiContext = GetApiContext();
    storeCategories = GetFlattenedStoreCategories();
}

Also, as @PatrickHofman points out, the initialization of static members should be done once - preferably in a static constructor: 另外,正如@PatrickHofman指出的那样,静态成员的初始化应该执行一次-最好在静态构造函数中进行:

static EbayFunctions()
{
    apiContext = GetApiContext();
    storeCategories = GetFlattenedStoreCategories();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM