我有一个名为 InstrumentConfigValues 的类,其属性具有实现接口的类型。现在我有一个名为 InstrumentConfig 的枚举,它具有一组值。这些值就像 json 文件中的键。我想映射类似[JsonProperty(InstrumentConfig.LowDiskpace.ToString()].
出于某种原因,它不允许这样做并抱怨说:
属性参数必须是常量表达式
我特别提到了许多帖子JsonStringEnumConverter。但是如何使用枚举键映射每个属性。我也看到了这篇文章JsonSerializationSettings但无法与我的问题相关联。请帮忙/
public class InstrumentConfigValues : IInstrumentConfig
{
public double SpaceNeededForSingleRun
{
get; set;
}
public int NumberOfInputSlots
{
get; set;
}
public int SupportedChannelCount
{
get; set;
}
}
//I want this inheritance as some other class wants to access the values.
public abstract class InstrumentConfigReadWrite : InstrumentConfigValues
{
protected ReturnCodes PopulateValuesFromJObject(JObject jObject, string path)
{
try
{
if (JsonConvert.DeserializeObject<InstrumentConfigValues>(jObject.ToString()) == null)
{
return ReturnCodes.ErrorReadingFile; …Run Code Online (Sandbox Code Playgroud) 我需要在c#中创建将转换为以下JSON的对象:
{
"Order": {
"CustomerCode": "9999999",
"Note": "New Order for Test -- UNIT TEST",
"Stops": {
"Stop": {
"Sequence": "1",
"StopType": "P",
"Name": "CVS"
},
"Stop": {
"OrderStopID": "5",
"Sequence": "2",
"StopType": "D",
}
},
"Jobs": {
"Job": {
"Sequence": "1",
"Drivers": {
"Driver": {
"Sequence": "1",
"DriverCode": "09"
},
"Driver": {
"Sequence": "2"
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我创建的代表此的对象:
public class RootObject
{
public Order Order { get; set; }
}
public class Order
{
public string CustomerCode …Run Code Online (Sandbox Code Playgroud) 我有一个像这样的嵌套类结构
public static class Animals{
private static class Dolphins
{
public static int GetAge()
{
}
}
private static class Cats
{
public static int GetAge()
{
}
}
private static class Dogs
{
public static int GetAge()
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
是否有可能在for循环中以编程方式获取子类名称,因此我可以为所有它们调用GetAge()方法?像这样(伪代码):
foreach(subclass in Animals)
{
subclass.GetAge()
}
Run Code Online (Sandbox Code Playgroud)
我通过反射进行了尝试,但是我能够获得基类的后代类,而不是像上面的静态基类中嵌套的类。
提前致谢,
我有界面:
public Interface Inter1
{
....
Task<(ITestBulk testBulk, bool resultBulk)> GetBulk(int par1, int par2, string par3);
}
Run Code Online (Sandbox Code Playgroud)
然后得到了一些方法:
class MyClass{
internal bool m_resultBulk;
internal async void NewBulk()
{
....
(ITestBulk, m_resultBulk) = await service.GetBulk(1, 2, "ggg");
}
}
Run Code Online (Sandbox Code Playgroud)
但m_resultBulk找不到。我做错了什么?
不确定我做错了什么。我一直在互联网上寻找如何使用 lambda 有效地获取基于另一个属性的属性值列表。
假设我有一个包含教师姓名和房间 ID 的教室列表:
public class ClassRoom
{
public string Teacher;
public int RoomId;
}
List<ClassRoom> classRooms = new List<ClassRoom>();
classRooms.Add(new ClassRoom() { RoomId = 2000, Teacher = "Mr. Taylor" });
classRooms.Add(new ClassRoom() { RoomId = 2010, Teacher = "Mrs. Lee" });
classRooms.Add(new ClassRoom() { RoomId = 3050, Teacher = "Mrs. McNamara" });
classRooms.Add(new ClassRoom() { RoomId = 4090, Teacher = "Mr. Taylor" });
Run Code Online (Sandbox Code Playgroud)
然后,我需要一个特定老师的房间 ID 列表。
List<int> RoomIds = new List<int>();
foreach(ClassRoom classRoom in classRooms)
{
if(classRoom.Teacher …Run Code Online (Sandbox Code Playgroud) 我有一个关于 invoke 用法的一般问题。我的大多数 C# winforms 项目都有一个后台工作人员和一个 UI。很快我意识到我需要来自后台工作人员 UI 的信息,或者我需要更改我的后台工作人员的 UI。例子:
//example invoke usage to get information from UI (dateMatch = CheckBox, fromDate&toDate = Datepicker)
bool isDateMatch = false;
dateMatch.Invoke(new MethodInvoker(delegate { isDateMatch = dateMatch.Checked; }));
DateTime fromDt = new DateTime();
fromDate.Invoke(new MethodInvoker(delegate { fromDt = fromDate.Value; }));
DateTime toDt = new DateTime();
toDate.Invoke(new MethodInvoker(delegate { toDt = toDate.Value; }));
Run Code Online (Sandbox Code Playgroud)
//example invoke usage to change UI (statusTxt = TextBox, progressBar = ProgressBar)
private void changeStatus(string statusTextV, bool enableProgressBar)
{
statusTxt.Invoke(new MethodInvoker(delegate …Run Code Online (Sandbox Code Playgroud) 来自 .NET 4 的 ASP.NET 5 MVC 项目使用 Substring 是源代码,如
id = row.Categoryid?.Substring(0, 1) == "$" ? row.Categoryid?.Substring(startIndex: 1) :
row.Categoryid?.Substring(0)
Run Code Online (Sandbox Code Playgroud)
Visual Studio 2019 质量检查员抛出消息
IDE0057 子串可以简化
在
开始索引:1
应用建议的修复后
使用范围运算符
代码重构为
id = row.Categoryid?.Substring(0, 1) == "$" ? row.Categoryid?.Substring[1..] :
row.Categoryid?.Substring(0)
Run Code Online (Sandbox Code Playgroud)
抛出编译错误
错误 CS0021 无法将带有 [] 的索引应用于“方法组”类型的表达式
如何解决这个问题,以便重构创建正确的代码?
c# visual-studio asp.net-core-mvc roslyn-code-analysis asp.net-core
当处理无法通过 完成的文件时StreamReader,通过 允许哪些功能,反之亦然?我检查了文档,它们都有读/写选项,包括更高级的选项。那么我什么时候应该使用它们呢?StreamWriterFileStream
我有一个自定义通用数组/容器,它有一个排序方法:
public void Sort()
{
for (int i = 0; i < Count; i++)
{
int min = i;
for (int j = i + 1; j < Count; j++)
if (Items[j].CompareTo(Items[min]) < 0)
min = j;
if (min == i) continue;
T temp = Items[i];
Items[i] = Items[min];
Items[min] = temp;
}
}
Run Code Online (Sandbox Code Playgroud)
Items是T对象的数组.T实现IComparable并具有CompareTo方法:
public int CompareTo(object obj)
{
if (!(obj is Player))
return -1;
Player player = (Player)obj;
if (Name.CompareTo(player.Name) < 0)
return -1;
else if (Name.CompareTo(player.Name) == …Run Code Online (Sandbox Code Playgroud) 我有这个字符串:
var a = "a new test string today";
Run Code Online (Sandbox Code Playgroud)
我如何解析一个只包含单词的另一个字符串
"a new test"
Run Code Online (Sandbox Code Playgroud) c# ×10
json.net ×2
.net ×1
asp.net-core ×1
async-await ×1
file ×1
filestream ×1
generics ×1
interface ×1
invoke ×1
json ×1
lambda ×1
linq ×1
list ×1
streamreader ×1
streamwriter ×1
task ×1
tuples ×1
winforms ×1