C# 从字典列表返回新对象

C# 从字典列表返回新对象,c#,generics,dictionary,types,polymorphism,C#,Generics,Dictionary,Types,Polymorphism,我有一个程序,我使用一个管理器来维护一个泛型类类型的字典。我希望能够注册新类型(使用多态性)并将其存储在字典中(使用基于整数的键),然后能够基于存储的类型创建新对象。以下是我所拥有的: Dictionary<uint,GenericClass> mGenericLibrary = new Dictionary<uint,GenericClass>(); public GenericClass GetNewGenericType(uint id) { retur

我有一个程序,我使用一个管理器来维护一个泛型类类型的字典。我希望能够注册新类型(使用多态性)并将其存储在字典中(使用基于整数的键),然后能够基于存储的类型创建新对象。以下是我所拥有的:

Dictionary<uint,GenericClass> mGenericLibrary = new Dictionary<uint,GenericClass>();

public GenericClass GetNewGenericType(uint id)
{
     return mGenericLibrary[id];
}
Dictionary-mGenericLibrary=new Dictionary();
公共GenericClass GetNewGenericType(uint id)
{
返回mGenericLibrary[id];
}
字典将保存GenericClass类型的子类,即GenericClassSub1、GenericClassSub2、GenericClassSub3等

此时,我希望在另一个类中能够调用GetNewGenericType并获取一个新对象,它是基于注册的整数ID的子类型之一


我怎样才能做到这一点呢?

您正在尝试的操作被称为“”。与其让字典保存您想要创建的类型,不如让字典保存生成您想要创建的类型的类(“工厂”)

然后声明您的词典并按如下方式使用:

class MyClassFactory
{
    Dictionary<uint,GenericFactory> mGenericLibrary = new Dictionary<uint,GenericFactory>();

    public void RegisterFactory(uint id, GenericFactory factory)
    {
        mGenericLibrary[id] = factory;
    }

    public GenericClass GetNewGenericType(uint id)
    {
         return mGenericLibrary[id].CreateInstance();
    }
}


void Example()
{
    var factory = new MyClassFactory();

    factory.RegisterFactory(1, new GenericClassSub1Factory());
    factory.RegisterFactory(2, new GenericClassSub2Factory());
    factory.RegisterFactory(3, new GenericClassSub3Factory());

    var item1 = factory.GetNewGenericType(1); //Contains a new GenericClassSub1;
    var item2 = factory.GetNewGenericType(2); //Contains a new GenericClassSub2;
    var item3 = factory.GetNewGenericType(3); //Contains a new GenericClassSub3;
}
类MyClassFactory
{
Dictionary mGenericLibrary=新字典();
公共无效注册表工厂(uint id,GenericFactory)
{
mGenericLibrary[id]=工厂;
}
公共GenericClass GetNewGenericType(uint id)
{
返回mGenericLibrary[id].CreateInstance();
}
}
void示例()
{
var factory=新的MyClassFactory();
RegisterFactory(1,新的GenericClassSub1Factory());
RegisterFactory(2,新的GenericClassSub2Factory());
RegisterFactory(3,新的GenericClassSub3Factory());
var item1=factory.GetNewGenericType(1);//包含一个新的GenericClassSub1;
var item2=factory.GetNewGenericType(2);//包含一个新的GenericClassSub2;
var item3=factory.GetNewGenericType(3);//包含一个新的GenericClassSub3;
}

更新:

如果您不想让人们创建工厂类,您仍然可以使用工厂模式,但可以通过委托来实现,这将需要在最终用户身上添加更少的代码到工厂中

class MyClassFactory
{
    Dictionary<uint,Func<GenericClass>> mGenericLibrary = new Dictionary<uint,Func<GenericClass>>();

    public void RegisterFactory(uint id, Func<GenericClass> factory)
    {
        mGenericLibrary[id] = factory;
    }

    public GenericClass GetNewGenericType(uint id)
    {
         //This could be one line, but i think mGenericLibrary[id]() looks too weird.
         Func<GenericClass> factory = mGenericLibrary[id];
         return factory();
    }
}

    void Example()
{
    var factory = new MyClassFactory();

    factory.RegisterFactory(1, () => new GenericClassSub1());
    factory.RegisterFactory(2, () => new GenericClassSub2());
    factory.RegisterFactory(3, () => new GenericClassSub3());

    var item1 = factory.GetNewGenericType(1); //Contains a new GenericClassSub1;
    var item2 = factory.GetNewGenericType(2); //Contains a new GenericClassSub2;
    var item3 = factory.GetNewGenericType(3); //Contains a new GenericClassSub3;
}
类MyClassFactory
{
Dictionary mGenericLibrary=新字典();
公共无效注册表工厂(uint id,Func工厂)
{
mGenericLibrary[id]=工厂;
}
公共GenericClass GetNewGenericType(uint id)
{
//这可能是一行,但我认为mGenericLibrary[id]()看起来太奇怪了。
Func factory=mGenericLibrary[id];
返回工厂();
}
}
void示例()
{
var factory=新的MyClassFactory();
RegisterFactory(1,()=>newGenericClassSub1());
RegisterFactory(2,()=>newGenericClassSub2());
RegisterFactory(3,()=>newGenericClassSub3());
var item1=factory.GetNewGenericType(1);//包含一个新的GenericClassSub1;
var item2=factory.GetNewGenericType(2);//包含一个新的GenericClassSub2;
var item3=factory.GetNewGenericType(3);//包含一个新的GenericClassSub3;
}

您尝试执行的操作称为“”。与其让字典保存您想要创建的类型,不如让字典保存生成您想要创建的类型的类(“工厂”)

然后声明您的词典并按如下方式使用:

class MyClassFactory
{
    Dictionary<uint,GenericFactory> mGenericLibrary = new Dictionary<uint,GenericFactory>();

    public void RegisterFactory(uint id, GenericFactory factory)
    {
        mGenericLibrary[id] = factory;
    }

    public GenericClass GetNewGenericType(uint id)
    {
         return mGenericLibrary[id].CreateInstance();
    }
}


void Example()
{
    var factory = new MyClassFactory();

    factory.RegisterFactory(1, new GenericClassSub1Factory());
    factory.RegisterFactory(2, new GenericClassSub2Factory());
    factory.RegisterFactory(3, new GenericClassSub3Factory());

    var item1 = factory.GetNewGenericType(1); //Contains a new GenericClassSub1;
    var item2 = factory.GetNewGenericType(2); //Contains a new GenericClassSub2;
    var item3 = factory.GetNewGenericType(3); //Contains a new GenericClassSub3;
}
类MyClassFactory
{
Dictionary mGenericLibrary=新字典();
公共无效注册表工厂(uint id,GenericFactory)
{
mGenericLibrary[id]=工厂;
}
公共GenericClass GetNewGenericType(uint id)
{
返回mGenericLibrary[id].CreateInstance();
}
}
void示例()
{
var factory=新的MyClassFactory();
RegisterFactory(1,新的GenericClassSub1Factory());
RegisterFactory(2,新的GenericClassSub2Factory());
RegisterFactory(3,新的GenericClassSub3Factory());
var item1=factory.GetNewGenericType(1);//包含一个新的GenericClassSub1;
var item2=factory.GetNewGenericType(2);//包含一个新的GenericClassSub2;
var item3=factory.GetNewGenericType(3);//包含一个新的GenericClassSub3;
}

更新:

如果您不想让人们创建工厂类,您仍然可以使用工厂模式,但可以通过委托来实现,这将需要在最终用户身上添加更少的代码到工厂中

class MyClassFactory
{
    Dictionary<uint,Func<GenericClass>> mGenericLibrary = new Dictionary<uint,Func<GenericClass>>();

    public void RegisterFactory(uint id, Func<GenericClass> factory)
    {
        mGenericLibrary[id] = factory;
    }

    public GenericClass GetNewGenericType(uint id)
    {
         //This could be one line, but i think mGenericLibrary[id]() looks too weird.
         Func<GenericClass> factory = mGenericLibrary[id];
         return factory();
    }
}

    void Example()
{
    var factory = new MyClassFactory();

    factory.RegisterFactory(1, () => new GenericClassSub1());
    factory.RegisterFactory(2, () => new GenericClassSub2());
    factory.RegisterFactory(3, () => new GenericClassSub3());

    var item1 = factory.GetNewGenericType(1); //Contains a new GenericClassSub1;
    var item2 = factory.GetNewGenericType(2); //Contains a new GenericClassSub2;
    var item3 = factory.GetNewGenericType(3); //Contains a new GenericClassSub3;
}
类MyClassFactory
{
Dictionary mGenericLibrary=新字典();
公共无效注册表工厂(uint id,Func工厂)
{
mGenericLibrary[id]=工厂;
}
公共GenericClass GetNewGenericType(uint id)
{
//这可能是一行,但我认为mGenericLibrary[id]()看起来太奇怪了。
Func factory=mGenericLibrary[id];
返回工厂();
}
}
void示例()
{
var factory=新的MyClassFactory();
RegisterFactory(1,()=>newGenericClassSub1());
RegisterFactory(2,()=>newGenericClassSub2());
RegisterFactory(3,()=>newGenericClassSub3());
var item1=factory.GetNewGenericType(1);//包含一个新的GenericClassSub1;
var item2=factory.GetNewGenericType(2);//包含一个新的GenericClassSub2;
var item3=factory.GetNewGenericType(3);//包含一个新的GenericClassSub3;
}

您能从另一个类调用
GetNewGenericType
吗?它是
public
。它不会返回集合中的实际对象而不是新对象吗?因此,您希望返回存储在给定
id
中的对象类型的新实例吗?如果是这样,不管调用它的位置如何,
GetNewGenericType
都不会这样做。你需要更新问题,更清楚地说明你需要什么。你能从另一个类调用
GetNewGenericType
吗?它是
public
。它不会返回集合中的实际对象而不是新对象吗?因此,您希望返回存储在给定
id
中的对象类型的新实例吗?如果是,r