如何从十六进制颜色代码中获取颜色(例如#FFDFD991
)?
我正在读取文件并获得十六进制颜色代码.我需要System.Windows.Media.Color
为十六进制颜色代码创建相应的实例.框架中是否有内置方法来执行此操作?
我正在测试从Dictionary VS列表中获取数据的速度.
我用这段代码来测试:
internal class Program
{
private static void Main(string[] args)
{
var stopwatch = new Stopwatch();
List<Grade> grades = Grade.GetData().ToList();
List<Student> students = Student.GetStudents().ToList();
stopwatch.Start();
foreach (Student student in students)
{
student.Grade = grades.Single(x => x.StudentId == student.Id).Value;
}
stopwatch.Stop();
Console.WriteLine("Using list {0}", stopwatch.Elapsed);
stopwatch.Reset();
students = Student.GetStudents().ToList();
stopwatch.Start();
Dictionary<Guid, string> dic = Grade.GetData().ToDictionary(x => x.StudentId, x => x.Value);
foreach (Student student in students)
{
student.Grade = dic[student.Id];
}
stopwatch.Stop();
Console.WriteLine("Using dictionary {0}", stopwatch.Elapsed);
Console.ReadKey();
}
}
public …
Run Code Online (Sandbox Code Playgroud) 有没有办法在.Net中检查路径中的字符串是否包含无效字符?我知道我可以迭代Path.InvalidPathChars中的每个字符,看看我的String是否包含一个,但我更喜欢一个简单的,也许更正式的解决方案.
有吗?
我发现如果我只检查Get,我仍会得到一个例外
更新:
我发现GetInvalidPathChars不会覆盖每个无效的路径字符.GetInvalidFileNameChars还有5个,包括'?',我遇到过.我要改用它,我会报告它是否也证明是不合适的.
更新2:
GetInvalidFileNameChars绝对不是我想要的.它包含':',任何绝对路径将包含("C:\ whatever").我想我毕竟不得不使用GetInvalidPathChars,并添加'?' 以及任何其他导致我出现问题的角色.欢迎提供更好的解
在 .NET 5 的 Program.cs 中,您可以在 Main(string[] args) 方法下添加方法。在 .NET 6 中,Main 方法存在,但默认情况下并未实际包含在 Program.cs 文件中。为此,我想知道如何向 Program.cs 添加方法。例如:
// .NET 5
public class Program
{
static void Main(string[] args)
{
// body
}
public static void MyMethodHere()
{
// method body
}
}
Run Code Online (Sandbox Code Playgroud)
如何在 Program.cs 的 .NET 6 中添加 MyMethodHere() 类,而无需手动键入整个 Program 类和 Main 方法?
由于在C#中可以使用以下代码,因此我认为string是否实际上是一个chars数组:
string a="TEST";
char C=a[0]; // will be T
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Linq to Entities查询数据库上下文,我收到此错误:
INQ to Entities无法识别方法'Int32 Int32(System.String)'方法,并且此方法无法转换为商店表达式.
码:
public IEnumerable<CourseNames> GetCourseName()
{
var course = from o in entities.UniversityCourses
select new CourseNames
{
CourseID = Convert.ToInt32(o.CourseID),
CourseName = o.CourseName,
};
return course.ToList();
}
Run Code Online (Sandbox Code Playgroud)
看到这个之后我就这样试过了
public IEnumerable<CourseNames> GetCourseName()
{
var temp = Convert.ToInt32(o.CourseID);
var course = from o in entities.UniversityCourses
select new CourseNames
{
CourseID = temp,
CourseName = o.CourseName,
};
return course.ToList();
}
Run Code Online (Sandbox Code Playgroud)
但它抛出一个错误:
"当前上下文中不存在名称'o'"
这是我的课程代码 GetCourseName
namespace IronwoodWeb
{
public class CourseNames
{
public int CourseID { …
Run Code Online (Sandbox Code Playgroud) 我有一个缩略图生成器lambda函数,我正在尝试更新到.NET Core 2.0,但是在使用Microsoft的System.Drawing.Common
NuGet包时遇到以下错误:
TypeInitializationException
'Gdip'的类型初始化程序引发了异常.在TestFailExample的System.Drawing.SafeNativeMethods.Gdip.GdipCreateBitmapFromScan0(Int32宽度,Int32高度,Int32步长,Int32格式,HandleRef scan0,IntPtr和位图)处于System.Drawing.Bitmap..ctor(Int32宽度,Int32高度,PixelFormat格式)处.Function.FunctionHandler(String输入,ILambdaContext上下文)在C:\ work\graphics\TestFailExample\Function.cs:第25行在lambda_method(Closure,Stream,Stream,LambdaContextInternal)
引起的
DllNotFoundException
无法加载DLL'libdl':找不到指定的模块或其中一个依赖项.\n(来自HRESULT的异常:0x8007007E),位于System.Drawing.SafeNativeMethods的Interop.Libdl.dlopen(String fileName,Int32标志). System.Drawing.SafeNativeMethods.Gdip..cctor()中的Gdip.LoadNativeLibrary()
我已经看到了这个问题,但没有解决方案.
重现问题的最小代码是:
public string FunctionHandler(string input, ILambdaContext context)
{
using (var bmp = new Bitmap(100, 100))
{
return bmp.Width.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
只需创建一个.NET Core 2.0 Lambda函数项目,添加对System.Drawing.Common
NuGet包的引用,并用上面的代码替换函数处理程序.将其放在AWS上并运行它以获取错误.我已经注意到,在您尝试实际使用它之前,引用该包不会导致问题,但这可能归结为编译器优化.
我已经将MCVE打包到一个项目中并将其上传到GitHub ,以简化人们为重现问题而必须完成的步骤.
我可以看到/lib64/libdl.so.2
存在,但/lib64/libdl.so
没有.由于符号链接似乎不可能(只读文件系统),我不知道如何解决这个问题.我尝试使用LD_LIBRARY_PATH
环境变量,通过创建一个文件夹/tmp
并将文件符号链接到那里作为函数的第一件事.不幸的是,它似乎在这里查找所有库,因此该函数根本不运行.我也试着设置LD_LIBRARY_PATH
到/var/lang/lib:/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib:/tmp
,虽然我现在可以再次运行该功能,这仍然没有帮助,我只是得到相同的Gdip错误.
我注意到/ var/task/lib已经包含在LD_LIBRARY_PATH中,所以我尝试用我的函数打包libdl.so和libgdiplus.so,但这也失败了,这次说明GdiplusStartup
找不到入口点libdgiplus.so
.这些文件不是来自Amazon Linux实例,因此我现在尝试安装Mono并从Amazon Linux实例获取它们.这没有帮助.
我已经尝试过使用CoreCompat 绘图库,但是这也报告了与之相关的问题libgdiplus.so
,即使我尝试将其与函数捆绑在一起.
我已经在我自己的Linux实例上尝试过并且可以确认它是System.Drawing.Common
有效的.
是否有一些聪明的解决方案可以让我System.Drawing.Common
在AWS Lambda 上使用?还有另一种方法可以捏造我的lambda函数来拥有libdl并工作吗?
假设我有:
public class Animal
{
virtual public void Attack() {};
}
public class Lion : Animal
{
public override void Attack() { base.Attack(); }
}
public class Boar : Animal
{
public override void Attack() { base.Attack(); }
}
Run Code Online (Sandbox Code Playgroud)
以及这两种动物的容器:
Dictionary<int, Lion> lions;
Dictionary<int, Boar> boars;
Run Code Online (Sandbox Code Playgroud)
有没有办法投射“狮子”和“野猪”,以便我可以将它们传递给这样的函数?
void IterateTable(Dictionary<int, Animal> dictionary)
{
foreach(var entry in dictionary)
entry.Value.Attack();
}
Run Code Online (Sandbox Code Playgroud) 我正在阅读这篇关于 DateTime 相关格式支持的 MSDocs 文章 https://learn.microsoft.com/en-us/dotnet/standard/datetime/system-text-json-support#support-for-the-iso-8601- 12019-格式
我试图弄清楚默认配置没有按预期工作,或者我一定错过了一些东西。
我是说:
DateTimeOffset.Parse("2021-03-17T12:03:14+0000");
Run Code Online (Sandbox Code Playgroud)
效果很好。
但
JsonSerializer.Deserialize<TestType>(@"{ ""CreationDate"": ""2021-03-17T12:03:14+0000"" }");
Run Code Online (Sandbox Code Playgroud)
没有。
例子:
using System;
using System.Text.Json;
using System.Threading.Tasks;
namespace CSharpPlayground
{
public record TestType(DateTimeOffset CreationDate);
public static class Program
{
public static void Main()
{
var dto = DateTimeOffset.Parse("2021-03-17T12:03:14+0000");
Console.WriteLine(dto);
var testType = JsonSerializer.Deserialize<TestType>(@"{ ""CreationDate"": ""2021-03-17T12:03:14+0000"" }");
Console.WriteLine(testType.CreationDate);
}
}
}
Run Code Online (Sandbox Code Playgroud)
抛出以下异常:
System.Text.Json.JsonException: The JSON value could not be converted to CSharpPlayground.TestType. Path: $.CreationDate | LineNumber: 0 | BytePositionInLine: 44.
---> System.FormatException: The JSON …
Run Code Online (Sandbox Code Playgroud) 有没有办法在不使用乘法的情况下将字符串转换为整数。int.Parse() 的实现也使用乘法。我还有其他类似的问题,您可以在其中手动将字符串转换为 int,但这也需要将数字乘以 10 为基数。这是我在其中一次采访中遇到的一个面试问题,我似乎找不到任何关于此的答案。