我有这样的通用方法(简化版):
public static TResult PartialInference<T, TResult>(Func<T, TResult> action, object param)
{
return action((T)param);
}
Run Code Online (Sandbox Code Playgroud)
在上面,param是object故意的类型.这是要求的一部分.
当我填写类型时,我可以这样称呼它:
var test1 = PartialInference<string, bool>(
p => p.EndsWith("!"), "Hello world!"
);
Run Code Online (Sandbox Code Playgroud)
但是,我想使用类型推断.最好,我想写这个:
var test2 = PartialInference<string>(
p => p.EndsWith("!"), "Hello world!"
);
Run Code Online (Sandbox Code Playgroud)
但这不编译.我想出的最好的是:
var test3 = PartialInference(
(string p) => p.EndsWith("!"), "Hello world!"
);
Run Code Online (Sandbox Code Playgroud)
我希望将此作为类型参数并且仍具有正确类型的返回类型的原因是因为我的实际调用看起来像这样:
var list1 = ComponentProvider.Perform(
(ITruckSchedule_StaffRepository p) => p.GetAllForTruckSchedule(this)
)
Run Code Online (Sandbox Code Playgroud)
哪个非常难看,我很乐意写这样的东西:
var list2 = ComponentProvider.Perform<ITruckSchedule_StaffRepository>(
p => p.GetAllForTruckSchedule(this)
)
Run Code Online (Sandbox Code Playgroud) public abstract class Entity : IEntity
{
[Key]
public virtual int Id { get; set; }
}
public class City:Entity
{
public string Code { get; set; }
}
public class BaseViewModel:IBaseViewModel
{
public int Id { get; set; }
}
public class CityModel:BaseViewModel
{
public string Code { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的域和视图类...
和
用于映射扩展
public static TModel ToModel<TModel,TEntity>(this TEntity entity)
where TModel:IBaseViewModel where TEntity:IEntity
{
return Mapper.Map<TEntity, TModel>(entity);
}
Run Code Online (Sandbox Code Playgroud)
我正在使用如下
City city = GetCity(Id);
CityModel model = f.ToModel<CityModel, …Run Code Online (Sandbox Code Playgroud)