当我引用两个具有相同命名空间和类型名称的外部程序集时,我遇到了不太可能发生的情况.当我尝试使用该类型时,编译器抛出一个错误,它无法解析我想要使用的那个.
我看到C#提供了一种为引用使用别名的机制.您甚至可以通过PropertyVisual Studio 2008中的引用窗口指定这些别名.如何在代码中使用此别名?据我所知,我应该使用::运算符,但它失败并出现以下错误:
CS0432 - Alias not found
通常的.操作员也会失败.
在输出窗口中,我看到编译器在其命令行中正确传递了别名.
关于我接下来可能尝试的任何指示都非常感谢.
我遇到了一个问题,我无法在不同的命名空间中引用类.我有2个班:
namespace Foo
{
public class Class1 { ... }
}
namespace My.App.Foo
{
public class Class2
{
public void SomeMethod()
{
var x = new Foo.Class1; // compile error!
}
}
}
Run Code Online (Sandbox Code Playgroud)
编译错误是:
命名空间"My.App.Foo"中不存在类型或命名空间名称"Class1"
在这种情况下,我似乎无法让Visual Studio认识到"Foo.Class1"指的是第一类.如果我将鼠标悬停在"Foo"上,则表明它试图将其解析为"My.App.Foo.Class1"
如果我把线:
using Foo;
Run Code Online (Sandbox Code Playgroud)
在包含Class2的.cs文件的顶部,然后它也将其解析为"My.App.Foo".
有没有一些技巧来引用正确的"Foo"命名空间而不只是重命名命名空间,以便它们不冲突?这两个命名空间都在同一个程序集中.
我有一些名称空间问题令我困惑,为什么会发生这种情况.
在下面的代码中,System.IO和System.Reflection试图引用abc.System,而不是使用我在顶部声明的System命名空间.这是为什么?
using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace abc.Data
{
public sealed class Access
{
public static void Open(string dbPath)
{
// error here referencing abc.System in System.IO, and System.Reflection.
string appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); }
}
Run Code Online (Sandbox Code Playgroud)
然后我在另一个文件中有另一个命名空间,如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace abc.System
{
public static class DateTimeExtensions
{
// Implemented from
// http://stackoverflow.com/questions/38039/how-can-i-get-the-datetime-for-the-start-of-the-week
public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
{
int diff …Run Code Online (Sandbox Code Playgroud) 在创建测试类时,我在我的项目中遇到了命名空间的奇怪问题。
所以我有一个我制作的 nuget 包,它的命名空间是
ppumkin.APIService
然后我创建了另一个测试解决方案,该解决方案的命名空间是
tests.ppumkin.ControllersAPI
问题是我正在尝试访问我的 nuget 包 dll 对象,但 Visual Studio 告诉我,that对象不存在,tests.APIService但我编写的代码是
var obj = new ppumkin.APIService.TheClass()
如何告诉 Visual Studio 该对象不在tests.ppumkin.APIService但只是在ppumkin.APIService (例如 - 没有前缀测试命名空间)
如果我在测试项目中定义的所有命名空间之外使用相同的对象创建,我可以克服这个问题。