Reputation: 207
In my C# application I am loading several assemblies into one Application. In order to quickly exchange objects between the assemblies, I am looking for an existing class in the commonly available .NET namespace that I could "abuse" to exchange generic objects.
I was thinkging about System.ConfigurationManager.AppSettings
but this one supports strings only. Is there one which has an object
type?
I know this is a terrible solution to exchange data between assemblies, but I cant change the interfaces right now.
Upvotes: 0
Views: 46
Reputation: 151594
I'm assuming you want to do this:
// Assembly 1
SomeSuperGlobal.Set("someKey", new Foo { Bar = "baz" });
// Assembly 2
var foo = SomeSuperGlobal.Get("someKey");
First off a warning, what you have is a terrible design. You should not let your code rely on global state, those practices have been abolished since the sixties at least. Do not do this, and thoroughly consider redesigning the application.
That being said, you could use named data slots:
// Assembly 1
LocalDataStoreSlot dataSlot = System.Threading.Thread.AllocateNamedDataSlot("someKey");
System.Threading.Thread.SetData(dataSlot, new Foo { Bar = "baz" });
// Assembly 2
LocalDataStoreSlot dataSlot = System.Threading.Thread.GetNamedDataSlot("someKey");
var foo = System.Threading.Thread.GetData(dataSlot);
Be sure to read Thread.AllocateNamedDataSlot()
's documentation.
Upvotes: 2