我有这样的源和目标对象:
class ProductWithCategories // Source class
{
public Product Product { get; set; } // Product is an EF entity class
public IEnumerable<Category> Categories { get; set; }
}
class ProductViewModel // Dest class
{
public int Id { get; set; }
// Other properties with the same name as Product class
public IEnumerable<CategoryViewModel> Categories { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
所以,我需要将值映射source.Product到dest,然后映射source.Categories到dest.Categories.AutoMapper有可能吗?
我试过这个,当它失败时我并不感到惊讶:
config.CreateMap<ProductWithCategories, ProductViewModel>()
.ForMember(q => q, option => option.MapFrom(q => q.Product)) …Run Code Online (Sandbox Code Playgroud) 好吧,我知道这是规则:
根据JLS:8.1.3内部类和封闭实例,内部类可能不会声明静态初始化器或成员接口.内部类可能不会声明静态成员,除非它们是编译时常量字段.
根据8.5.2静态成员类型声明,"成员接口始终是隐式静态的.允许但不需要声明成员接口明确列出静态修饰符".他们总是顶级的,而不是内心的.
我只是想知道为什么.如果我们被允许在内部类中声明接口会发生什么?如果我将内部类放入另一个类文件中,内部类是否会成为顶级类?
我刚刚意识到默认情况下fetch不发送Accept-Language标头,因此我计划添加它。但是,我还没有找到任何方法来获取浏览器应生成的字符串。
例如,在我的浏览器中,所有正常请求都有一个很好的 Accept-Language 值:
en-US,en;q=0.9,ja;q=0.8,vi;q=0.7,la;q=0.6
我的问题可以通过以下任一选项解决:
fetch?navigator.languages?目前我正在使用这种方法,但我自己做了,不确定它是否矫枉过正/不好。如果可能的话,我更愿意使用内置方式。var getAcceptLanguageValue = () => {
// Assume there are less than 10 languages
let q = 1;
return navigator.languages
.slice(0, 10)
.map(l => `${l};q=0.${10-q++}`)
.join(",");
};
Run Code Online (Sandbox Code Playgroud)
我得到的结果显然并不相同:
en-US;q=0.9,en;q=0.8,ja;q=0.7,vi;q=0.6,la;q=0.5
的扩展GetOrCreateAsync方法IMemoryCache:
public static async Task<TItem?> GetOrCreateAsync<TItem>(this IMemoryCache cache, object key, Func<ICacheEntry, Task<TItem>> factory)
{
if (!cache.TryGetValue(key, out object? result))
{
using ICacheEntry entry = cache.CreateEntry(key);
result = await factory(entry).ConfigureAwait(false);
entry.Value = result;
}
return (TItem?)result;
}
Run Code Online (Sandbox Code Playgroud)
为什么他们会回来TItem?而不是仅仅TItem?假设我的factory方法永远不会返回 null,那么可以安全地假设它永远不会返回null并使用 null-forgiving 运算符忽略它!吗?
public async Task<Foo> GetFooAsync()
=> (await cache.GetOrCreateAsync("Foo", async _ => new Foo()))!
Run Code Online (Sandbox Code Playgroud) 我需要两个导航按钮,其文本是<和>.但是,编译器不允许我使用这些符号,即使我使用\<和\>.
无论如何将这些符号放入XML设计文件中?
我有一个属性接口:
public interface IEntityModifier
{
...
bool AutoDetachOnFinished { get; set; }
bool Finished { get; }
...
}
Run Code Online (Sandbox Code Playgroud)
然后我实现它:
bool IEntityModifier.AutoDetachOnFinished { get; set; }
bool IEntityModifier.Finished { get { return this.mFinished; } }
Run Code Online (Sandbox Code Playgroud)
但是当我需要AutoDetachOnFinished在同一个类中访问时,会弹出一个编译器错误:
void IEntityModifier.Update(IEntity pEntity, Microsoft.Xna.Framework.GameTime pGameTime)
{
if (!this.mFinished)
{
this.Value += this.Delta * (float)pGameTime.ElapsedGameTime.TotalSeconds;
if (this.Value >= this.Max)
{
this.Value = this.Max;
this.mFinished = true;
if (this.AutoDetachOnFinished) { /* Error Here */ }
}
}
}
Run Code Online (Sandbox Code Playgroud)
错误消息:
14'MEngine.Entities.EntityModifier.SingleValueEntityModifier'不包含'AutoDetachOnFinished'的定义,并且没有扩展方法'AutoDetachOnFinished'接受类型'MEngine.Entities.EntityModifier.SingleValueEntityModifier'的第一个参数可以找到(你错过了使用吗?指令或程序集引用?)
我有两个问题:
IEntityModifier.s …我的泛型类中有以下方法:
// This is the class declaration
public abstract class BaseService<TEntity, TKey> : IBaseService<TEntity, TKey> where TEntity : class, IEntity<TKey>
// The Method
public IQueryable<TEntity> GetActive()
{
if (typeof(IActivable).IsAssignableFrom(typeof(TEntity)))
{
return this.repository.Get().Cast<IActivable>()
.Where(q => q.Active)
.Cast<TEntity>();
}
else
{
return this.Get();
}
}
Run Code Online (Sandbox Code Playgroud)
这是界面:
public interface IActivable
{
bool Active { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
基本上,TEntity是一个实体(POCO)类,如果它们具有Active属性,则可以实现IActivable .我希望该方法返回所有Active值为true的记录.但是,我有这个错误:
无法将类型"WebTest.Models.Entities.Product"转换为"Data.IActivable"类型.LINQ to Entities仅支持转换EDM原语或枚举类型.
我理解为什么会出现这种错误.但关于SO的文章对我的案例没有任何有效的解决方案.用它Cast或其他方式可以实现吗?注意:我不想转换为IEnumerable,我想保留IQueryable.
使用 Net 6 和实体框架我有以下实体:
public class Address {
public Int32 Id { get; set; }
public String CountryCode { get; set; }
public String Locality { get; set; }
public Point Location { get; set; }
public virtual Country Country { get; set; }
public virtual ICollection<User> Users { get; set; } = new List<User>();
}
Run Code Online (Sandbox Code Playgroud)
在项目定义中<Nullable>enable</Nullable>,我收到警告:
Non-nullable property '...' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
Run Code Online (Sandbox Code Playgroud)
在地址属性中:
CountryCode, Locality, Location …Run Code Online (Sandbox Code Playgroud) 我在 TypeScript 上遇到了一个奇怪的问题。我最近了解了void ...operator,因为我需要应用它所以eslint不会报告no-floating-promises。然而,这个特定的代码片段以某种方式导致了我无法在 TypeScript Playground 上重现的问题:
class A {
async a() {}
async onTickAsync(repeat: boolean) {
try {
await this.a();
} catch(e) {
console.error(e);
} finally {
if (repeat) {
window.setTimeout(() => void this.onTickAsync(true), 200);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
VS Code 会报这个错误:
TS7011:缺少返回类型注释的函数表达式隐式具有“any”返回类型。
但是,该问题在TS Playground上无法重现。VS Code 和 Playground 都使用 TypeScript 4.5.4。这是我的tsconfig.json:
class A {
async a() {}
async onTickAsync(repeat: boolean) {
try {
await this.a();
} catch(e) { …Run Code Online (Sandbox Code Playgroud) c# ×6
.net ×2
.net-6.0 ×1
.net-core ×1
android ×1
any ×1
automapper ×1
class ×1
function ×1
interface ×1
java ×1
javascript ×1
linq ×1
localization ×1
memorycache ×1
oop ×1
symbols ×1
syntax ×1
typescript ×1
void ×1
xml ×1
xml-layout ×1