我正在从FileFormat.Info的搜索中找到特殊字符的Unicode .
某些字符呈现为经典的黑白字形,例如⚠(警告标志\u26A0
或⚠
).这些是首选,因为我可以将CSS样式(如颜色)应用于它们.
其他人正在渲染为更新的卡通表情符号,例如hour(沙漏,\u231B
或⌛
).这些不是优选的,因为我不能完全定型它们.
看来浏览器正在进行此更改,因为我能够在Mac Firefox上看到沙漏字形,而不是Mac Chrome或Mac Safari.
有没有办法强制浏览器显示旧的(单调单调)版本?
更新:看来(从下面的评论)有一个文本显示选择,FE0E
,可用来执行文本VS-表情符号.选择器作为后缀连接到字符的代码上,没有空格,例如⌛︎
HTML十六进制或\u231B\uFE0E
JS.但是,它并没有被所有浏览器(例如Chrome和Edge)所尊重.
我正在尝试创建自定义操作过滤器属性.还有一些地方,我需要设施,比如TempData [key]和TryUpdateModel ...我的自定义属性类派生自ActionFilterAttribute,我可以访问以下两种方法.
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,从两个filtercontext局部变量,我不知道如何访问TempData.我试图跟随几个线索,但没有成功.毕竟,也许filterContext变量中有TempData.在这种情况下,如何访问可用的TemData?
谢谢你的帮助
public class Address{
public string ContactName {get; private set;}
public string Company {get; private set;}
//...
public string Zip {get; private set;}
}
Run Code Online (Sandbox Code Playgroud)
我想实现一个distint地址的概念,所以我重写了Equals()来测试所有字段中不区分大小写的相等性(因为这些是US地址,我使用Ordinal而不是InvariantCulture来获得最大性能):
public override bool Equals(Object obj){
if (obj == null || this.GetType() != obj.GetType())
return false;
Address o = (Address)obj;
return
(string.Compare(this.ContactName, o.ContactName, StringComparison.OrdinalIgnoreCase) == 0) &&
(string.Compare(this.Company, o.Company, StringComparison.OrdinalIgnoreCase) == 0)
// ...
(string.Compare(this.Zip, o.Zip, StringComparison.OrdinalIgnoreCase) == 0)
}
Run Code Online (Sandbox Code Playgroud)
我想像这样写一个GetHashCode()(暂时忽略连接效率低下):
public override int GetHashCode(){
return (this.contactName + this.address1 + this.zip).ToLowerOrdinal().GetHashCode();
}
Run Code Online (Sandbox Code Playgroud)
但那不存在.我应该用什么呢?或者我应该在我的Equals()方法中使用InvariantCulture?
(我在想.ToLowerInvariant().GetHashCode() …
使用同一对象的任意数量的实例创建列表的最佳方法是什么?即是否有更紧凑或有效的方式来做以下事情?
static List<MyObj> MyObjs = Enumerable.Range(0, 100)
.Select(i => new MyObj())
.ToList();
Run Code Online (Sandbox Code Playgroud)
(Enumerable.Repeat
会给我十个对同一个对象的引用,所以我觉得它不会起作用.)
使用代码优先的实体框架和.NET 4,我试图在父母与子女之间建立一对多的关系:
public class Parent
{
[Key]
public int ParentId { get; set; }
[Required]
public string ParentName { get; set; }
public IEnumerable<Child> Children { get; set; }
}
public class Child
{
[Key]
public int ChildId { get; set; }
[ForeignKey]
public int ParentId { get; set; }
[Required]
public string ChildName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
正如这里所指出的,为了将外键关系带入数据库,必须链接实际对象,而不仅仅是它们的ID.如果子项包含对其父项的引用(示例),则执行此操作的常规方法.
但是,如何在我的实现中强制执行外键,这是另一种方式(父引用子代)?
我有一个C#regex-parser程序,里面有三个文件,每个文件都包含一个静态类:
1)一个填充字符串字典的静态类
static class MyStringDicts
{
internal static readonly Dictionary<string, string> USstates =
new Dictionary<string, string>()
{
{ "ALABAMA", "AL" },
{ "ALASKA", "AK" },
{ "AMERICAN SAMOA", "AS" },
{ "ARIZONA", "AZ" },
{ "ARKANSAS", "AR" }
// and so on
}
// and some other dictionaries
}
Run Code Online (Sandbox Code Playgroud)
2)将这些值编译为正则表达式的类
public static class Patterns
{
Public static readonly string StateUS =
@"\b(?<STATE>" + CharTree.GenerateRegex(Enumerable.Union(
AddrVals.USstates.Keys,
AddrVals.USstates.Values))
+ @")\b";
//and some more like these
}
Run Code Online (Sandbox Code Playgroud)
3)一些基于这些字符串运行正则表达式的代码:
public static class Parser
{ …
Run Code Online (Sandbox Code Playgroud) 我AuthorizeAttribute
在遗留MVC5项目中有一个自定义:
public class AuthorizeWithLoggingAttribute : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
if (!base.AuthorizeCore(httpContext)) {Log(FilterContext);}
}
}
Run Code Online (Sandbox Code Playgroud)
我们在查看日志时注意到,除了要应用于控制器之外[AuthorizeWithLogging]
,它还在代码中的其他位置显式调用,生成虚假日志:
var filters = new FilterInfo(FilterProviders.Providers.GetFilters(controllerContext, actionDescriptor));
foreach (var authFilter in filters.AuthorizationFilters)
{
authFilter.OnAuthorization(authContext);
if (authContext.Result != null) {return false;}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法告诉(通过StackTrace
什么)OnAuthorization
方法是显式调用,还是从属性调用?我目前最好的是
Environment.StackTrace.Contains("at System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters")
.
EF Core 打开和关闭DbConnection
为每个查询打开和关闭 a,除非您传入已打开的连接。
我有很多小疑问,因此我不想每次都打开和关闭连接,而是希望每次保持连接打开五秒钟,同时为每个查询/命令重用该连接。(上面链接的问题的解决方案使连接在 DBContext 的整个生命周期内保持开放状态。)
抛开锁定/并发问题,我可以在哪里注入自定义连接解析/打开逻辑DbContext
?就像是
before executing query:
if connection is not open
open
set timer to fire close request in five seconds
take lock on connection (to prevent closing)
execute query
release lock
Run Code Online (Sandbox Code Playgroud) 是否可以使Func委托一个扩展方法?例如,就像你可以创建函数一样
bool isSet(this string x) {return x.Length > 0;}
Run Code Online (Sandbox Code Playgroud)
我希望能够写出类似的东西
Func<string, bool> isSet = (this x => x.Length > 0);
Run Code Online (Sandbox Code Playgroud)
当然,上述内容在语法上并不正确.有什么事吗?如果没有,那是语法或编译的限制吗?
我想要一种方法,它会IEnumerable
在谓词中拆分,通过相对于谓词的索引将项目组合在一起.例如,它可以分割List<string>
满足的项目x => MyRegex.Match(x).Success
,其中"中间"项目将这些匹配组合在一起.
它的签名可能看起来像一些线
public static IEnumerable<IEnumerable<TSource>> Split<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate,
int bool count
)
Run Code Online (Sandbox Code Playgroud)
,可能还有一个包含所有分隔符的输出的额外元素.
有没有比foreach
循环更有效和/或更紧凑的方法来实现它?我觉得应该可以用LINQ方法实现,但我不能指责它.
例:
string[] arr = {"One", "Two", "Three", "Nine", "Four", "Seven", "Five"};
arr.Split(x => x.EndsWith("e"));
Run Code Online (Sandbox Code Playgroud)
以下任何一种都可以:
IEnumerable<string> {{}, {"Two"}, {}, {"Four", "Seven"}, {}}
IEnumerable<string> {{"Two"}, {"Four", "Seven"}}
Run Code Online (Sandbox Code Playgroud)
用于存储匹配的可选元素将是{"One", "Three", "Nine", "Five"}
.
c# ×8
linq ×2
.net ×1
asp.net-mvc ×1
attributes ×1
code-first ×1
css ×1
ef-core-3.1 ×1
emoji ×1
equality ×1
foreign-keys ×1
html ×1
javascript ×1
lambda ×1
list ×1
t4 ×1
tempdata ×1
unicode ×1