2 回答

TA贡献1798条经验 获得超7个赞
该变量dataTypeModel未在代码的任何位置声明。
如果您想以通用方式返回,您应该执行以下操作:
public DataTypeModel<T> GetDataType<T>(string str) where T : class
{
List<DataTypeDomain> dataTypeDomain = new List<DataTypeDomain>();
_dataProvider.ExecuteCmd(
"config_select_by_key",
inputParamMapper: delegate (SqlParameterCollection paramCol)
{
paramCol.AddWithValue("@ConfigKey", str);
},
singleRecordMapper: delegate (IDataReader reader, short set)
{
int i = 0;
DataTypeModel<int> dataTypeModel = new DataTypeModel<int>();
string key = string.Format("Key{0}", i);
DataTypeDomain dtd = dataTypeDomain.Find(x => x.ConfigKey == key);
dataTypeModel.ConfigKey = dtd.ConfigKey;
dataTypeModel.ConfigValue = int.Parse(dtd.ConfigValue);
}
);
return new DataTypeModel<T>()
{
ConfigKey = "What your key is",
ConfigValue = dataTypeDomain.First() as T //Supposing that the list only contains one config element , if not, you should change your method return type to a List<DataTypeModel<T>> and return a List doing this for each element.
};
}
然后在您的界面中:
public interface IDataTypeService
{
DataTypeModel<T> GetDataType<T>(string str) where T : class;
}
快速说明
当您使用泛型时,您应该在以下方法上指定 T :
DataTypeModel<T> GetDataType<T>(string str) --> Only use T inside method scope
另一种声明方式T是在类/接口级别,例如:
public interface IDataTypeService<T> --> With this you can use `T` in all of the class/interface
此外,如果你想指定一些T应该遵循的约束,你可以这样做:
where T : class; --> In our case this allow us to cast the object to T
该代码未经测试,但我想它应该可以工作。

TA贡献1725条经验 获得超7个赞
您不能T
在接口定义中保持打开状态。你必须关闭类型
DataTypeModel<SomeType> GetDataType(string str);
- 2 回答
- 0 关注
- 310 浏览
添加回答
举报