为什么C#集合的属性要求它们的属性时不会被标记为过时了吗? [英] Why are C# collection-properties not flagged as obsolete when calling properties on them?

查看:109
本文介绍了为什么C#集合的属性要求它们的属性时不会被标记为过时了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图国旗为已过时找到所有的occurances并保持对事物萎缩名单在我的警告名单来解决,由于这样的事实,我们需要用别的东西来代替这个集合属性的类集合属性






修改:我已经通过Microsoft连接提交这个,的issue#417159



修改16.11.2010 :已验证编译为.NET 3.5和4.0当这现在工作在C#4.0编译器,两者。我得到4警告在发布代码,包括一个与评论不OK?






然而,我惊讶的是,名单只包含少数occurances,远远低于我知道有,并且spotchecks告诉我,由于某种原因,物业的使用并不总是标记为通过在警告列表中的编译器已经过时了。



下面是一个例子程序,准备在Visual Studio 2008进行编译。



请注意接近尾声标记四大行#1-#4,这些,我期望他们都报告说,使用的属性已经过时,但3号没有了,看来如果我只是移动到集合属性或方法直接地说,物业本身的使用没有标记为过时。注意,#3和#4被引用相同的属性,和#4被标记为使用一个过时的属性,而#3不是。测试表明,如果在的表情,我访问属性或收集方法的属性返回,编译器不抱怨。



这是一个错误,或者是?C#编译器的这是一个隐藏的宝石我不知道的。

 使用系统; 
使用System.Collections.Generic;

命名TestApp
{
公共抽象类BaseClass的
{
[作废]
公共抽象的字符串值
{
获得;
}

[作废]
公共抽象的String [] ValueArray
{
搞定;
}

[作废]
公共抽象列表<串GT; ValueList
{
获得;
}
}

公共类DerivedClass:BaseClass的
{
[作废]
公众覆盖字符串值
{
得到
{
返回测试;
}
}

[作废]
公众覆盖的String [] ValueArray
{
得到
{
返回新的[] {A,B};
}
}

[作废]
公众覆盖列表<串GT; ValueList
{
得到
{
返回新的List<串GT;(新[] {A,B});
}
}
}

公共类节目
{
公共静态无效的主要(字串[] args)
{
BaseClass的BC =新DerivedClass();
Console.Out.WriteLine(bc.Value); //#1 - 确定
Console.Out.WriteLine(bc.ValueArray.Length); //#2 - 确定
Console.Out.WriteLine(bc.ValueList.Count); //#3 - 不好吗?
名单,LT;弦乐>名单= bc.ValueList; //#4 - 确定
}
}
}


解决方案

嗯...看起来像一个编译器故障而给我!它失败以下(ECMA 334v4):




24.4.3废弃的属性废弃的属性被用于标记
类型和使用中应
不再类型的成员。如果程序使用了装饰有
过时的属性的
型或成员,那么
编译器须为发出警告或
错误,提醒开发商,
这样的问题的代码可以是固定的。
具体来说,应编译,如果没有错误参数提供
,或发出
警告如果错误参数是
,且具有值false。如果错误参数是
规定和值为true的
编译器会发出一个编译时的
错误。




在特定的,标真当它应该发出一个错误,它没有。好找!你可以在连接的报告,或者如果你不希望建立一个登录的痛苦,让我知道,我会愉快地记录它(这里引用您的文章,不打算偷东西)<。 / p>

(更新)



减少代码复制:

 使用系统; 
使用System.Collections.Generic;
静态类节目{
静态无效的主要(){
诠释计数= Test.Count;
}

[过时(应该ERROR,真)]
公共静态列表<串GT;测试{
获得{抛出新NotImplementedException();}
}
}

需要注意的是单声道2.0得到它的权利,作为是否MS C#2.0编译器。这仅仅是MS C#3.0(.NET 3.5)编译器坏了。


I tried to flag a collection property on a class as Obsolete to find all the occurances and keep a shrinking list of things to fix in my warning-list, due to the fact that we need to replace this collection property with something else.


Edit: I've submitted this through Microsoft Connect, issue #417159.

Edit 16.11.2010: Verified that this now works in the C# 4.0 compiler, both when compiling for .NET 3.5 and 4.0. I get 4 warnings in the posted code, including the one with the comment "Not OK?".


However, to my surprise, the list only contained a few occurances, far fewer than I knew there were, and spotchecks tells me that for some reason, the usage of the property isn't always flagged as obsolete by the compiler in the warning list.

Here's an example program, ready to compile in Visual Studio 2008.

Note the four lines near the end tagged with #1-#4, of these, I'd expect all of them to report that the property used was obsolete, but #3 isn't, and it seems that if I just move on to the collection properties or methods directly, the usage of the property itself isn't flagged as obsolete. Note that #3 and #4 is referencing the same property, and #4 is flagged as using an obsolete property, whereas #3 isn't. Tests shows that if, in the expression, I access properties or methods of the collection the property returns, the compiler doesn't complain.

Is this a bug, or is this a "hidden gem" of the C# compiler I wasn't aware of?

using System;
using System.Collections.Generic;

namespace TestApp
{
    public abstract class BaseClass
    {
        [Obsolete]
        public abstract String Value
        {
            get;
        }

        [Obsolete]
        public abstract String[] ValueArray
        {
            get;
        }

        [Obsolete]
        public abstract List<String> ValueList
        {
            get;
        }
    }

    public class DerivedClass : BaseClass
    {
        [Obsolete]
        public override String Value
        {
            get
            {
                return "Test";
            }
        }

        [Obsolete]
        public override String[] ValueArray
        {
            get
            {
                return new[] { "A", "B" };
            }
        }

        [Obsolete]
        public override List<String> ValueList
        {
            get
            {
                return new List<String>(new[] { "A", "B" });
            }
        }
    }

    public class Program
    {
        public static void Main(String[] args)
        {
            BaseClass bc = new DerivedClass();
            Console.Out.WriteLine(bc.Value);             // #1 - OK
            Console.Out.WriteLine(bc.ValueArray.Length); // #2 - OK
            Console.Out.WriteLine(bc.ValueList.Count);   // #3 - Not OK?
            List<String> list = bc.ValueList;            // #4 - OK
        }
    }
}

解决方案

Hmm... looks like a compiler bug to me! It fails the following (ECMA 334v4):

24.4.3 The Obsolete attribute The attribute Obsolete is used to mark types and members of types that should no longer be used. If a program uses a type or member that is decorated with the Obsolete attribute, then the compiler shall issue a warning or error in order to alert the developer, so the offending code can be fixed. Specifically, the compiler shall issue a warning if no error parameter is provided, or if the error parameter is provided and has the value false. The compiler shall issue a compile-time error if the error parameter is specified and has the value true.

In particular, when marked true it should issue an error, and it doesn't. Good find! You could report it on "connect", or if you don't want the pain of setting up a login, let me know and I'll happily log it (referencing your post here; no attempt to "steal" anything).

(update)

Reduced code to reproduce:

using System;
using System.Collections.Generic;
static class Program {
    static void Main() {
        int count = Test.Count;
    }

    [Obsolete("Should error", true)]
    public static List<string> Test {
        get {throw new NotImplementedException();}
    }
}

Note that mono 2.0 gets it right, as does the MS C# 2.0 compiler. It is only the MS C# 3.0 (.NET 3.5) compiler that is broken.

这篇关于为什么C#集合的属性要求它们的属性时不会被标记为过时了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆