我正在尝试使用 C# 读取 Windows 电脑上的所有系统信息。这是我的代码:
public static string GetSystemInfo()
{
String command = "systeminfo";
ProcessStartInfo cmdsi = new ProcessStartInfo("cmd.exe");
cmdsi.Arguments = command;
Process cmd = Process.Start(cmdsi);
cmd.WaitForExit();
return cmd.StandardOutput.ReadToEnd();
}
Run Code Online (Sandbox Code Playgroud)
但它只是打开一个控制台,不执行systeminfo命令。
如何解决这个问题?
我目前正在尝试用 C# 将不同的东西翻译成不同的语言。我确信有比我目前使用的方法更好的方法。我在枚举文档中进行了搜索,但并没有真正找到我想要的东西。我想要英文索引,但我想要语言中的值作为参数传递。是否存在一种方法可以将其放入 .. 一行?
即 day("MO" : fr="lundi", eng="monday", de="montag")或类似的东西(我愿意接受建议)
private void SetDays(string language)
{
switch(language)
{
case "french":
days.Add( "MO", "lundi");
days.Add( "TU", "mardi" );
days.Add( "WE", "mercredi" );
days.Add( "TH", "jeudi" );
days.Add( "FR", "vendredi" );
days.Add( "SA", "samedi" );
days.Add( "SU", "dimanche" );
break;
case "english":
days.Add( "MO", "monday" );
days.Add( "TU", "tuesday" );
days.Add( "WE", "wednesday" );
days.Add( "TH", "thursday" );
days.Add( "FR", "friday" );
days.Add( "SA", "saturday" );
days.Add( "SU", "sunday" );
break; …Run Code Online (Sandbox Code Playgroud) 是否有可能写入可能会接受一个函数的任何类型的object作为参数?
就像是
private bool isItAString(var Input)
{
string example = "example";
return Input.GetType() == example.GetType();
}
Run Code Online (Sandbox Code Playgroud)
关键字var是无效在这种情况下和它希望它是对象的特定类型。
Dictionary在 .NET 框架中是否有一种使用 Linq将一个映射到另一个的优雅方法?
这可以通过枚举来完成foreach:
var d1 = new Dictionary<string, string>() {
{ "One", "1" },
{ "Two", "2" }
};
// map dictionary 1 to dictionary 2 without LINQ
var d2 = new Dictionary<string, int>();
foreach(var kvp in d1) {
d2.Add(kvp.Value, int.Parse(kvp.Value));
}
Run Code Online (Sandbox Code Playgroud)
...但我正在寻找某种方式来完成 LINQ:
// DOES NOT WORK
Dictionary<string, int> d2 =
d1.Select(kvp => {
return new KeyValuePair<string, int>(kvp.Key, int.Parse(kvp.Value));
})
Run Code Online (Sandbox Code Playgroud) 我尝试将列表转换为字符串。但我遇到错误,我尝试这样做,但时间太长了,我需要你的帮助
我想做什么:
List<string> list = new List<string> {"a","a","a" };
Run Code Online (Sandbox Code Playgroud)
我想得到这样的字符串:
我的字符串将等于:
"["a","a","a"]"
Run Code Online (Sandbox Code Playgroud)
我怎么能在一行中做到这一点?
谢谢!!!!!!
我想写一个这样的方法。但是,该语言不喜欢T obj = null方法参数中的 。它也不喜欢obj == null。我试过T : object T : INullable无济于事。有什么办法可以让 C# 接受这个吗?
public virtual static async Task<T> Get<T>(T obj = null) where T : struct
{
if (obj == null)
{
// Do stuff
}
else
{
// Do other stuff
}
}
Run Code Online (Sandbox Code Playgroud) 我有以下测试类:
using System;
using DDDSample1.Domain.DriverDuties;
using DDDSample1.Domain.Shared;
using DDDSample1.Controllers;
using System.Collections.Generic;
using Moq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace IntegrationTests
{
public class DriverDutyControllerTest
{
[TestMethod]
public async void GetByIdTest()
{
var repo = new Mock<IDriverDutyRepository>();
var unitOfWork = new Mock<IUnitOfWork>();
var service = new DriverDutyService(unitOfWork.Object, repo.Object);
var controller = new DriverDutiesController(service);
DriverDutyId id = new DriverDutyId("id1");
string key = "keyDD1";
string driver = "DriverDD";
List<String> workblocks = new List<String>();
workblocks.Add("wb1");
workblocks.Add("wb2");
workblocks.Add("wb3");
var ddDto = new CreatingDriverDutyDto(key, driver, workblocks.ToArray());
var dd = …Run Code Online (Sandbox Code Playgroud) 我在从列表中随机选择时遇到问题。当我运行程序时,它会一遍又一遍地写入相同的字符串,当我希望它每次都想出不同的随机字符串时。下面是一个例子:
using System;
using System.Collections.Generic;
namespace List
{
class Program
{
static void Main(string[] args)
{
var letterList = new List<string>
{
"A","B","C","D"
};
Random r = new Random();
int letterListIndex = r.Next(letterList.Count);
for (int i = 0; i < 10; i++) {
Console.WriteLine(letterList[letterListIndex]);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
例如:当我运行这段代码时,它会写 10 次“B”。我希望它每次都想出 10 个不同的字母。我知道你可以写:
int letterListIndex1 = r.Next(letterList.Count);
int letterListIndex2 = r.Next(letterList.Count);
int letterListIndex3 = r.Next(letterList.Count);
Console.WriteLine(letterList[letterListIndex1]);
Console.WriteLine(letterList[letterListIndex2]);
Console.WriteLine(letterList[letterListIndex3]);
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有更简单的方法来做到这一点。
谢谢。
我有一个大的 switch 语句,它的圈复杂度为 31,它必须重构为至少 25。这是错误:严重性代码描述项目文件行抑制状态抑制状态错误 CA1502 'Worker.StartListening()' has a cyclomatic复杂度为 31。重写或重构方法以将复杂度降低到 25。
谢谢!
这是代码:
public void StartListening()
{
var consumerSettingsSection= this.configurationManager.GetSection<ConsumerSettingsSection>("appZ/consumer");
foreach (var setting in consumerSettingsSection.QueueSettings)
{
var eventType = ConsumedEventType.NotSpecified;
switch (setting.Name)
{
case "A":
eventType = ConsumedEventType.A;
break;
case "B":
eventType = ConsumedEventType.B;
break;
case "C":
eventType = ConsumedEventType.C;
break;
case "D":
eventType = ConsumedEventType.D;
break;
case "E":
eventType = ConsumedEventType.E;
break;
case "F":
eventType = ConsumedEventType.F;
break;
case "G":
eventType = ConsumedEventType.G;
break;
case "H":
eventType = …Run Code Online (Sandbox Code Playgroud) 我相信 C# 的对象初始化顺序是这样的:
下面你会看到一个简单的测试程序和它在我运行时产生的输出。
public class UiBase
{
protected static string Something = "Hello";
public UiBase()
{
Console.WriteLine(this.ToString());
}
}
public class Point : UiBase
{
private int X = -1;
private int Y = -1;
static Point()
{
Something = "Bar";
}
public Point(int x, int y)
{
X = x;
Y = y;
}
public override string ToString()
{
return $"Point:{X}/{Y}/{Something}";
}
}
public static class Program{
public static void …Run Code Online (Sandbox Code Playgroud) c# ×10
.net ×5
list ×2
cmd ×1
dictionary ×1
enumeration ×1
generics ×1
linq ×1
process ×1
refactoring ×1
string ×1
testing ×1
types ×1