Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#使用具有基类型的字典重写方法_C#_Inheritance_Dictionary - Fatal编程技术网

c#使用具有基类型的字典重写方法

c#使用具有基类型的字典重写方法,c#,inheritance,dictionary,C#,Inheritance,Dictionary,注意:这个问题与潜在的重复问题有细微的不同。。。这有一个有效的答案——请在投票前阅读这两个答案:) -- 我们有两个字典,它们的值有一个共同的基类型: 代码如下: // Three classes class BaseClass {...} class Foo : BaseClass {...} class Bar : BaseClass {...} // Two collections var fooDict = new Dictionary<long, Foo>; var ba

注意:这个问题与潜在的重复问题有细微的不同。。。这有一个有效的答案——请在投票前阅读这两个答案:)

--

我们有两个字典,它们的值有一个共同的基类型:

代码如下:

// Three classes
class BaseClass {...}
class Foo : BaseClass {...}
class Bar : BaseClass {...}

// Two collections
var fooDict = new Dictionary<long, Foo>;
var barDict = new Dictionary<long, Bar>;

// One method
public void FooBar (Dictionary<long, BaseClass> dict) {...}

// These do not work
FooBar(fooDict);
FooBar(barDict);
//三个类
类基类{…}
类Foo:BaseClass{…}
类栏:基类{…}
//两套
var fooDict=新字典;
var barDict=新词典;
//一种方法
公共void FooBar(字典dict dict){…}
//这些不起作用
FooBar(fooDict);
FooBar(barDict);
有没有一种方法可以让继承在字典中工作,或者我们必须使用一种不同的范式——或者我只是太愚蠢了

在此方面的任何帮助或指导都将不胜感激


提前感谢。

诀窍是使方法通用,并通过where关键字限制类型。 试试这个:

namespace GenericsTest
{
using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        Program p = new Program();

        p.Run();

        Console.In.ReadLine();
    }

    private void Run()
    {
        Dictionary<long, Foo> a = new Dictionary<long, Foo> {
            { 1, new Foo { BaseData = "hello", Special1 = 1 } },
            { 2, new Foo { BaseData = "goodbye", Special1 = 2 } } };

        Test(a);
    }

    void Test<Y>(Dictionary<long, Y> data) where Y : BaseType
    {
        foreach (BaseType x in data.Values)
        {
            Console.Out.WriteLine(x.BaseData);
        }
    }
}

public class BaseType { public string BaseData { get; set; } }

public class Foo : BaseType { public int Special1 { get; set; } }

public class Bar : BaseType { public int Special1 { get; set; } }
}

诀窍是使方法泛型,并通过where关键字限制类型。 试试这个:

namespace GenericsTest
{
using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        Program p = new Program();

        p.Run();

        Console.In.ReadLine();
    }

    private void Run()
    {
        Dictionary<long, Foo> a = new Dictionary<long, Foo> {
            { 1, new Foo { BaseData = "hello", Special1 = 1 } },
            { 2, new Foo { BaseData = "goodbye", Special1 = 2 } } };

        Test(a);
    }

    void Test<Y>(Dictionary<long, Y> data) where Y : BaseType
    {
        foreach (BaseType x in data.Values)
        {
            Console.Out.WriteLine(x.BaseData);
        }
    }
}

public class BaseType { public string BaseData { get; set; } }

public class Foo : BaseType { public int Special1 { get; set; } }

public class Bar : BaseType { public int Special1 { get; set; } }
}

所以这不是由设计来实现的-因为这样做是不安全的。。。下面我将看一看wavy的答案给未来读者的提示-下面是WavyDavy和Himbrombree的两个很好的解决方案-再次检查它们,因为这不是由设计实现的-因为这样做是不安全的。。。下面我将看看wavy的答案给未来读者的提示——下面是WavyDavy和Himbrombrey的两个很好的解决方案——检查一下他们两个,这太聪明了。。。安全吗?如果您的解决方案提供相同的名称会更好。无论多么好:)现在这是如此聪明。。。安全吗?如果您的解决方案提供相同的名称会更好。不管多么好:)很好,但是Wavy先到了;)更整洁。可读性更强。很好,但Wavy先到了;)更整洁。更具可读性。
public void FooBar<T> (Dictionary<long, T> dict) where T : BaseClass {...}
interface IFoo {}
class Foo : IFoo {}
class Bar : Bar {}

public void FooBar(Dictionary<long, IFoo> dict) {...}