简体   繁体   English

在C#中访问数据结构中通用类型对象的通用方法

[英]Accessing a common method of generically typed objects in a data structure in c#

I am trying to have a datastructure of an abstract class so I can access a method of different objects implementing that abstract class. 我试图拥有抽象类的数据结构,以便可以访问实现该抽象类的不同对象的方法。 This is my code: 这是我的代码:

    using Subsync.App;
    List<App> installed_apps = new List<object>();
    public Views (string[] args)
            {
                //INSTALL APPS HERE.
                installed_apps.Add (new HelloWorld.HelloWorld ());
                //INSTALL END

                foreach (App app in installed_apps) {
                    foreach (KeyValuePair<string, List<object>> match in app.getTokens ()) {
                        tokens.Add (match.Key, match.Value);
                    }
                }

                //begin dispatch
                Dictionary<string,List<object>> tokenized = Tokenize(args);
                Dispatch(tokenized);
            }

In namespace Subsync: 在名称空间Subsync中:

public abstract class App
    {
        public abstract Dictionary<string, List<object>> getTokens ();
    }

In namespace HelloWorld: 在名称空间HelloWorld中:

public class HelloWorld : App
{

    public HelloWorld ()
    {
    }

    public override Dictionary<string, List<object>> getTokens ()
    {
        Dictionary<string, List<object>> ret = new Dictionary<string, List<object>> ();
        ret.Add("helloworld",new List<object>() {"0","-hw","-helloworld"});
        return ret;
    }
}

Compiling gives me the error 编译给我错误

Error CS0246: The type or namespace name `App' could not be found. 错误CS0246:找不到类型或名称空间名称“ App”。 Are you missing a using directive or an assembly reference? 您是否缺少using指令或程序集引用? (CS0246) (App) (CS0246)(应用程序)

All the code files are in the same project folder. 所有代码文件都在同一项目文件夹中。 What am I doing wrong? 我究竟做错了什么? Thanks! 谢谢!

将摘要作为头等修饰符,而不是公共。

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

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