假设我有一个通用参数类,但参数值必须是以下之一:
int, double, string, bool, DateOnly
任何其他类型都应被拒绝。
public record Parameter<T>(string Name, T Value) where T : ...
假设我有一个通用参数类,但参数值必须是以下之一:
int, double, string, bool, DateOnly
任何其他类型都应被拒绝。
public record Parameter<T>(string Name, T Value) where T : ...
假设我有这个超类:
public class SuperClass { }
还有这些子类:
public class SubClass1 : SuperClass { }
public class SubClass2 : SuperClass { }
我们有这个客户端代码:
public void DoStuff(SuperClass arg)
{
if (arg is instance of SubClass1, but not an instance of SubClass2) // How to check this ?
{
var data = (SubClass1) arg;
// Do stuff
}
}
如何检查是否arg
是SubClass1
only 或SubClass2
only ?
是否可以通过Form
标签将对象发布到 Razor 页面?
假设我有这个页面,其中包含OnPost
用于生成 PDF 文件的处理程序
页面内容:
@page "/generate-report"
@model GeneratePdfModel
@{
}
西文:
namespace MyApp
{
public class GeneratePdfModel : PageModel
{
public void OnPost(Data data)
{
// Do stuffs with data and return PDF File result
}
}
}
然后我有另一个页面,其中包含以下表格:
<form action="/generate-report" method="post">
<!-- How to send a Data object -->
<button type="submit">Export</button>
</form>
我如何从中发布数据对象Form
?
假设我们有这个实体和相应的 Dto:
public class MyEntity
{
public string Name {get; set;}
}
public class MyEntityDto
{
public string Name {get;set;}
}
这个 Aggregate 和相应的 Dto:
public class MyAgg
{
public string SomeProp {get;set;}
public MyEntity Entity {get;set;}
}
public class MyAggDto
{
public string SomeProp {get;set;}
public AggDataDto Data {get;set;}
public class AggDataDto
{
public MyEntityDto Entity {get;set;}
}
}
这是映射配置文件:
public class MyProfile : Profile
{
public MyProfile()
{
CreateMap<MyEntity, MyEntityDto>();
CreateMap<MyAgg, MyAggDto>()
.ForMember(dest => dest.Data, opt => opt.MapFrom(src => {
return new MyAggDto.AggDataDto
{
/*
Can I inject and use IMapper here to get MyEntityDto from src.Entity ?
Should I create the Dto with new MyEntityDto(...) ?
Any other approach ?
*/
Entity = ...
}
}));
}
}
如何映射嵌套对象?