C# 为什么CredentialCache类需要完整的限定名?
只是想知道为什么CredentialCache类需要一个完整的限定名 比如说C# 为什么CredentialCache类需要完整的限定名?,c#,C#,只是想知道为什么CredentialCache类需要一个完整的限定名 比如说 using System.Net; namespace test { class tc { public void tm() { credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials; //error credentials.Windows.ClientCredential = System.Net.Cred
using System.Net;
namespace test
{
class tc
{
public void tm()
{
credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials; //error
credentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials; //no error
}
}
}
除非在作用域中有另一个
CredentialCache
类,并且编译器不知道如何正确解析符号,否则这不会导致错误
您应该能够从实际的错误文本中确定这一点。它没有。但是你省略了一个
使用System.Net后进行编码>
更新
好的,因此错误消息导致假设您的类中有一个名为CredentialCache
的成员。由于C#规范声明名称是从内部到外部解析的,因此它会在到达System.Net.CredentialCache之前选择该成员(有关详细说明,请参阅ECMA 334第四版C#语言规范-C#,10.8名称空间和类型名称(从我的头顶上看))
您可以使用如下类重现此行为:
using System.Net;
namespace NameResolution{
public class TestUnqualifiedNameResolution {
public DoSomething(){
Dictionary<string, Microsoft.Xrm.Sdk.Client.AuthenticationCredentials> CredentialCache = new Dictionary<string, Microsoft.Xrm.Sdk.Client.AuthenticationCredentials>();
//Fully qualified name is resolved to the type you are looking for, because the Dictionary does not match the name
var credentialCache0 = System.Net.CredentialCache.DefaultCredentials;
//Unqualified name is resolved to the nearest matching (here the Dictionary)
var credentialCache1 = CredentialCache.DefaultNetworkCredentials;
}
}
}
using System;
using System.Net;
namespace ConsoleApplication {
class Program {
static void Main()
{
// That's a System.Int32!
int a = System.Net.CredentialCache;
Console.WriteLine("{0} == System.Int32", a.GetType().FullName);
Console.ReadKey();
}
public static class System
{
public static Net Net { get { return new Net(); } }
}
public class Net
{
public int CredentialCache { get { return 0; } }
}
}
}
CredentialCache本身不需要完整的限定名称。问题一定在别的地方。您可能在测试命名空间中定义了另一个CredentialCache类。
为了提供更多帮助,我需要知道您遇到了什么错误,以及credentials.Windows.ClientCredential中的credentials变量是什么。你在哪里定义了它,它的类型是什么?没错,但由于这是op提供的代码中唯一的错误,我认为他误解了错误消息;在使用System.Net之后。我刚刚在我的问题中解决了这个问题。以下是错误详细信息:错误2“System.Collections.Generic.Dictionary”不包含“DefaultNetworkCredentials”的定义,并且找不到接受“System.Collections.Generic.Dictionary”类型的第一个参数的扩展方法“DefaultNetworkCredentials”(是否缺少using指令或程序集引用?)以下是错误详细信息:“System.Collections.Generic.Dictionary”不包含“DefaultNetworkCredentials”的定义,并且找不到接受“System.Collections.Generic.Dictionary”类型的第一个参数的扩展方法“DefaultNetworkCredentials”(是否缺少using指令或程序集引用?)以下是错误详细信息:“System.Collections.Generic.Dictionary”不包含“DefaultNetworkCredentials”的定义,并且找不到接受“System.Collections.Generic.Dictionary”类型的第一个参数的扩展方法“DefaultNetworkCredentials”(是否缺少using指令或程序集引用?)