简体   繁体   English

C#覆盖问题

[英]c# problems with override

I have a base class that has the following property: 我有一个具有以下属性的基类:

public virtual Dictionary<String, int> activity
    {
        get;
        set;

    }

A derived class overrides this as such: 派生类将其重写为:

Dictionary<string, int> myActivity = new Dictionary<string, int>()
    {
        {"result", 5},
        {"total", 6}
    };


    public override Dictionary<string, int> activity
    {
        get
        {
            return myActivity;
        }

    }

I want to call the override dictionary from within the same assembly of the base class, but a different class. 我想从基类的相同程序集中调用替代字典,但从另一个类调用它。

so for example from Class2 I want to call: 所以例如从Class2我想打电话给:

Class1 c = new Class1();

And then try to access dictionary as such: 然后尝试这样访问字典:

c.activity["result"]

I get an error saying it's not within the dictionary? 我收到错误消息说它不在词典中?

The derived class is in another assembly...dll file. 派生类在另一个Assembly ... dll文件中。

Any ideas? 有任何想法吗?

Thanks. 谢谢。

Class1 c = new Class2();
c.activity["result"]

will work you override activity property in Class2 not in Class1 将覆盖您在Class2中而不是Class1中的活动属性

It doesnt sound like the Class1 c = new Class1(); 听起来不像Class1 c = new Class1(); is the same instance as class where you have instantiated the Dictionary 与实例化Dictionary类是相同的实例

To be able to access the Dictionary property with the value, the class you have instantiated must be either the class which instantiates the Dictionary , or a class that derives from it. 为了能够使用该值访问Dictionary属性,您实例化的类必须是实例化Dictionary的类或从其派生的类。

If you don't have an dictionary instance declared in Class1 then mark the property abstract. 如果您在Class1中没有声明字典实例,请标记属性abstract。 If you do, then just add the value "result" to it. 如果这样做,则只需在其中添加值“结果”。 Think clearly on who owns the data between Class1 and Class2. 仔细考虑谁拥有Class1和Class2之间的数据。 In fact maybe Class1 needs to be just an interface here if it does not intent to actually allocate any data. 实际上,如果Class1不打算实际分配任何数据,则可能这里只是一个接口。

You've created an instance of Class1 which if I'm interpreting the question correctly, is your base class (including the class declarations would be helpful). 您已经创建了Class1的实例,如果我正确解释了这个问题,它就是您的基类(包括类声明会有所帮助)。 In order for your object c to use the dictionary from Class2, it needs to be created as an instance of Class2. 为了使您的对象c使用Class2中的字典,需要将其创建为Class2的实例。

Your code would then look like this: 您的代码将如下所示:

Base class: 基类:

class Class1
{
    public virtual Dictionary<String, int> activity
    {
        get;
        set;
    }
}

Derived class: 派生类:

class Class2
{
    Dictionary<string, int> myActivity = new Dictionary<string, int>()
    {
        {"result", 5},
        {"total", 6}
    };


    public override Dictionary<string, int> activity
    {
        get
        {
            return myActivity;
        }

    }
}

And then the calling code 然后调用代码

Class1 c = new Class2();

At this point, c.activity["result"] will contain the expected result. 此时,c.activity [“ result”]将包含预期的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM