背景:
我有一个静态类,但静态方法不是扩展方法.我决定将方法重构为扩展方法,并且不希望任何代码破坏,因为扩展方法可以像静态方法一样被调用.但是,当静态导入用于保存扩展方法的静态类时,代码确实中断了.
例:
我有一个带有扩展方法和静态方法的静态类:
namespace UsingStaticExtensionTest.Extensions
{
static class ExtensionClass
{
internal static void Test1(this Program pg)
{
System.Console.WriteLine("OK");
}
internal static void Test2(Program pg)
{
System.Console.WriteLine("OK");
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我使用以下using指令时,测试程序中的所有内容都可以正常工作:
using UsingStaticExtensionTest.Extensions;
namespace UsingStaticExtensionTest
{
class Program
{
static void Main(string[] args)
{
var p = new Program();
ExtensionClass.Test1(p); // OK
p.Test1(); // OK
ExtensionClass.Test2(p); // OK
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我使用static import using指令来识别带有扩展方法的类时,我不能将扩展方法称为静态方法:
using static UsingStaticExtensionTest.Extensions.ExtensionClass;
class Program
{
static void Main(string[] args)
{
var p = new Program(); …Run Code Online (Sandbox Code Playgroud) 我在Swift 2中使用SFSafariViewController在带有ios 9.1(13B143)的iPad Air 2上显示网页.每个网页都需要用户提供的凭据.但是,当用户按下注销按钮时,我需要清除这些凭据.我尝试过使用以下内容:
let allCreds = NSURLCredentialStorage.sharedCredentialStorage().allCredentials
for (protectionSpace, userCredsDict) in allCreds {
for (_, cred) in userCredsDict {
print("DELETING CREDENTIAL")
NSURLCredentialStorage.sharedCredentialStorage().removeCredential(cred, forProtectionSpace: protectionSpace, options: ["NSURLCredentialStorageRemoveSynchronizableCredentials" : true])
}
}
// Clear cookies
if let cookies = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies {
for (cookie) in cookies {
print("DELETING COOKIE")
NSHTTPCookieStorage.sharedHTTPCookieStorage().deleteCookie(cookie)
}
}
// Clear history
print("CLEARING URL HISTORY")
NSURLCache.sharedURLCache().removeAllCachedResponses()
NSUserDefaults.standardUserDefaults().synchronize()
Run Code Online (Sandbox Code Playgroud)
但它不起作用.实际上,"DELETING CREDENTIAL"和"DELETING COOKIE"永远不会被打印出来.此外,我可以完全删除iPad上的应用程序并重新安装它,当我导航回网址时,凭据仍然被缓存.我发现清除凭据的唯一方法是关闭iPad并重新启动它.
问题:如何以编程方式清除凭据?
我最近遇到了一种情况,我List<Base>在期待 a 时收到了a List<Derived>,这确实是我所需要的。事实证明,Derived 是 Base 的子类,List<Base>实际上包含了我需要的 Derived 对象。
但是,我得到的 List 是从第三方 API 返回的,我现在担心它最终可能会返回不止一种派生自 Base 的类型。我需要转换List<Base>为List<Derived>并且我想确保我只转换我需要的类型,同时安全地跳过与我需要的类型无关的任何其他子类型。
例如,如果我有以下类层次结构:
public class Derived : Base {}
public class DerivedOne : Base {}
public class Base {public int i;}
Run Code Online (Sandbox Code Playgroud)
然后我从下面的代码中得到以下结果:
static void Main(string[] args)
{
List<Base> bList = new List<Base>() {new Derived(), new DerivedOne()};
List<Derived> dList = new List<Derived>();
/*InvalidCastException*/
// dList = bList.ConvertAll(x => (Derived)x);
/*InvalidCastException*/
// dList = new List<Derived>(bList.Cast<Derived>());
/*Works, but essentially …Run Code Online (Sandbox Code Playgroud)