Tag: .net

生成人类可读/可用,简短但唯一的ID

每天需要处理> 1000但是<10000条新记录 不能使用GUID / UUID,自动递增数字等 理想情况下应该是5或6个字符长,当然可以是阿尔法 如果可用的话,希望重用现有的,众所周知的算法 什么都有

使用通用接口或类的约束

假设我们写一个测试系统。 测试包含任务列表,每个任务包含问题和答案列表。 我们也假定问题或答案不仅可以是文本,而是可以是例如图像。 所以我们使用泛型: public interface IQuestion<T> { T Content { get; } } public interface IAnswer<T> { T Content { get; } bool IsCorrect { get; } } 而当我们创建任务时,就会出现问题: interface ITask<TQuestion, TAnswer> { TQuestion Question { get; } List<TAnswer> Answers { get; } } 如何写TQuestion应该是IQuestion和TAnswer子类型 – IAnswer子类型? 我试过了: interface ITask<TQuestion, TAnswer> where TQuestion : […]

在c#.net中验证Firebase JWT

我正在尝试验证一个由firebase android客户端获得的json Web令牌,并将其传递给运行.net的服务器 在这里的答案之后,我创建了这些方法来验证令牌并提取uid: public static async Task<string> GetUserNameFromTokenIfValid(string jsonWebToken) { const string FirebaseProjectId = "testapp-16ecd"; try { // 1. Get Google signing keys HttpClient client = new HttpClient(); client.BaseAddress = new Uri("https://www.googleapis.com/robot/v1/metadata/"); HttpResponseMessage response = await client.GetAsync("x509/securetoken@system.gserviceaccount.com"); if (!response.IsSuccessStatusCode) { return null; } var x509Data = await response.Content.ReadAsAsync<Dictionary<string, string>>(); SecurityKey[] keys = x509Data.Values.Select(CreateSecurityKeyFromPublicKey).ToArray(); // […]

一个协变类型参数可以在构造函数的输入位置吗?

在这个答案中 , Michael建议将泛型类型参数作为协变量,以允许创建一个空节点。 由于Tree<T>及其子类型的所有属性都是只读的( val ),因此泛型类型参数位于所有输出位置。 但是它在构造函数的输入位置中具有类型参数。 我认为这个代码在C#中不起作用,所以我尝试了一下,而且我感到惊讶,它工作得很好。 // See this: https://stackoverflow.com/questions/36753579/algebraic-data-types-in-kotlin // Short url: https://stackoverflow.com/a/36753782/303685 interface ITree<out T> { } class Tree<T>: ITree<T> { } sealed class Node<T> : Tree<T> { private readonly T _left; private readonly T _right; public Node(T left, T right) { _left = left; _right = right; } public T […]