English 中文(简体)
如何在图2中采用通用的多变性?
原标题:How to implement generic polymorphism in c# - Part 2?

我已经张贴了

该法典应当更好。

避免混淆 概述一些法典:

using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            IManager<ISpecificEntity> specificManager = new SpecificEntityManager();
            IManager<IAntoherSpecificEntity> anotherSpecificManager = new AnotherSpecificEntityManager();
            Dictionary<string, IManager<IIdentifier>> managers = new Dictionary<string, IManager<IIdentifier>>();
            managers.Add("SpecificManager", (IManager<IIdentifier>)specificManager);
            managers.Add("AnotherSpecificManager", (IManager<IIdentifier>)anotherSpecificManager);

            foreach (var manager in managers.Values)
            {
                IIdentifier entity = manager.Container.GetEntity();
            }
        }
    }

    internal interface IIdentifier
    {
        int Id { get; set; }
    }

    internal interface ISpecificEntity : IIdentifier
    {
        string SpecificValue { get; set; }
    }

    internal class SpecificEntity : ISpecificEntity
    {
        public int Id { get; set; }
        public string SpecificValue { get; set; }
    }


    internal interface IAntoherSpecificEntity : IIdentifier
    {
        string AnotherSpecificValue { get; set; }
    }

    internal class AntoherSpecificEntity : IAntoherSpecificEntity
    {
        public int Id { get; set; }
        public string AnotherSpecificValue { get; set; }
    }

    internal interface IContainer<out TIdentifier> where TIdentifier : IIdentifier
    {
        TIdentifier GetEntity();
    }

    internal interface ISpecificContainer : IContainer<ISpecificEntity>
    {
    }

    internal class SpecificContainer : ISpecificContainer
    {
        public ISpecificEntity GetEntity()
        {
            return new SpecificEntity { SpecificValue = "SpecificValue" };
        }
    }

    internal interface IAnotherSpecificContainer : IContainer<IAntoherSpecificEntity>
    {
    }

    internal class AnotherSpecificContainer : IAnotherSpecificContainer
    {
        public IAntoherSpecificEntity GetEntity()
        {
            return new AntoherSpecificEntity { AnotherSpecificValue = "AnotherSpecificValue" };
        }
    }

    internal interface IManager<TIdentifier> where TIdentifier : IIdentifier
    {
        IContainer<TIdentifier> Container { get; set; }
    }

    internal class SpecificEntityManager : IManager<ISpecificEntity>
    {
        public IContainer<ISpecificEntity> Container { get; set; }
    }

    internal class AnotherSpecificEntityManager : IManager<IAntoherSpecificEntity>
    {
        public IContainer<IAntoherSpecificEntity> Container { get; set; }
    }
}

在I debug 之后,I在Main(第12行文中获得了英瓦列。

I know that ISpecificEntity implements IIdentifier. But obviously a direct cast from an IManager<ISpecificEntity> into an IManager<IIdentifier> does not work.

我认为,与共体合作可做trick,但可作改动:IManager<TIdentifier> into IManager<in TIdentifier>IManager<out TIdentifier>不行。

因此,有没有办法将<条码>具体编码>Manager输入<条码>IManager<IIdentifier>?

感谢一切最好。

最佳回答

首先,大家对通用物有错误的想法。

If you Look at Foo its a generic type. Foo and Foo are NEW types, they do not derive from List, neither the types are conneted by means of inheritance. Using generics creates new types, its does not derive!

然而,你所期待的是http://msdn.microsoft.com/en-us/library/ee207183.aspx” rel=“nofollow” Covariance and Contravariance。 这将使你能够创造一种“遗传多变”的类型,但为了做到这一点,你需要在其通用的<>定义内具体说明这一点。 因此,它将只为盒子中很少的通用框架开展工作。

class Program
{
    static void Main(string[] args)
    {
        IManager<IIdentifier> f1 = new C1();
        IManager<IIdentifier> f2 = new SpecificEntityManager(); //IManager<ISpecificEntity>
    }
}

interface IIdentifier { }
interface ISpecificEntity : IIdentifier { }
interface IManager<out T> { }

class C1 : IManager<IIdentifier> { }
class SpecificEntityManager : IManager<ISpecificEntity> { }

这里,YOU必须改变:

internal interface IContainer<out TIdentifier> where TIdentifier : IIdentifier 
{ 
    TIdentifier GetEntity(); 
}
internal interface IManager<out TIdentifier> where TIdentifier : IIdentifier 
{
    IContainer<IIdentifier> Container { get; }
}
internal class SpecificEntityManager : IManager<ISpecificEntity>
{
    public IContainer<IIdentifier> Container { get; set; }
}
internal class AnotherSpecificEntityManager : IManager<IAntoherSpecificEntity>
{
    public IContainer<IIdentifier> Container { get; set; }
}
问题回答

你们需要一种非基因的IManager,即IManager<TIdentifier> 能够继承。 然后是IManager<IIdentifer> 是IManager,可以投放吗?





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签