gpt4 book ai didi

c# - 为什么此处需要引用未使用的程序集

转载 作者:行者123 更新时间:2023-11-30 15:40:50 24 4
gpt4 key购买 nike

我有一个引用 dll 的 exe。

在dll中有一些方法,我们感兴趣的两个是:

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
};
}

在我的 exe 中,当我尝试调用此方法时:仅使用 cache.SetValue(key, item); 它编译并工作正常,但是当使用 cache.SetValue(key , item, new TimeSpan(0, 1, 0)); 我得到以下异常:

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 do I need to reference a dll which I'm not using directly?但它指的是接口(interface),这里不是这种情况。

编辑:还有这个:

protected static void SetValue(string Key, object Value, CacheItemPolicy Policy)
{
// work is done here
}

但是那是 protected ,调用的 exe 不应该看到它。

Edit2:当上面的 SetValueCacheItemPolicy 设置为 privateinternal 时,它可以工作,为什么 protected(和 protected internal)导致它需要程序集?

最佳答案

由于静态 SetValue 是 protected ,因此它对派生类可见。

因此,如果您可以从该类派生(即它是公共(public)的),那么您就有机会派生并使用该方法,这意味着需要引用。

关于c# - 为什么此处需要引用未使用的程序集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8868620/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com