【问题标题】:error CS0234: The type or namespace name 'AccountManagement' does not exist in the namespace 'System.DirectoryServices'错误 CS0234:命名空间“System.DirectoryServices”中不存在类型或命名空间名称“AccountManagement”
【发布时间】:2018-08-01 17:32:34
【问题描述】:

我正在尝试为某个项目引用 System.DirectoryServices.AccountManagement,但不断收到此错误。我已经在 VS 2017 中转到 Project > Add Reference 并包含 System.DirectoryServices.AccountManagement.dll。之后,我进入解决方案资源管理器中的参考属性并将“复制本地”属性设置为 true。这是来自 csc 的完整错误消息:

Microsoft (R) Visual C# Compiler version 2.8.3.63029 (e9a3a6c0)
Copyright (C) Microsoft Corporation. All rights reserved.

Program.cs(7,32): error CS0234: The type or namespace name 'AccountManagement' does not exist in the namespace 'System.DirectoryServices' (are you missing an assembly reference?)

这里有更多信息:

using System;
using System.DirectoryServices; 
using System.DirectoryServices.AccountManagement; 
...
PrincipalContext context = new PrincipalContext(ContextType.Domain, "mycompany.local");
GroupPrincipal findAllGroups = new GroupPrincipal(context, "*");
PrincipalSearcher ps = new PrincipalSearcher(findAllGroups); 

【问题讨论】:

  • 投票关闭此作为没有复制,因为 OP 在重建项目解决问题的答案之一下声明。

标签: c# .net c#-4.0


【解决方案1】:

您的目标可能是较旧版本的 .Net Framework。转到项目属性并查看Target framework 下的应用程序选项卡。确保它是 4 或更高版本。

如果这不是问题,执行清理和重建有时会解决此问题。

【讨论】:

  • 已验证我的目标是 4.6.1
  • 该死,这是我唯一的猜测。除此之外,我只在编译器出现故障时才看到这种情况。尝试清理并重建?
  • 出于某种原因,清理和重建工作正常!感谢您的帮助。
【解决方案2】:

如果你想尝试一些东西,那就试试这个可能性。

创建两个虚拟额外函数。在代码中的某处调用第一个。

经过两次编译(可能仍然会失败)。在编辑器中编写一些代码来强制“Intellisense”从程序集中查找一些枚举值。

然后尝试再次编译。 (在黑暗中拍摄)。

一旦你得到它编译你可能可以删除这两个函数。

这里是函数。

    private void CallThisToForceRefrence()
    {
        int x = 0;
        x = 1;
        x = (x + 1);
        if (x == 42)
        {
            DummyForRoslyn(); //will never execute
        }
        return;
    }

    private void DummyForRoslyn()
    {
        System.DirectoryServices.AccountManagement.PrincipalContext fakeCtx = new System.DirectoryServices.AccountManagement.PrincipalContext(System.DirectoryServices.AccountManagement.ContextType.Domain, "FakeNeverGoingToExecute");
        int sillyNum = 0;
        sillyNum = (int)fakeCtx.ContextType;
        if (sillyNum == (int)System.DirectoryServices.AccountManagement.ContextType.Domain)
        {
            // Does this Enum Refrence Force Roslyn to Go Looking in a First Refrence - First Compile Scenario ?
            sillyNum = 42;
        }
        return;
    }

【讨论】:

    猜你喜欢
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 2012-10-13
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多