小编Kri*_*ner的帖子

如何在LINQ查询中检查c#7元组?

鉴于:

class Program
{
    private static readonly List<(int a, int b, int c)> Map = new List<(int a, int b, int c)>()
    {
        (1, 1, 2),
        (1, 2, 3),
        (2, 2, 4)
    };

    static void Main(string[] args)
    {
        var result = Map.FirstOrDefault(w => w.a == 4 && w.b == 4);

        if (result == null)
            Console.WriteLine("Not found");
        else
            Console.WriteLine("Found");
    }
}
Run Code Online (Sandbox Code Playgroud)

在上面的示例中,遇到编译器错误if (result == null).

CS0019运算符'=='不能应用于'(int a,int b,int c)'和''类型的操作数

在继续执行"找到"逻辑之前,我该如何检查是否找到了元组?

在使用新的c#7元组之前,我会这样:

class Program
{
    private static readonly List<Tuple<int, int, …
Run Code Online (Sandbox Code Playgroud)

c# linq c#-7.0

41
推荐指数
7
解决办法
1万
查看次数

如何在RunSettings文件中访问TestRunParameters

通过https://msdn.microsoft.com/en-us/library/jj635153.aspx阅读 我创建了一个.RunSettings文件,其中包含一些类似于示例的参数:

  <TestRunParameters>
    <Parameter name="webAppUrl" value="http://localhost" />
    <Parameter name="webAppUserName" value="Admin" />
    <Parameter name="webAppPassword" value="Password" />
  </TestRunParameters>
Run Code Online (Sandbox Code Playgroud)

我计划.RunSettings为每个环境提供一个文件,其中包含适当的URL和凭据,用于在指定的RunSettings文件环境中运行CodedUI测试.

我可以看到从命令行引用我可以运行的设置文件:

vstest.console myTestDll.dll /Settings:Local.RunSettings /Logger:trx
vstest.console myTestDll.dll /Settings:QA.RunSettings /Logger:trx
Run Code Online (Sandbox Code Playgroud)

等等...

但我没有看到任何方式来调用如何TestRunParameters在codedUI测试中实际使用from.

我想要做的是设置测试初始化​​程序,使用它TestRunParameters来确定登录的位置以及要使用的凭据.像这样的东西:

[TestInitialize()]
public void MyTestInitialize()
{

    // I'm unsure how to grab the RunSettings.TestRunParameters below
    string entryUrl = ""; // TestRunParameters.webAppUrl
    string userName = ""; // TestRunParameters.webAppUserName
    string password = ""; // TestRunParameters.webAppPassword

    LoginToPage(entryUrl, userName, password);
}

public void LoginToPage(string entryUrl, string …
Run Code Online (Sandbox Code Playgroud)

c# asp.net coded-ui-tests vs-unit-testing-framework runsettings

35
推荐指数
3
解决办法
2万
查看次数

将excel单元格格式保留为带有"date like"数据的文本

这看起来很愚蠢,但是我无法以#/####格式化字符串的形式获取我的值,而不是将其格式化为excel中的日期.

我正在使用ClosedXML写入excel,并使用以下内容:

// snip
IXLRangeRow tableRow = tableRowRange.Row(1);
tableRow.Cell(1).DataType = XLCellValues.Text;
tableRow.Cell(1).Value = "2/1997";
// snip
Run Code Online (Sandbox Code Playgroud)

查看输出excel表我进入单元格2/1/1997- 即使我在代码中将格式设置为文本,我在excel表中将其作为"日期" - 我通过右键单击单元格检查了这一格式,格式单元格,将"日期"视为格式.

如果我改变了:

// snip
IXLRangeRow tableRow = tableRowRange.Row(1);
tableRow.Cell(1).Value = "2/1997";
tableRow.Cell(1).DataType = XLCellValues.Text;
// snip
Run Code Online (Sandbox Code Playgroud)

我改为35462输出.

我只想让我的文字值2/1997显示在工作表上.请告知如何纠正.

c# asp.net excel closedxml

12
推荐指数
1
解决办法
1万
查看次数

应该在哪里放置IDBConnection以减少重复的代码?

大多数Dapper教程使用私有IDBConnection对象来调用方法,即

private IDbConnection db = new SqlConnection(...)
Run Code Online (Sandbox Code Playgroud)

使用ASP.NET和MVC 5时,我应该在哪里放置它,所以我不必在每个使用Dapper的控制器/存储库中重复它?例如,有没有办法将它放在一个启动类中并使用像ASP.NET Core中的依赖注入,或者在整个应用程序中使用其他技术来访问它?

c# asp.net asp.net-mvc dapper

6
推荐指数
1
解决办法
924
查看次数

在try/catch块中避免警告"变量已声明但从未使用过"

给出C#中的以下代码:

public void CatchExceptionThenThrow()
{
    try
    {
        StartThings();
    }
    catch (Exception)
    {
        throw;
    }
}
Run Code Online (Sandbox Code Playgroud)

我使用dotnetfiddle VB.net转换器将其转换为VB

Public Sub CatchExceptionThenThrow()
    Try
        StartThings()
    Catch As Exception
        Throw
    End Try
End Sub
Run Code Online (Sandbox Code Playgroud)

这会引发编译错误:

Catch As Exception
Run Code Online (Sandbox Code Playgroud)

声明结束预期

然后我改为:

Public Sub CatchExceptionThenThrow()
    Try
        StartThings()
    Catch ex As Exception
        Throw
    End Try
End Sub
Run Code Online (Sandbox Code Playgroud)

但这会产生一个警告"变量已声明但从未使用过".如何在不获取警告的情况下进行throw而不是throw ex在VB中进行操作,同时保留整个堆栈跟踪,就像在第一个C#示例中一样?


所有好的评论,并感谢冗余信息,我意识到完全不需要try/catch,因为无论是否有try/catch都会发生这种情况.问题更多的是出于好奇心的缘故,我认为,在(一个好的代码)现实中没有真正的基础.

我最近在博客文章中看到过类似于异常处理的内容以及为什么要使用throwvs throw ex,并且对于如何在VB中完成相同的代码感到好奇 - 因为我对VB不太强大并且我想要更好地理解它,并且异常处理.

我曾希望能够找到我上面引用的博客文章,但却无法找到.它的要点(产生了问题)可以找到:https://dotnetfiddle.net/741wAi

c# vb.net try-catch stack-trace compiler-warnings

5
推荐指数
1
解决办法
8016
查看次数

Activator.CreateInstance与通用存储库

我正在尝试(我认为)一个工厂,它根据传递给方法的枚举创建一个存储库.看起来像这样:

RepositoryFactory

public class RepositoryFactory
{
    public IRepository<IEntity> GetRepository(FormTypes formType)
    {
        // Represents the IRepository that should be created, based on the form type passed
        var typeToCreate = formType.GetAttribute<EnumTypeAttribute>().Type;

        // return an instance of the form type repository
        IRepository<IEntity> type = Activator.CreateInstance(typeToCreate) as IRepository<IEntity>;

        if (type != null)
            return type;

        throw new ArgumentException(string.Format("No repository found for {0}", nameof(formType)));
    }
}
Run Code Online (Sandbox Code Playgroud)

IRepository

public interface IRepository <T>
    where T : class, IEntity
{
    bool Create(IEnumerable<T> entities);

    IEnumerable<T> Read();

    bool Update(IEnumerable<T> entities);

    bool …
Run Code Online (Sandbox Code Playgroud)

c# generics factory activator

5
推荐指数
1
解决办法
842
查看次数

.netcore 中用户机密的工作流程?

我在 .netcore 中玩弄并尝试使用用户机密存储,这里有一些详细信息:https ://docs.asp.net/en/latest/security/app-secrets.html

我在本地工作时与它相处得很好,但我无法理解如何在团队环境中有效地利用它,以及我是否想从多台计算机上处​​理这个项目。

商店本身(至少在默认情况下)将其配置 json 文件保存在 users/appdata 中(在 Windows 上)。如果您将项目上传到 github,以隐藏您的 API 密钥、连接字符串等,则此功能非常有用。当只​​有我在一台机器上处理项目时,这一切都很棒。但是在团队环境或多台机器上工作时,这是如何工作的?我唯一能想到的就是找到配置文件,将其签入私有仓库,并确保在发生更改时将其替换到正确的目录中。

有没有另一种我不知道的方法来管理这个?

configuration .net-core asp.net-core

5
推荐指数
1
解决办法
826
查看次数

VS2015 中的 Bower 错误“bower requirejs extra-resolution Unnecessary resolution: requirejs#~2.2.0”

我正在尝试使用 durandal,但收到此错误:

在此输入图像描述

错误:

Bower requirejs extra-resolution 不必要的分辨率:requirejs#~2.2.0

鲍尔.json

{
  "name": "asp.net",
  "private": true,
  "dependencies": {
    "underscore": "~1.8.3",
    "bootstrap": "~3.3.6",
    "bootswatch": "3.3.6",
    "jquery": "2.2.3",
    "jquery-validation": "1.15.0",
    "jquery-validation-unobtrusive": "~3.2.6",
    "angular": "1.5.7",
    "angular-route": "~1.5.7",
    "durandal": "~2.1.0",
    "requirejs": "~2.2.0"
  }
}
Run Code Online (Sandbox Code Playgroud)

没有运气找出它的含义或我需要做什么来解决它。RequireJS 已将其放入我的 lib 文件夹中,所以我想知道这是否不是真正的错误?

requirejs bower visual-studio-2015 asp.net-core

5
推荐指数
1
解决办法
2460
查看次数

用于.netcore应用程序的wwwroot之外的静态文件

我在我的@home网络服务器上使用https://github.com/ebekker/ACMESharp作为我的SSL(它是免费的!:O).这是非常手动的,但在维基上注意到它提到了https://github.com/Lone-Coder/letsencrypt-win-simple上的另一个项目,这是一个用于自动申请,下载和安装SSL的GUI证书到您的网络服务器.

GUI用于验证域的方法是您的,通过创建一个随机命名的文件,其中包含一个随机的文本字符串,其中[webroot]/.well-known/[randomFile]包含扩展名.使用.dotnetcore应用程序在此[webroot]上运行,即使遵循IIS下更改"处理程序映射"的说明,我也无法提供该文件.

看起来我可以通过直接导航到他们来提供文件[webRoot]/wwwroot/[whatever]- 所以为什么我不能进入[webroot]/.well-known/[randomFile]

有人知道解决这个问题吗?我可以删除.netcore应用程序,然后运行SSL证书安装,但这个安装需要每2-3个月进行一次,因为它是手动的,我更愿意弄清楚如何以正确的方式做到这一点.

iis ssl static-files staticfilehandler asp.net-core

5
推荐指数
1
解决办法
979
查看次数

VS2017 安装程序 - 包清单未通过签名验证

我已经安装了VS2015,之前在这台机器上安装了VS2017。在 VS2017 上添加扩展似乎完全破坏了我的安装,所以我想接下来要做的是重新安装 VS2017。

哦,我多么希望我没有。

安装程序因“包清单签名验证失败”而失败,我尝试了以下步骤:

运行安装程序时,我会看到:

在此处输入图片说明

在选择要安装的产品之前。

尝试按照https://www.hanselman.com/blog/HowToMakeAnOfflineInstallerForVS2017.aspx 上的“离线安装程序”步骤进行操作时

在我运行的步骤中:

vs_community.exe --layout e:\vs2017offline --lang en-US
Run Code Online (Sandbox Code Playgroud)

我看到(最终)一个控制台窗口:

在此处输入图片说明

安装的日志文件。

dd_setup_*.log:

[0df4:000c][2017-05-24T08:37:22] Setup Engine v1.10.101, Microsoft Windows NT 10.0.10586.0
[0df4:000c][2017-05-24T08:37:22] Command line: "C:\Program Files (x86)\Microsoft Visual Studio\Installer\resources\app\ServiceHub\Hosts\Microsoft.ServiceHub.Host.CLR\vs_installerservice.exe" desktopClr$C94B8CFE-E3FD-4BAF-A941-2866DBB566FE 18a10ed3a2b52a1e605bf4679dbe1364
[0df4:000c][2017-05-24T08:37:24] ManifestVerifier verification: Exception has been thrown by the target of an invocation. Stack:    at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at …
Run Code Online (Sandbox Code Playgroud)

visual-studio visual-studio-2017

5
推荐指数
1
解决办法
1万
查看次数