首页 \ 问答 \ F#泛型约束(F# Generics Constraints)

F#泛型约束(F# Generics Constraints)

我在课堂上有这个方法:

member this.GetDbSet<'TEntity, 'TDTO, 'TKey when 'TKey :> IEquatable<'TKey> and 'TEntity :> IEntity<'TKey> and 'TEntity : not struct and 'TDTO :> IDTO<'TKey> and 'TEntity : equality and 'TEntity : null and 'TDTO : equality and 'TDTO : null and 'TKey : equality>(repository : BaseRepository<'TEntity, 'TDTO, 'TKey>) = 
    repository.DbSetFuncGetter().Invoke(uow.Context())

但是当我构建项目时,我得到了这个错误

This code is not sufficiently generic. The type variable 'TEntity when 'TEntity :> IEntity<'TKey> and 'TEntity : not struct and 'TEntity : equality and 'TEntity : null and 'TKey :> IEquatable<'TKey> and 'TKey : equality could not be generalized because it would escape its scope.

但所有的限制都存在。 我错过了什么吗?

编辑:

如果您需要更多代码:

https://github.com/Ar3sDevelopment/Caelan.Frameworks.BIZ/blob/fsharp/Caelan.Frameworks.BIZ/Classes.fs

这是开源项目


I have this method on class:

member this.GetDbSet<'TEntity, 'TDTO, 'TKey when 'TKey :> IEquatable<'TKey> and 'TEntity :> IEntity<'TKey> and 'TEntity : not struct and 'TDTO :> IDTO<'TKey> and 'TEntity : equality and 'TEntity : null and 'TDTO : equality and 'TDTO : null and 'TKey : equality>(repository : BaseRepository<'TEntity, 'TDTO, 'TKey>) = 
    repository.DbSetFuncGetter().Invoke(uow.Context())

but when I build the project I get this error

This code is not sufficiently generic. The type variable 'TEntity when 'TEntity :> IEntity<'TKey> and 'TEntity : not struct and 'TEntity : equality and 'TEntity : null and 'TKey :> IEquatable<'TKey> and 'TKey : equality could not be generalized because it would escape its scope.

But all constraints are there. Am I missing something?

Edit:

If you need more code:

https://github.com/Ar3sDevelopment/Caelan.Frameworks.BIZ/blob/fsharp/Caelan.Frameworks.BIZ/Classes.fs

this is the open source project


原文:https://stackoverflow.com/questions/24430515
更新时间:2022-04-06 18:04

最满意答案

当你有一个带有空格的图片url时,问题就会发生,而传递给css url()的字符串不带引号

[style.background-image]="'url(' + photo + ')'"

上面的代码在你的url没有空格时工作正常。 如果有空格只是用单引号包裹实际图像的URL:

[style.background-image]="'url(\'' + photo + '\')'"

顺便说一下 ,如果这仍然不起作用,请尝试禁用Angular的内置消毒处理以获取传入的值:

bypassSecurityTrustStyle(style)

你可以为此创建一个管道:

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';

@Pipe({ name: 'safe' })
export class SafePipe implements PipeTransform {
  constructor(
    private sanitizer: DomSanitizer
  ) { }

  transform(style) {
    return this.sanitizer.bypassSecurityTrustStyle(style);
  }
}

The problem occurs when you have an image url with spaces in it and the string, which is passed to css url() is without quotes:

[style.background-image]="'url(' + photo + ')'"

The code above works fine when your url has no spaces. If there are spaces just wrap actual image url with single quotes:

[style.background-image]="'url(\'' + photo + '\')'"

By the way, if this still does not work, try disabling Angular's built-in sanitization for the value passed in:

bypassSecurityTrustStyle(style)

You can create a pipe for that:

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';

@Pipe({ name: 'safe' })
export class SafePipe implements PipeTransform {
  constructor(
    private sanitizer: DomSanitizer
  ) { }

  transform(style) {
    return this.sanitizer.bypassSecurityTrustStyle(style);
  }
}

相关问答

更多
  • 需要从style属性获取url 尝试使用.css("backgroundImage") , String.prototype.replace()和RegExp /url\(|\)/g var url = $("div").css("backgroundImage").replace(/url\(|\)/g, ""); console.log(url) div { width:100px; height: 100px; }