我不确定如何更好地表达这个问题,但是在尝试多次创建通用接口字典时遇到了以下问题。尝试创建处理不同类型的注册表类型集合时,通常会发生这种情况:

namespace GenericCollectionTest
{
    [TestFixture]
    public class GenericCollectionTest
    {
        interface IValidator<T>
        {
            bool Validate(T item);
        }

        class TestObject
        {
            public int TestValue { get; set; }
        }

        private Dictionary<Type, IValidator<object>> Validators = new Dictionary<Type, IValidator<object>>();

        class BobsValidator : IValidator<TestObject>
        {
            public bool Validate(TestObject item)
            {
                if (item.TestValue != 1)
                {
                    return false;
                }
            }
        }

        [Test]
        public void Test_That_Validator_Is_Working()
        {
            var Test = new TestObject {TestValue = 1};
            Validators.Add(typeof(BobsValidator), new BobsValidator());

            Assert.That(Validators[typeof(TestObject)].Validate(Test));
        }
    }
}


但是,编译失败是因为BobsValidator无法分配给参数类型IValidator。基本上,我不需要验证程序之外的类型安全性,但是一旦进入验证器,就不需要接口的使用者将其强制转换为要使用的类型。

在Java中,我可以:

Dictionary<Type, IValidator<?>>


我知道我可以做这样的事情(ala IEnumerable):

interface IValidator
{
    bool Validate(object item);
}

interface IValidator<T> : IValidator
{
    bool Validate(T item);
}

abstract class ValidatorBase<T> : IValidator<T>
{
    protected bool Validate(object item)
    {
        return Validate((T)item);
    }

    protected abstract bool Validate(T item);
}


然后使字典改为使用IValidator并扩展ValidatorBase,但似乎必须有一种我看不到的更好的方法。还是,这只是整体设计不佳?看来我需要这样的某种结构:

WhatTheHecktionary<T, IValidator<T>>


谢谢!

最佳答案

为了将BobsValidator分配给IValidator,您需要使接口通用参数成为协变量,这将使IValidator指向更具体的类型,例如IValidator。

interface IValidator<out T>
{
   bool Validate(T item);
}


但是,您将意识到您无法编译,因为您的接口不再是类型安全的,因此编译器将不允许这样做。那么,为什么它不再是类型安全的呢?想象一下:

using NUnit.Framework;
namespace GenericCollectionTest
{
    [TestFixture]
    public class GenericCollectionTest
    {
        //.NET Compiling Error:
        //"Invalid variance: The type parameter 'T' must be contravariantly valid ..."
        interface IValidator<out T>
        {
            //Error: "Parameter must be type-safe. Invalid variance..."
            bool Validate(T item);
        }

        class MyObject
        {
            public int TestValue { get; set; }
        }

        class YourObject
        {
            public int CheckValue { get; set; }
        }

        class MyValidator : IValidator<MyObject>
        {
            public bool Validate(MyObject item)
            {
                return (item).TestValue == 1;
            }
        }

        class YoursValdator : IValidator<YourObject>
        {
            public bool Validate(YourObject item)
            {
                return (item).CheckValue == 1;
            }
        }

        [Test]
        public void Test_That_Validator_Is_Working()
        {
            //.NET compiler tries to prevent the following scenario:

            IValidator<object> someObjectValidator = new MyValidator();
            someObjectValidator.Validate(new YourObject()); // Can't use MyValidator to validate Yourobject

            someObjectValidator = new YoursValdator();
            someObjectValidator.Validate(new MyObject()); // Can't use YoursValidator to validate MyObject

        }
    }
}


要解决此问题,建议您尝试使用非泛型作为基类,以便将验证器存储在字典中。查看以下情况是否适合您的情况:

using System;
using System.Collections.Generic;
using NUnit.Framework;
namespace GenericCollectionTest
{
    [TestFixture]
    public class GenericCollectionTest
    {

        interface IValiadtor
        {
            bool Validate(object item);
        }

        abstract class ValidatorBase<T> : IValidator<T>
        {
            public bool Validate(object item)
            {
                return Validate((T)item);
            }

            public abstract bool Validate(T item);
        }

        interface IValidator<T> : IValiadtor
        {
            //Error: "Parameter must be type-safe. Invalid variance..."
            bool Validate(T item);
        }

        class MyObject
        {
            public int TestValue { get; set; }
        }

        class YourObject
        {
            public int CheckValue { get; set; }
        }

        class MyValidator : ValidatorBase<MyObject>
        {
            public override bool Validate(MyObject item)
            {
                return (item).TestValue == 1;
            }
        }

        class YoursValdator : ValidatorBase<YourObject>
        {
            public override bool Validate(YourObject item)
            {
                return (item).CheckValue == 1;
            }
        }

        [Test]
        public void Test_That_Validator_Is_Working()
        {
            Dictionary<Type, IValiadtor> Validators = new Dictionary<Type, IValiadtor>();
            Validators.Add(typeof(MyObject), new MyValidator() );
            Validators.Add(typeof(YourObject), new YoursValdator());

            var someObject = new MyObject();
            someObject.TestValue = 1;
            Assert.That(Validators[someObject.GetType()].Validate(someObject));


        }
    }
}

07-26 06:26