C# 泛型输入,使用接口的不同泛型输出?
我对泛型相当陌生,我在理解泛型的部分工作原理方面遇到了一些困难,也没能让它按照我想要的方式工作 到目前为止,我有这个C# 泛型输入,使用接口的不同泛型输出?,c#,asp.net,generics,C#,Asp.net,Generics,我对泛型相当陌生,我在理解泛型的部分工作原理方面遇到了一些困难,也没能让它按照我想要的方式工作 到目前为止,我有这个 public interface IObjectMapper { T MapObject<T>() where T : new(); } public class CustomObjectMapper : IObjectMapper { T IObjectMapper.MapObject<T>() { T retO
public interface IObjectMapper
{
T MapObject<T>() where T : new();
}
public class CustomObjectMapper : IObjectMapper
{
T IObjectMapper.MapObject<T>()
{
T retObject = new T();
//do stuff......
}
}
公共接口IObjectMapper
{
T MapObject(),其中T:new();
}
公共类CustomObjectMapper:IObjectMapper
{
T IObjectMapper.MapObject()
{
T retObject=new T();
//做事。。。。。。
}
}
这很好,但我不明白“where t:new()”在做什么。有人能解释一下吗?另外,对于我的第二个问题,我需要一个名为DemapObject的第二个方法,它接受2个参数,相同的泛型类型T来自object,然后不同的泛型类型U-U也应该是返回类型
public interface IObjectMapper
{
T MapObject<T>() where T : new();
U DemapObject<T, U>() ??????? what goes here?
}
公共接口IObjectMapper
{
T MapObject(),其中T:new();
U DemapObject()???这里是什么?
}
最后,一旦我完成了DemapObject的接口方法,如何从实现类调用它
public class CustomObjectMapper : IObjectMapper
{
NameValueCollection IObjectMapper.DempaObject<T, NameValueCollection>()
{
//is this right????????????
}
}
公共类CustomObjectMapper:IObjectMapper
{
NameValueCollection IObjectMapper.DempaObject()
{
//是这样吗????????????
}
}
“其中T:”将在T上设置约束。本例中的约束为“new()”。这意味着泛型类型T必须有一个不带参数的构造函数
可以在同一类型上堆叠where子句:
class MyClass<T> where T : class, new() { }
class MyClass其中T:class,new(){}
或在不同类型上:
class MyClass<T,U>
where T : class, new()
where U : IMyInterface
{
}
class-MyClass
其中T:class,new()
其中U:IMyInterface
{
}
“其中T:”将在T上设置约束。本例中的约束为“new()”。这意味着泛型类型T必须有一个不带参数的构造函数
可以在同一类型上堆叠where子句:
class MyClass<T> where T : class, new() { }
class MyClass其中T:class,new(){}
或在不同类型上:
class MyClass<T,U>
where T : class, new()
where U : IMyInterface
{
}
class-MyClass
其中T:class,new()
其中U:IMyInterface
{
}
其中T:new()
要求用作T
的任何类型都应实现默认值(无参数构造函数)。否则就不可能创建T类的实例
最后一个问题是:您不能对泛型方法进行专门的实现。但是您可以对接口进行专门的实现。考虑这一点:
public interface IObjectMapper<U> where U:new
{
T MapObject<T>(U arg) where T:new()
U DemapObject<T>(T arg);
}
public class CustomObjectMapper : IObjectMapper<NameValueCollection>
{
T MapObject<T>(NameValueCollection arg) where T:new(){
....
}
NameValueCollection DemapObject<T>(T arg)
{
....
}
}
IObjectMapper<NameValueCollection> mapper = new CustomObjectMapper();
公共接口IObjectMapper,其中U:新建
{
T映射对象(U参数),其中T:new()
U数据对象(T arg);
}
公共类CustomObjectMapper:IObjectMapper
{
T映射对象(NameValueCollection参数),其中T:new(){
....
}
NameValueCollection数据对象(T参数)
{
....
}
}
IOObjectMapper mapper=新的CustomObjectMapper();
我还没有理解你的第二个问题,你能详细说明一下吗?其中t:new()
要求用作t
的任何类型都应该实现默认值(无参数构造函数)。否则就不可能创建T类的实例
最后一个问题是:您不能对泛型方法进行专门的实现。但是您可以对接口进行专门的实现。考虑这一点:
public interface IObjectMapper<U> where U:new
{
T MapObject<T>(U arg) where T:new()
U DemapObject<T>(T arg);
}
public class CustomObjectMapper : IObjectMapper<NameValueCollection>
{
T MapObject<T>(NameValueCollection arg) where T:new(){
....
}
NameValueCollection DemapObject<T>(T arg)
{
....
}
}
IObjectMapper<NameValueCollection> mapper = new CustomObjectMapper();
公共接口IObjectMapper,其中U:新建
{
T映射对象(U参数),其中T:new()
U数据对象(T arg);
}
公共类CustomObjectMapper:IObjectMapper
{
T映射对象(NameValueCollection参数),其中T:new(){
....
}
NameValueCollection数据对象(T参数)
{
....
}
}
IOObjectMapper mapper=新的CustomObjectMapper();
我还不明白你的第二个问题,你能详细说明一下吗?我不确定我问的问题是否正确,但看看你的代码,我认为你还是解决了它。我正在尝试创建一个映射对象的接口——有点像ORM之类的东西。因此,基本上,DemapObject方法将获取一个泛型对象并去掉它的值,将它们填充到返回类型中,然后将其吐出。因此,一个实现可能具有DemapObject的DataTable返回类型,而另一个实现可能具有NameValueCollection返回类型。另一个可能只是返回一个字符串,其中包含了查询字符串所需的所有值。我不确定我问的问题是否正确,但看看你的代码,我认为你还是解决了这个问题。我正在尝试创建一个映射对象的接口——有点像ORM之类的东西。因此,基本上,DemapObject方法将获取一个泛型对象并去掉它的值,将它们填充到返回类型中,然后将其吐出。因此,一个实现可能具有DemapObject的DataTable返回类型,而另一个实现可能具有NameValueCollection返回类型。另一个可能只是返回一个字符串,其中包含所有抛出的值,用于查询字符串。