I need to create base class like the following code.
public class ResourceBase
{
protected static IDictionary<string, XDocument> resources;
protected static IDictionary<string, XDocument> Resources
{
get
{
if (resources == null)
{
// cache XDocument instance in resources variable seperate by culture name.
// load resx file to XDocument
}
return resources;
}
}
protected static string GetString(string resourceKey)
{
return GetString(resourceKey, System.Threading.Thread.CurrentThread.CurrentUICulture.Name);
}
protected static string GetString(string resourceKey, string cultureName)
{
// get data from XDocument instance
var result = (
from rs in Resources
let node = rs.Value.Root.Elements(XName.Get("data")).SingleOrDefault<XElement>(x => x.Attribute(XName.Get("name")).Value == resourceKey)
where
(rs.Key == DEFAULT_CULTUREKEY || cultureName == rs.Key) &&
node != null
orderby cultureName == rs.Key descending
select node.Element(XName.Get("value"))
).FirstOrDefault<XElement>();
return result.Value;
}
}
Next, I create child class like the following code.
public class MainResource : ResourceBase
{
public static string AppName
{
return GetString("AppName");
}
}
public class OtherResource : ResourceBase
{
public static string OtherName
{
return GetString("OtherName");
}
}
I have some problem because resource variable in base class. All child classes use some Resource variable. So, they always use same cached XDocument instance. Do you have any idea for fixing my sourcecode?
PS. I found some Attribute like ContextStaticAttribute which indicates that the value of a static field is unique for a particular context. I think a particular context should be difference thread. So, I can't use it for solving this question.
Thanks,