C# 如何发布列表<;MyModel>;列出<;对象>;
我不确定我要做的是设置是否正确,所以我请求帮助 我有一个基类a,它有一个公共虚拟方法a(…)。 在方法A中,我有以下陈述:C# 如何发布列表<;MyModel>;列出<;对象>;,c#,C#,我不确定我要做的是设置是否正确,所以我请求帮助 我有一个基类a,它有一个公共虚拟方法a(…)。 在方法A中,我有以下陈述: var recordToValidate = GetBulkRecordsToBeValidated(profileId); < more code that uses recordToValidate > var-recordToValidate=GetBulkRecordsToBeValidated(profileId); 在基类A中,我定义GetBu
var recordToValidate = GetBulkRecordsToBeValidated(profileId);
< more code that uses recordToValidate >
var-recordToValidate=GetBulkRecordsToBeValidated(profileId);
<更多使用recordToValidate的代码>
在基类A中,我定义GetBulkRecordsToBeValidation如下:
internal virtual List<object> GetBulkRecordsToBeValidated(int profileId)
{
throw new NotImplementedException();
}
内部虚拟列表GetBulkRecordsToBeValidated(int profileId)
{
抛出新的NotImplementedException();
}
我还有其他从类A继承的类(即B类从类A继承,C类从类A继承,等等)
每个继承的类都重写GetBulkRecordsToBeValidated,后者调用数据库以返回该实体的特定记录。每个实体返回一个不同的模型,这就是为什么在基类中我让方法返回一个对象列表的原因
在B类中,它看起来像这样:
internal override List<object> GetBulkRecordsToBeValidated(int profileId)
{
return makes call to database which returns a List<ModelBRecords>
}
internal override List<object> GetBulkRecordsToBeValidated(int profileId)
{
return makes call to database which returns a List<ModelCRecords>
}
public class A<T>
{
public void MethodA()
{
List<T> recordToValidate = GetBulkRecordsToBeValidated(profileId);
....
}
internal virtual List<T> GetBulkRecordsToBeValidated(int profileId) // you might want to make this an abstract method instead of virtual
{
....
}
}
public class C : A<ModelCRecords>
{
internal override List<ModelCRecords> GetBulkRecordsToBeValidated(int profileId)
{
return makes call to database which returns a List<ModelCRecords>
}
}
内部覆盖列表GetBulkRecordsToBeValidated(int profileId)
{
return调用返回列表的数据库
}
在C类中,它看起来像这样:
internal override List<object> GetBulkRecordsToBeValidated(int profileId)
{
return makes call to database which returns a List<ModelBRecords>
}
internal override List<object> GetBulkRecordsToBeValidated(int profileId)
{
return makes call to database which returns a List<ModelCRecords>
}
public class A<T>
{
public void MethodA()
{
List<T> recordToValidate = GetBulkRecordsToBeValidated(profileId);
....
}
internal virtual List<T> GetBulkRecordsToBeValidated(int profileId) // you might want to make this an abstract method instead of virtual
{
....
}
}
public class C : A<ModelCRecords>
{
internal override List<ModelCRecords> GetBulkRecordsToBeValidated(int profileId)
{
return makes call to database which returns a List<ModelCRecords>
}
}
内部覆盖列表GetBulkRecordsToBeValidated(int profileId)
{
return调用返回列表的数据库
}
问题是我在返回的B类和C类中遇到以下错误:
无法将表达式类型System.Collection.Generic.List ModelBRecords转换为返回类型System.Collection.Generic.List对象
我需要做什么才能将不同的列表类型返回到基类A的方法A中对GetBulkRecordsToValidate的调用?作为一个快速修复,请使用
.Cast.ToList()
但是,您可能需要考虑使基类泛型如下:
internal override List<object> GetBulkRecordsToBeValidated(int profileId)
{
return makes call to database which returns a List<ModelBRecords>
}
internal override List<object> GetBulkRecordsToBeValidated(int profileId)
{
return makes call to database which returns a List<ModelCRecords>
}
public class A<T>
{
public void MethodA()
{
List<T> recordToValidate = GetBulkRecordsToBeValidated(profileId);
....
}
internal virtual List<T> GetBulkRecordsToBeValidated(int profileId) // you might want to make this an abstract method instead of virtual
{
....
}
}
public class C : A<ModelCRecords>
{
internal override List<ModelCRecords> GetBulkRecordsToBeValidated(int profileId)
{
return makes call to database which returns a List<ModelCRecords>
}
}
公共A类
{
公开无效方法a()
{
List recordToValidate=GetBulkRecordsToBeValidated(profileId);
....
}
内部虚拟列表GetBulkRecordsToBeValidated(int profileId)//您可能希望将其作为抽象方法而不是虚拟方法
{
....
}
}
公共C类:A
{
内部覆盖列表GetBulkRecordsToBeValidated(int profileId)
{
return调用返回列表的数据库
}
}
您可以创建泛型类
class A<T>
{
public virtual List<T> GetBulkStuff() { throw new Exception(); }
}
class B : A<ModelBRecords>
{
public override List<ModelBRecords> GetBulkStuff()
{
return base.GetBulkStuff();
}
}
A类
{
公共虚拟列表GetBulkStuff(){抛出新异常();}
}
B类:A
{
公共覆盖列表GetBulkStuff()
{
return base.GetBulkStuff();
}
}
您的目标是.NET的哪个版本?@maksymiuk:。OP正在询问铸造,但他真正需要的是一个通用基类。