我假设lambda functions
,delegates
并且anonymous functions
使用相同的主体将具有相同的"速度",但是,运行以下简单程序:
static void Main(string[] args)
{
List<int> items = new List<int>();
Random random = new Random();
for (int i = 0; i < 10000000; i++)
{
items.Add(random.Next());
}
Stopwatch watch;
IEnumerable<int> result;
Func<int, bool> @delegate = delegate(int i)
{
return i < 500;
};
watch = Stopwatch.StartNew();
result = items.Where(@delegate);
watch.Stop();
Console.WriteLine("Delegate: {0}", watch.Elapsed.TotalMilliseconds);
Func<int, bool> lambda = i => i < 500;
watch = Stopwatch.StartNew();
result = items.Where(lambda);
watch.Stop();
Console.WriteLine("Lambda: {0}", …
Run Code Online (Sandbox Code Playgroud) 我想获得一个MyAttribute
使用T4/EnvDTE 装饰的项目中所有公共方法的列表.
我知道这可以用反射来实现,但我不希望加载的组装和反映了它在T4模板,相反,我想用现有的代码文件作为来源.
以下是我在互联网上找到的样板代码,它获取对当前项目的引用
<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ assembly name="EnvDTE" #>
<#@ assembly name="System.Core.dll" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
<#@ output extension=".cs" #>
<#
IServiceProvider _ServiceProvider = (IServiceProvider)Host;
if (_ServiceProvider == null)
throw new Exception("Host property returned unexpected value (null)");
EnvDTE.DTE dte = (EnvDTE.DTE)_ServiceProvider.GetService(typeof(EnvDTE.DTE));
if (dte == null)
throw new Exception("Unable to retrieve EnvDTE.DTE");
Array activeSolutionProjects = (Array)dte.ActiveSolutionProjects;
if (activeSolutionProjects == null)
throw new Exception("DTE.ActiveSolutionProjects returned null"); …
Run Code Online (Sandbox Code Playgroud) 不确定这是否是发布此问题的正确位置,但我已经看到有关ISBN数据库的问题,所以我认为这是合适的.
在我的网站上,我打算允许我的用户在所有美国大学/学院(社区或4年制学院)之间进行选择.然后我会将他们的选择存储在数据库中.
起初我想过让他们自己输入名字,但看到了一些问题.我试图寻找某种类型的数据库,但我找到的只是寻找特定大学的搜索引擎.
我希望找到一个数据库可以导出到我自己的数据库(SQL Server)并让用户搜索我自己的数据库.
有没有人遇到过这个问题并找到了合理的解决方案?
我的应用程序使用MEF从外部程序集导出一些类.这些类是为构造函数注入而设置的.我面临的问题是,当我尝试访问它们时,MEF正在尝试实例化类.有没有办法让Ninject负责该类的实例化?
IEnumerable<Lazy<IMyInterface>> controllers =
mefContainer.GetExports<IMyInterface>();
// The following line throws an error because MEF is
// trying to instantiate a class that requires 5 parameters
IMyInterface firstClass = controllers.First().Value;
Run Code Online (Sandbox Code Playgroud)
有多个类实现IMyInterface
,我想选择具有特定名称的类,然后让Ninject创建它的实例.我不确定我是否想要懒惰.
[Export(typeof(IMyInterface))]
public class MyClassOne : IMyInterface {
private MyRepository one;
private YourRepository two;
public MyClassTwo(MyRepository repoOne, YourRepository repoTwo) {
one = repoOne;
two = repoTwo;
}
}
[Export(typeof(IMyInterface))]
public class MyClassTwo : IMyInterface {
private MyRepository one;
private YourRepository two;
public MyClassTwo(MyRepository repoOne, YourRepository repoTwo) {
one …
Run Code Online (Sandbox Code Playgroud) 我有一个自定义委托处理程序来管理请求的身份验证.在我的一个控制器中,不应为特定操作启用身份验证.如何禁用方法和路由的委托处理程序POST api/MyController
?
一种选择是对处理程序内的路由进行硬编码,但是,我宁愿将此逻辑保留在处理程序之外.此外,我发现自己将此行为添加到更多操作中,这可能使此方法难以维护.
protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
if (request.Method == HttpMethod.Post
&& request.RequestUri.PathAndQuery.StartsWith("/api/MyController"))
return base.SendAsync(request, cancellationToken);
// Do authentication
}
Run Code Online (Sandbox Code Playgroud)
有更好的方法更容易维护吗?
由于C#没有前,后,后,第一等作为其foreach的一部分.挑战是使用以下标准尽可能优雅地模仿这种行为:
如果你能超过上述标准,那就别随便做!
我将在下面发布我的答案,但它不优雅也不可行,所以我想看看社区可以想到什么.
对于循环的硬编码有时令人烦恼=(
我看到了这段代码:
$("ul li").text().search(new RegExp("sometext", "i"));
Run Code Online (Sandbox Code Playgroud)
并想知道这是否可以扩展到任何字符串?
我想完成以下任务,但它不起作用:
$("li").attr("title").search(new RegExp("sometext", "i"));
Run Code Online (Sandbox Code Playgroud)
此外,任何人都有这个功能的jQuery文档的链接?我显然在Google上搜索失败了.
在我的ASP.NET MVC应用程序中,我有一个包含所有业务逻辑/服务层的项目.该项目与我的数据库(实体框架)进行交互,该数据库位于一个单独的项目中.
我希望能够轻松访问服务层,因此我在其中创建了静态类,以便可以轻松地引用它们.例如,如果我在我的控制器中,我需要创建一个新帐户:
ServiceLayer.Accounts.CreateAccount(userName, passWord) //etc..
Run Code Online (Sandbox Code Playgroud)
然后服务层执行所有必需的逻辑,然后通过存储库创建用户DatabaseLayer
.
private static AllRepos _Repos;
private static AllRepos Repos {
get
{
if(_Repos == null)
_Repos = new AllRepos();
return _Repos
}
}
public static void CreateAccount(string username, password)
{
string salt = GenerateSalt();
Account newAccount = DatabaseLayer.Models.Account
{
Name = username,
Password = HashPassword(password, salt),
Salt = salt
};
Repos.AddAccount(newAccount);
}
Run Code Online (Sandbox Code Playgroud)
因为我不想在我的服务层中到处执行以下操作:
AccountRepository Accounts = new DatabaseLayer.AccountRepository();
Run Code Online (Sandbox Code Playgroud)
我为我的存储库创建了一个包装类,这样我只需要实例化一次就可以使用所有其他存储库.
public class AllRepos
{
private AccountRepository _Accounts;
public AccountRepository Accounts
{
get
{ …
Run Code Online (Sandbox Code Playgroud) 我可以做以下事情:
container.Users.Where(u => u.Name == "Omar").FirstOrDefault()
但
container.Users.FirstOrDefault(u => u.Name == "Omar")
返回一个NotSupportedException (The method 'FirstOrDefault' is not supported.)
.
由于这些基本相同,为什么不支持?
我正在寻找在我正在构建的API中实现HMACSHA256请求签名.从我地理解https://tools.ietf.org/html/rfc4868,这是最好的秘密密钥是相同的位数的哈希算法(即SHA256密钥应该是256比特/ 32字节).
我可以使用C#中许多不同的随机数生成器之一,或者是否需要生成这些密钥的特定方法.
最后,亚马逊Web服务使用HMACSHA256,但他们提供的(至少对我来说)密钥是320比特/ 40字节(当关键是使用转换为字节UTF-8,看https://github.com/aws/ AWS-SDK-净/斑点/主/ AWSSDK/Amazon.Runtime /内部/认证/ AWS4Signer.cs#L205-L232).有没有理由使用散列算法超过需要,因为它被截断了?