I have a very old but very large library which I am considering converting to a C# class library. The existing library uses a lot of global variables stored in the TLS. C# h
Presuming you're going to use .NET 4.0, you could have a static ThreadLocal where your ThreadLocalData class has all your variables as properties:
class ThreadLocalData
{
public int GlobalInt { get; set; }
public string GlobalString { get; set; }
}
class Global
{
static ThreadLocal _ThreadLocal =
new ThreadLocal( () => new ThreadLocalData() );
public static ThreadLocalData ThreadLocal
{
get { return _ThreadLocal.Value; }
}
}
You would then access the properties like this:
int i = Global.ThreadLocal.GlobalInt;
You could add any global variables that are not thread-local as normal properties of the Global class.