Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.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#_Generics - Fatal编程技术网

C# 如何实现基于传递类型返回对象的方法

C# 如何实现基于传递类型返回对象的方法,c#,generics,C#,Generics,我需要实现一个方法,它返回一个基于类型的对象,比如 public interface IBase { } public class Base1 : IBase { } public class Base2 : IBase { } public class Base3 : IBase { } public class MyClass { public IBase GetObject<T>() where T:IBase { // should ret

我需要实现一个方法,它返回一个基于类型的对象,比如

 public interface IBase
{ 
}
public class Base1 : IBase { }
public class Base2 : IBase { }
public class Base3 : IBase { }
public class MyClass
{
    public IBase GetObject<T>() where T:IBase
    {
        // should return the object based on type.
        return null;
    }

}
公共接口IBase
{ 
}
公共类Base1:IBase{}
公共类Base2:IBase{}
公共类Base3:IBase{}
公共类MyClass
{
公共IBase GetObject(),其中T:IBase
{
//应根据类型返回对象。
返回null;
}
}
我是否需要像在GetObject方法中一样维护字典

            Dictionary<Type, IBase> test = new Dictionary<Type, IBase>();
字典测试=新建字典();
有没有更好的办法

[编辑]:-我不想每次都创建对象。我需要把它保存在内存中,当有电话时。我想从那里归还物品。除了字典还有别的方法吗?

公共类MyClass{
public class MyClass {
    public IBase GetObject<T>() where T:IBase, new() // EDIT: Added new constraint 
    {
        // should return the object based on type.
        return new T();
    }

}
public IBase GetObject(),其中T:IBase,new()//编辑:添加了新约束 { //应根据类型返回对象。 返回新的T(); } }
公共类MyClass{
public IBase GetObject(),其中T:IBase,new()//编辑:添加了新约束
{
//应根据类型返回对象。
返回新的T();
}
}

您可以向泛型类型参数添加
new()
约束。请读一读。然后它看起来有点像这样:

public T GetObject<T>() where T : IBase, new()
{
    return new T();
}
public T GetObject(),其中T:IBase,new()
{
返回新的T();
}
并使用它

IBase b = GetObject<Base1>();
IBase b=GetObject();

实际上,有一种内置的方法可以基于类型创建对象,即

IBase b=Activator.CreateInstance();

您可以向泛型类型参数添加
new()
约束。请读一读。然后它看起来有点像这样:

public T GetObject<T>() where T : IBase, new()
{
    return new T();
}
public T GetObject(),其中T:IBase,new()
{
返回新的T();
}
并使用它

IBase b = GetObject<Base1>();
IBase b=GetObject();

实际上,有一种内置的方法可以基于类型创建对象,即

IBase b=Activator.CreateInstance();

在您的情况下,有两种方法:

1) 创建自己的收藏并自己维护(类似这样的东西)

公共接口IBase{}
公共类Base1:IBase{public int x;}
公共类Base2:IBase{public int y;}
公共类Base3:IBase{public int z;}
公共类MyClass
{
字典m_类型集合;
公共MyClass()
{
//比如说
m_typesCollection=新字典();
添加(typeof(Base1),newbase1());
添加(typeof(Base2),newbase2());
}
公共IBase GetObject()
其中T:IBase,new()
{
if(m_typescolection.ContainsKey(typeof(T)))
返回m_types集合[typeof(T)];
添加(typeof(T),new T());
返回m_types集合[typeof(T)];
}
}

2) -使用依赖项注入容器作为类型的集合

在您的情况下,有两种方法:

1) 创建自己的收藏并自己维护(类似这样的东西)

公共接口IBase{}
公共类Base1:IBase{public int x;}
公共类Base2:IBase{public int y;}
公共类Base3:IBase{public int z;}
公共类MyClass
{
字典m_类型集合;
公共MyClass()
{
//比如说
m_typesCollection=新字典();
添加(typeof(Base1),newbase1());
添加(typeof(Base2),newbase2());
}
公共IBase GetObject()
其中T:IBase,new()
{
if(m_typescolection.ContainsKey(typeof(T)))
返回m_types集合[typeof(T)];
添加(typeof(T),new T());
返回m_types集合[typeof(T)];
}
}

2) -使用依赖项注入容器作为类型的集合

您可以发布特定类型的所需用法吗?您的问题是:维护字典完全取决于您是要创建新对象还是返回缓存对象。您需要在问题中提供更多信息以澄清用法。好的,为什么要保留创建的对象?一个原因可能是创建对象的成本很高,您希望缓存它们,并且可能有多个对象实例。另一种情况是,您希望确保一个对象只有一个实例(即单例模式),在这种情况下,您仍然可以使用我的答案,即您使用一个新操作符,但只会创建一个对象实例。那就不需要编字典了,在我看来字典并不那么优雅。我将编辑答案以显示这一点。是的,Raiden创建对象的成本很高,并且我无法在每次请求时都创建它们。我认为您在这里尝试实现的是工厂模式。在这种情况下,我认为使用字典是可以的,这是一个不错的选择。看一看你能用特定类型发布你想要的用法吗?你的问题是:维护字典完全取决于你是想创建新对象,还是返回缓存对象。您需要在问题中提供更多信息以澄清用法。好的,为什么要保留创建的对象?一个原因可能是创建对象的成本很高,您希望缓存它们,并且可能有多个对象实例。另一种情况是,您希望确保一个对象只有一个实例(即单例模式),在这种情况下,您仍然可以使用我的答案,即您使用一个新操作符,但只会创建一个对象实例。那就不需要编字典了,在我看来字典并不那么优雅。我将编辑答案以显示这一点。是的,Raiden创建对象的成本很高,并且我无法在每次请求时都创建它们。我认为您在这里尝试实现的是工厂模式。在这种情况下,我认为使用字典是可以的,这是一个不错的选择。看一看,其中还需要一个新的()约束,就像IBase一样