小编Joh*_*ica的帖子

锁定非静态方法

我可以lock非静态方式使用对象吗?另一方面,此代码是线程安全的吗?

static readonly object _object = new object();  
public void Get()  
{  
  lock (_object)  
  {  
    ...  
  }  
}
Run Code Online (Sandbox Code Playgroud)

c#

4
推荐指数
1
解决办法
118
查看次数

如何在控制台窗口中将字符串设为粗体?

所以我试图找到一个简单的代码,使我的代码以粗体打印。当我在互联网上搜索时,所有这些都非常复杂,甚至不值得。有没有更简单的方法来制作字符串或只是一个 Console.WriteLine(" "); 胆大?

Console.Write("Type in the number of people to create: ");
int time = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\nGenerating " + time + " people please stand by...");
System.Threading.Thread.Sleep(3000);
Console.Clear();
Run Code Online (Sandbox Code Playgroud)

c#

4
推荐指数
1
解决办法
2161
查看次数

使用 JsonConverter 在 C# 中自定义 JSON 反序列化

我在 .Net Core 中有两个课程

班上Ownership

namespace CustomStoreDatabase.Models
{
    public class Ownership
    {
        public string OwnershipId { get; set; }
        public List<string> TextOutput { get; set; }
        public DateTime DateTime { get; set; }
        public TimeSpan MeanInterval { get; set; }// Like long ticks, TimeSpan.FromTicks(Int64), TimeSpan.Ticks
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要使用方法和来显示MeanInterval长刻度。TimeSpan.FromTicks(Int64)TimeSpan.Ticks

我的自定义 JsonConverter

using CustomStoreDatabase.Models;
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace CustomStoreDatabase.Util
{
    public class OwnershipJSonConverter : JsonConverter<Ownership>
    {
        public override bool CanConvert(Type typeToConvert)
        {
            return true; …
Run Code Online (Sandbox Code Playgroud)

c# deserialization jsonconverter system.text.json

4
推荐指数
1
解决办法
5866
查看次数

无法从方法组转换为 Int32

我希望我的小型数学程序看起来非常时尚,我的意思是在该Main方法下我有以下方法:

Greet()
UserInput1()
UserInput2()
Result()
Run Code Online (Sandbox Code Playgroud)

Greet()我只是说“你好”,UserInput1()我想收集第一个数字,UserInput2()我想收集第二个数字,Result()我想打印UserInput1 + UserInput2. 我可以收集数字,UserInput 1 and 2但如果不在函数Result()下为它们分配值,我似乎无法将它们发送给它们Main()

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Greet();
            firstNumber();
            secondNumber();
            result(firstNumber, secondNumber);
            Console.ReadKey();
        }

        public static void Greet()
        {
            Console.WriteLine("Hello, pls insert two numbers");
        }

        public static int firstNumber()
        {
            int num01 = Convert.ToInt32(Console.ReadLine());
            return num01;
        }

        public static int secondNumber()
        {
            int num02 = Convert.ToInt32(Console.ReadLine()); …
Run Code Online (Sandbox Code Playgroud)

c# methods int compiler-errors

3
推荐指数
1
解决办法
4912
查看次数

如何将整个MailKit MIME消息另存为字节数组

我正在构建一个简单的.net MailKit IMAP客户端。而不是一次又一次地从IMAP服务器提取电子邮件,是否可以将整个MailKit MIME消息(包括附件在内的完整消息)存储为字节数组?如果是这样,怎么办?

然后,我可以将其写入MySql或文件中,然后将其重新用于测试代码更改。

.net c# mailkit

3
推荐指数
1
解决办法
964
查看次数

如何使用linq删除项目

我有以下数组,它表示我试图从某个数据结构实例中删除的项目:

string[] diseasesToRemove = new string[] { "D1", "D3", "D5" };
Run Code Online (Sandbox Code Playgroud)

我可以使用以下方法完成:

for (int i = 0; i < healthGroup.DiseaseGroups.Length; i++)
{
    var dgDiseases = new List<Disease>(healthGroup.DiseaseGroups[i].Diseases);
    for (int j = 0; j < dgDiseases.Count; j++)
    {
        if (diseasesToRemove.Contains(dgDiseases[j].Name))
        {                 
            dgDiseases.RemoveAt(j);
            j--;
        }
    }
    healthGroup.DiseaseGroups[i].Diseases = dgDiseases.ToArray();
}
Run Code Online (Sandbox Code Playgroud)

但是,我确信使用Linq或其他方法有更好的方法.在那儿?

以下是课程:

public class HealthGroup
{
    public DiseaseGroup[] DiseaseGroups { get; set; }

    public HealthGroup(DiseaseGroup[] diseaseGroups)
    {
        DiseaseGroups = diseaseGroups;
    }
}

public class DiseaseGroup
{
    public string Name { get; set; …
Run Code Online (Sandbox Code Playgroud)

c# linq filter

3
推荐指数
1
解决办法
107
查看次数

删除图像中的所有水平线和垂直线

我想删除所有水平和垂直线,但一些小的垂直线没有被删除。添加输入和输出图像以及下面的代码。

            string ImageUrl = @"C:\Users\Jayant\Desktop\test images\rtaImage.tiff";
            Image<Bgr, Byte> image = new Image<Bgr, byte>(ImageUrl);
            Image<Bgr, byte> res = image.Copy();

            LineSegment2D[] lines =
                image
                    .Convert<Gray, byte>()
                    .Canny(16, 16)
                    .HoughLinesBinary(1, Math.PI / 16, 10, 50, 1)[0];

            foreach (LineSegment2D line in lines)
            {
                res.Draw(line, new Bgr(System.Drawing.Color.White), 2);
            }

            res.Save(ImageUrl);
Run Code Online (Sandbox Code Playgroud)

我想删除所有水平和垂直线,但一些小的垂直线没有被删除。添加上述代码的输入和输出。

输入图像:

在此输入图像描述

输出图像:

在此输入图像描述

如果您发现一些垂直线没有被删除。我在 Visual Studio 中使用 emgu.cv 库,代码是 C# 。任何不使用 emgu 的解决方案也将受到赞赏

c# opencv image-processing emgucv

3
推荐指数
1
解决办法
5100
查看次数

代码正在修改错误的变量......为什么?

我有一个奇怪的事情,我正在做的一些代码是修改副本和原始列表..我已尽可能地解决问题,只能在单个文件中显示错误.虽然我的现实世界的例子我们要复杂得多......但是根本就是这个问题.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestingRandomShit
{
    class Program
    {
        private static string rawInput;
        private static List<string> rawList;
        private static List<string> modifiedList;

        static void Main(string[] args)
        {
            rawInput = "this is a listing of cats";

            rawList = new List<string>();
            rawList.Add("this");
            rawList.Add("is");
            rawList.Add("a");
            rawList.Add("listing");
            rawList.Add("of");
            rawList.Add("cats");

            PrintAll();

            modifiedList = ModIt(rawList);

            Console.WriteLine("\n\n**** Mod List Code has been run **** \n\n");
            PrintAll();
        }

        public static List<string> ModIt(List<string> wordlist)
        {

            List<string> huh = new List<string>();
            huh = …
Run Code Online (Sandbox Code Playgroud)

c#

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

.Net Core 3.0通过Reflection调用.Net Framework 4.8程序

我有一个 .net Core 3.0 应用程序尝试通过反射调用 .Net Framework 4.8。我们的目标是使用 ConfigurationManager 读取加密的 AppSettings.config (xml),这在 .Net Core 中是不可能的。未加密是,但已加密(CipherData),否。

我访问静态类和方法的反射代码如下所示:

        Assembly assembly = Assembly.LoadFrom(exeFullName);
        MethodInfo method = assembly.GetType(nameSpacenClass).GetMethod(methodName);
        if (method != null)
        {
            object rtnVal = method.Invoke(null, new object[] { jsonParms });
            return rtnVal;
        }
        else
            return null;
Run Code Online (Sandbox Code Playgroud)

method.Invoke 错误并显示以下异常消息: FileNotFoundException: Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'。该系统找不到指定的文件。

所以我在.Net Framework 4.8中编写了一个代理程序,并使用相同的代码来访问通过反射读取加密配置的代码,并且工作正常。

然后,我编写了 .net Core 3.0 代码以通过反射访问该代理,但失败了。代理 .Net Framework 4.8 没有任何引用。它是准系统。然而,即使代理不需要,核心仍然需要 ConfigurationManager。Config 程序只有一个引用 - System.Configuration。

Core 3.0 => 加密 App.config => …

.net c# asp.net .net-core asp.net-core

3
推荐指数
1
解决办法
1092
查看次数

如何在 .NET Core 3.1.1 中将 json 更改为 CamelCase?

升级到 3.1.1 后,AddNewsoftJson 不见了,现在怎么改json大小写格式?

services.AddControllers()
    .AddNewtonsoftJson(options =>
    {
        options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    });
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core

3
推荐指数
1
解决办法
4688
查看次数