I have an exe referencing a dll.
In the dll there’s some methods, the two we’re interested in are:
private static CacheItemPolicy _policy = new CacheItemPolicy();
[MethodImpl(MethodImplOptions.NoInlining)]
public static void SetValue(string Key, object Value)
{
SetValue(Key, Value, _policy);
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static void SetValue(string Key, object Value, TimeSpan TTL)
{
SetValue(Key, Value, GetPolicy(TTL));
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static CacheItemPolicy GetPolicy(TimeSpan TTL)
{
return new CacheItemPolicy()
{
SlidingExpiration = TTL
};
}
In my exe when I try to call this method: using just cache.SetValue(key, item); it compiles and works fine, but when called using cache.SetValue(key, item, new TimeSpan(0, 1, 0)); I get the following exception:
Error 1 The type ‘System.Runtime.Caching.CacheItemPolicy’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘System.Runtime.Caching, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’.
Why does the compiler say that I need to add this reference to an assembly that I’m not directly using in my code at this point?
I’m sure it’s something simple that I’m missing. I looked at Why do I need to reference a dll which I'm not using directly? but it refers to interfaces and that is not the case here.
Edit: there is also this:
protected static void SetValue(string Key, object Value, CacheItemPolicy Policy)
{
// work is done here
}
But that’s protected and shouldn’t be visible to the calling exe.
Edit2: when this above SetValue that takes a CacheItemPolicy is set to private or internal, it works, why does protected (and protected internal) cause it to require the assembly?
Since that static
SetValueis protected, it’s visible to derived classes.So, if you can derive from the class (i.e. it’s public), then there’s a chance you could derive and use that method, meaning the reference is required.