在VSTO插件中执行后,C#类库实例不会失效 [英] C# class library instances are not destrying after the execution in the VSTO addin

查看:62
本文介绍了在VSTO插件中执行后,C#类库实例不会失效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在c#中创建了一个类库,以便我可以在Microsoft Word VSTO项目中使用。但我有一个问题。 


这是我的类库的一个例子。只需将其称为TestClassLibrary


公共类测试{


      private static Test _instance;


      protected Test(){


            //构造     


     }


      // Singeleton    pattern


    public static Test Instance(){


      if(_instance ==  null){


          _instance = new Test();   


      }


      return _instance;


     }


     公共字符串名称{


            get;


           设置;


     }


}



在我的VSTO加载项 


  public void OnShowButton(Office.IRibbonControl control)

  {

            MessageBox.Show(TestClassLibrary.Test.Name);


             TestClassLibrary.Test.Name =" test add-in";


  }



如果我在加载项中执行  OnShowButton事件,则第一个输出为空消息框。但是,当我第二次执行该事件时,我将消息框设为 "test add-in"。它应该是空消息框。它的样子。所以它看起来像
库实例在执行后没有在加载项中销毁。请问 任何人都对此有所了解?或者我是否需要清除每个实例?


解决方案

Hello nishmj,


似乎Test类是Singleton类,对吧?我想知道代码是如何工作的,Name属性不是静态的,那么我们怎么能直接将它称为TestClassLibrary.Test.Name?


此外,我想知道是否该问题与VSTO项目有关。如果您尝试在一个简单的Windows窗体项目中进行相同的测试,问题是否仍然存在?


最好的问候,


Terry


Hi,

I have created a class library in c# so that I can use in Microsoft Word VSTO project. But I have a problem. 

This is an exmaple for my class library. Just call it as TestClassLibrary

public class Test{

     private static Test _instance;

     protected Test(){

           //constructor     

     }

     //Singeleton   pattern

    public static Test Instance(){

      if(_instance ==  null){

          _instance = new Test();   

      }

      return _instance;

     }

     public string Name{

           get;

           set;

     }

}

in my VSTO add-in 

 public void OnShowButton(Office.IRibbonControl control)
 {
            MessageBox.Show(TestClassLibrary.Test.Name);

            TestClassLibrary.Test.Name = "test add-in";

  }

If I execute the OnShowButton event in the add-in, the first output is empty message box. But when I execut the event second time I got the message box as "test add-in". It should be empty message box. Its look like. So its look like library instance are not destroying in the add-in after execution. Does  any one have an idea about this? Or do I need to clear each instance?

解决方案

Hello nishmj,

It seems that the Test class is a Singleton class, right? I'm wondering how could the code work, the Name property is not static, so how could we call it as TestClassLibrary.Test.Name directly?

Besides, I'm wondering if the issue is related to VSTO project. If you try to do the same test in a simple windows form project, will the issue still exist?

Best Regards,

Terry


这篇关于在VSTO插件中执行后,C#类库实例不会失效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆