为什么我不能有对象的类型约束 [英] Why can't I have a type Constraint of Object

查看:146
本文介绍了为什么我不能有对象的类型约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置的自定义IDictionary的,这将使对象作为我TValue。

I am trying to setup custom IDictionary that will allow Object as my TValue.

下面是什么样子:

public class NullTolerantDictionary<TKey, TValue> 
             : Dictionary<TKey, TValue> where TValue : class
{
    public TValue this[TKey key]
    {
        get
        {
            TValue value;
            if (TryGetValue(key, out value))
            {
                return value;
            }
            else
            {
                return DependencyProperty.UnsetValue;
            }
        }        
    }
}

在此编译它说:

无法隐式转换类型'对象'到'TValue。

Cannot implicitly convert type 'object' to 'TValue'.

因此​​,要解决这个问题我改变我的类型约束是这样的:

So to fix this I change my type constraint to be like this:

where TValue : object

对象而不是类)

然后我得到这个消息:

约束不能特殊类对象

如何解决这个问题呢?

附加题解释为什么对象是不是能成为一个制约因素。

Extra credit for explaining why object is not able to be a constraint.

推荐答案

这里的问题是不是与泛型类型参数的约束,但与索引器的签名。

The problem here is not with generic type argument constraints but with the signature of the indexer.

该索引是记录总是返回 TValue 不管这恰好是的。然而,其他分支试图返回 DependencyProperty.UnsetValue ,其类型为对象。由于不是所有的对象 TValue s时,编译器的投诉(不隐式转换)。

The indexer is documented to always return a TValue, whatever that happens to be. The else branch however tries to return DependencyProperty.UnsetValue, which is of type object. Since not all objects are TValues, the compiler complaints ("no implicit conversion").

如果您放置任意数量的限制对 TValue 这将使没有什么区别,甚至如果你能限制它对象。类型参数仍然可以设置为任何超过派生对象 DependencyProperty.UnsetValue 仍然不能转换到类型。

It would make no difference if you placed any number of constraints on TValue, and even if you could constrain it to object. The type argument could still be set to anything more derived than object and DependencyProperty.UnsetValue would still not be convertible to that type.

如果你想实现你可以使用公共静态这样的效果只读属性,如

If you want to achieve such an effect you could use a public static read-only property such as

public class NullTolerantDictionary<TKey, TValue> 
         : Dictionary<TKey, TValue> where TValue : class
{
    public static TValue MissingValue { get; private set; }
}

和返回,从转位器,presumably使得它随后可在参考比较中使用。

and return that from the indexer, presumably so that it can then be used in reference comparisons.

但是,这仍然留下了一个问题:如何真正得到一个 TValue 的价值为 MissingValue 使用?您可以将新()约束的 TValue ,并创建一个静态构造函数中的实例,但这样会降低可能的应用为此类。这导致在设计开始变得有点笨重使用,因为这是你想什么,以避免有不往那个方向多点。

But that would still leave open the question: how do actually get a TValue value to use as MissingValue? You could place the new() constraint on TValue and create an instance inside a static constructor, but that would decrease the possible applications for this class. This results in the design starting to get a bit clunky to use, and since that's exactly what you are trying to avoid there's not much point in going that way.

这篇关于为什么我不能有对象的类型约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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