即使该属性存在,编译时也会出现 CS1061 错误

发布于 2024-11-07 23:32:49 字数 2192 浏览 0 评论 0原文

作为 .Net 开发人员,我遇到了有史以来最奇怪的问题。我正在编译一个库,该库在 UserInfo 类中新添加了 DeviceID 属性。该库在内部使用该类型并且它是新属性就很好,但是当我尝试从另一个库引用它时,编译器会返回一个编译器错误,指出

'library.UserInfo' does not contain a definition for 'DeviceID' and no extension 
method 'DeviceID' accepting a first argument of type 'library.UserInfo' could 
be found 

即使我的类定义看起来

public class UserInfo
{
    public static UserInfo Current
    {
        get
        {
            if (UserInfoPrincipal.Current != null)
            {
                return UserInfoPrincipal.Current.UserData;
            }
            else
            {
                return null;
            }
        }
    }

    public string UserID { get; set; }
    public string DeviceID { get; set; }
    public string MikeLiUserID { get; set; }
    public string TransactionServer { get; set; }
    public string ApplicationKey { get; set; }
    public string IpAddress { get; set; }

}

像这样:有问题的代码如下所示:

    internal LogDetail BuildLogDetail(LogType entryType, string message)
    {
        return new LogDetail
        {
            ActingUserID = UserInfo.Current.UserID,
            ActingDeviceID = UserInfo.Current.DeviceID,
            ApplicationKey = UserInfo.Current.ApplicationKey,
            IpAddress = UserInfo.Current.IpAddress,
            EntryType = entryType,
            OwnerID = UserInfo.Current.UserID,
            LogData = message
        };
    }

我会需要注意的是,UserInfo 类的所有其他成员都正确地通过了编译器,只是今天添加的 DeviceID 导致了问题。我尝试过 Clean All,我尝试过从 TFS 刷新所有内容,手动删除两个项目的 obj 和 bin 目录......但还没有任何效果。

更新:此代码是库的一部分,可以正常工作:

public class UserInfoPrincipal : IPrincipal
{
    public static UserInfoPrincipal Current
    {
        get
        {
            if (Thread.CurrentPrincipal is UserInfoPrincipal)
                return (UserInfoPrincipal)Thread.CurrentPrincipal;
            else
                return null;
        }
    }

    ...

    internal UserInfo UserData
    {
        get { return _userInfo; }
    }

    public string DeviceID
    {
        get { return _userInfo.DeviceID; }
    }

    ...
}

I have come across the most curious problem ever as .Net dev. I am compiling a library which has a newly added property DeviceID in the class of UserInfo. The library internally uses the type and it's new property just fine, but when I try and reference it from another library, the compiler kicks back a compiler error stating

'library.UserInfo' does not contain a definition for 'DeviceID' and no extension 
method 'DeviceID' accepting a first argument of type 'library.UserInfo' could 
be found 

Even though my class definition looks like:

public class UserInfo
{
    public static UserInfo Current
    {
        get
        {
            if (UserInfoPrincipal.Current != null)
            {
                return UserInfoPrincipal.Current.UserData;
            }
            else
            {
                return null;
            }
        }
    }

    public string UserID { get; set; }
    public string DeviceID { get; set; }
    public string MikeLiUserID { get; set; }
    public string TransactionServer { get; set; }
    public string ApplicationKey { get; set; }
    public string IpAddress { get; set; }

}

The offending code reads as such:

    internal LogDetail BuildLogDetail(LogType entryType, string message)
    {
        return new LogDetail
        {
            ActingUserID = UserInfo.Current.UserID,
            ActingDeviceID = UserInfo.Current.DeviceID,
            ApplicationKey = UserInfo.Current.ApplicationKey,
            IpAddress = UserInfo.Current.IpAddress,
            EntryType = entryType,
            OwnerID = UserInfo.Current.UserID,
            LogData = message
        };
    }

I'd like to note that all of the other members of the UserInfo class go through the compiler correctly and it is just the DeviceID, which was added today, is causing the issue. I've tried Clean All, I've tried refreshing everything from TFS, manually deleting the obj and bin directories of both projects... nothing yet has worked.

UPDATE: This code, which is part of the library, works correctly:

public class UserInfoPrincipal : IPrincipal
{
    public static UserInfoPrincipal Current
    {
        get
        {
            if (Thread.CurrentPrincipal is UserInfoPrincipal)
                return (UserInfoPrincipal)Thread.CurrentPrincipal;
            else
                return null;
        }
    }

    ...

    internal UserInfo UserData
    {
        get { return _userInfo; }
    }

    public string DeviceID
    {
        get { return _userInfo.DeviceID; }
    }

    ...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

抱着落日 2024-11-14 23:32:49

所以我的万岁玛丽通行证是删除项目引用,然后再次添加它。然后就编译了。不知道为什么会这样,但我想我会把它发布在这里,供其他可能遇到同样问题的人参考。

So my hail mary pass was to remove the project reference and then add it again. Then it compiled. Have no clue why that worked, but figured I'd post it here for other who might run into the same problem.

巾帼英雄 2024-11-14 23:32:49

我以前曾陷入过几次类似的情况。这对我有用:

这两个代码示例是否位于不同的项目中?如果是这样,我会说尝试重建第一个项目(包含 UserInfo 类),然后取出编译失败的行并尝试重建第二个项目。然后全部重建。然后重新添加有问题的行并重建所有行。

可能不适合你,但值得一试。我知道这种情况令人沮丧。

I've gotten stuck in a few situations like this before. Here's what worked for me:

Are those two samples of code in separate projects? If so, I would say to try rebuilding the first project (containing the UserInfo class), then take out the line that fails the compilation out and try rebuilding the second project. Then do a rebuild all. Then add the offending line back in and do a rebuild all.

May not work for you, but worth a shot. I know that situation is frustrating.

最佳男配角 2024-11-14 23:32:49

其他库是使用项目引用还是二进制引用?如果它是二进制引用,您确定它使用最新版本吗?

Is the other library using a project reference or a binary reference? If its a binary reference, are you sure its using the latest build?

别再吹冷风 2024-11-14 23:32:49

检查产生错误的项目的引用路径;确保您引用库项目(如果它是您的解决方案的一部分)或库的最新版本(如果不是。)

Check the reference path of the project that's generating the error; make sure you're either referencing the library project (if it's part of your solution) or the most recent build of the library (if it's not.)

梦里梦着梦中梦 2024-11-14 23:32:49

对我来说——尝试重新创建显示问题的行。写下对象的名称句点(.),然后等待 VS 显示可用属性列表

for me -- try to recreate the line that shows an issue. Write the name of the object period (.) and wait for VS to show you the list of available properi

追星践月 2024-11-14 23:32:49

我遇到了非常相似的问题。

就我而言,我有一段代码每年只需要运行几次。当我尝试使用它时,访问会员时出错。自上次使用该代码以来,应该没有任何变化。 Intellisense 在使用“.”时检测到该成员在视觉工作室中。我重新启动了 Visual Studio 和计算机,但问题仍然存在。

最后为了解决我的问题,我创建了一个新文件,将代码从原始文件复制到新文件,就是这样。没有代码修改。我使用了 Ctrl-C、Ctrl-V,因此内容没有通过手动触摸进行更正。这不是第一次通过复制和粘贴修复错误,因此值得将这个想法保留在工具箱中。

有时,一个神秘的问题需要同样神秘的解决方案。

I encountered a very similar problem.

In my case I have a piece of code that I only need to run a couple times a year. When I attempted to use it there was an error accessing a Member. Nothing should have changed since the last time I used the code. Intellisense was detecting the member when using the '.' in Visual Studio. I restarted Visual Studio and the computer but the problem stayed.

In the end to fix my problem, I created a new file, copied the code from the original to the new file, and that was it. No code modifications. I used Ctrl-C, Ctrl-V so the content wasn't corrected by a manual touch. This isn't the first time copy and paste has fixed a bug so it's worth keeping the idea in the tool chest.

Sometimes a mysterious problem demands an equally mysterious solution.

柠檬色的秋千 2024-11-14 23:32:49

就我而言,这是 Web 应用程序的项目属性的问题。为了解决这个问题,我执行了以下操作:

  1. 右键单击项目>单击属性。
  2. 在“构建”选项卡上,将所有配置的输出路径值更改为: bin\

以前,我的输出路径是 bin\Debug 或 bin\Release,具体取决于我正在查看的配置。我不知道为什么这会影响我的标记页面查看代码隐藏中的方法的能力,但确实如此。一旦我改变了这个,错误就消失了。

这是 VS2012 w/ update 2 中的内容。

In my case, it was a problem with the web application's project properties. To fix it, I did the following:

  1. Right-click the project > click Properties.
  2. On the Build tab, change the Output path value for all configurations to: bin\

Previously, my output path had been bin\Debug or bin\Release depending on which configuration I was looking at. I don't know why this screwed with my markup page's ability to see methods in my codebehind, but it did. Once I changed this, the error disappeared.

This was in VS2012 w/ update 2.

生生漫 2024-11-14 23:32:49

我的解决方案:我创建了另一个设置属性的名称方法。我的问题是在 VS2015 + Silverlight 5 上

My solution: I was create another name method what set property. My problem was on VS2015 + Silverlight 5

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文