如何使用泛型类的成员创建对象集合? [英] How could I create an object collection with members from generic class?

查看:72
本文介绍了如何使用泛型类的成员创建对象集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个成员添加到泛型类的集合中,但是它会产生两个错误,如下所示:

I want to add a member to a collection of a generic class, but it generates two errors as below:

Error   1   The best overloaded method match for ''Test.TransitionToolManager.Add(Test.StyleHandleCollection<object>)'' has some invalid arguments


Error   2   Argument 1: cannot convert from ''Test.StyleHandleCollection<Test.Plane>'' to ''Test.StyleHandleCollection<object>''


我做错了什么?
这是代码:


What am I doing wrong?
Here is the code:

namespace Test
{
    public interface ITransitionTool
    {
    }

    public class Plane : ITransitionTool
    {
    }

    public class Car : ITransitionTool
    {
    }

    public class Style
    {
        private Color mColor = new Color();
    }

    public class StyleHandle<T>
    {
        private List<T> mUserObjects = new List<T>();

        public void Adduser(T obj)
        {
            mUserObjects.Add(obj);
        }

        public void RemoveUser(T obj)
        {
            mUserObjects.Remove(obj);
        }
    }

    public class StyleHandleCollection<T>
    {
        public Dictionary<string, StyleHandle<T>> mDict = null;

        public StyleHandleCollection()
        {
            mDict = new Dictionary<string, StyleHandle<T>>();
        }
    }

    public class TransitionToolManager
    {
        private Dictionary<Type, StyleHandleCollection<object>> mCollection = null;

        public TransitionToolManager()
        {
            mCollection = new Dictionary<Type, StyleHandleCollection<object>>();
        }

        public void Add(StyleHandleCollection<object> collection)
        {
            mCollection.Add(typeof(object), collection);
        }
    }

    public class Document
    {
        private TransitionToolManager mMgr = null;

        public Document()
        {
            mMgr = new TransitionToolManager();
            StyleHandleCollection<Plane> planeStyles = new StyleHandleCollection<Plane>(); // error happens in this line...
            mMgr.Add(planeStyles);
        }
    }
}

推荐答案

您的问题似乎是当方法要求类型为Test.PlaneTest.TransitionToolManager.AddStyleHandleCollection时,您传递了Test.TransitionToolManager.AddStyleHandleCollection类型的StyleHandleCollection c3>.您正在尝试传递错误的集合类型.设置您的planeStyles类型为StyleHandleCollection<object>,然后查看是否仍然出现错误.

希望这会有所帮助,

Ed:)
Your issue seems to be that you pass Test.TransitionToolManager.Add a StyleHandleCollection of type Test.Plane when the method requires it to be of type object. You are trying to pass in the wrong collection type. Make your planeStyles of type StyleHandleCollection<object> and see if you still get the error.

Hope this helps,

Ed :)


这是我的解决方案,尽管它并不完美,但可以. :)
Here is my solution, though it is not perfect, it works. :)
namespace Test
{
    public interface ITransitionTool
    {
    }

    public class Plane : ITransitionTool
    {
    }

    public class Car : ITransitionTool
    {
    }

    public interface IStyleCollection
    {
    }

    public class Style
    {
        private Color mColor = new Color();
    }

    public class StyleHandle<T>
    {
        private List<T> mUserObjects = new List<T>();

        public void Adduser(T obj)
        {
            mUserObjects.Add(obj);
        }

        public void RemoveUser(T obj)
        {
            mUserObjects.Remove(obj);
        }
    }

    public class StyleHandleCollection<T> : IStyleCollection
    {
        public Dictionary<string, StyleHandle<T>> mDict = null;

        public StyleHandleCollection()
        {
            mDict = new Dictionary<string, StyleHandle<T>>();
        }
    }

    public class TransitionToolManager
    {
        private Dictionary<Type, IStyleCollection> mCollection = null;

        public TransitionToolManager()
        {
            mCollection = new Dictionary<Type, IStyleCollection>();
        }

        public void Add<T>(StyleHandleCollection<T> collection)
        {
            mCollection.Add(typeof(object), collection);
        }
    }

    public class Document
    {
        private TransitionToolManager mMgr = null;

        public Document()
        {
            mMgr = new TransitionToolManager();
            StyleHandleCollection<Plane> planeStyles = new StyleHandleCollection<Plane>();
            mMgr.Add(planeStyles);
        }
    }
}


这篇关于如何使用泛型类的成员创建对象集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆