Accessing static members across processes C#

2019-06-14 10:28发布

问题:

Ok, here is the problem. I have a third party c# lib and im kind of writing a tool about it. So there are a few static Containers that i want to monitor from another application but ofcourse i cant reach them in the domain of my app. A simple example would be :

namespace DefinedInAsembly1
{
     public class Resource
     {
       public static IList<DateTime> DateTimes {get;set;}
     }
}

 namespace DefinedInAssembly2
 {
    class RunningProgram
    {
      static void Main(string[] args)
      {
         while(true)
         {
          Resource.DateTimes.Add(DateTime.Now); 
          Thread.Sleep(10000);
         }
      }
    }
 }

namespace DefinedInAssembly3
{
 class ToolProgram
 {
    static void Main(string[] args)
    {
         //Accessing Resource.DateTimes with the values inserted from RunningProgram
         //Any ideas?
    }

 }
}

回答1:

You need to use any of available on host OS IPC (Inter Process Commnuication) tecniques:

  • Memory Mapped Files
  • Named Pipes
  • IPC Mechanisms in C# - Usage and Best Practices

So applications that have to be listened by someone, should expose themselves via one of these, so another application that would like to sniff, or affect the state of them, can communicate with them via these channels.