Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Double generic types and members with non-generic equivalents

Đang mở
#2,757 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
38/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
csharp
Lĩnh vực
cli

Hướng nghiên cứu

Bắt đầu bằng việc xem xét API generic ParseResult.GetValue hiện có và các constructor của Option, lấy các overload dựa trên Type được đề xuất làm phạm vi. So sánh pattern này với API DI không generic, sau đó xác minh rằng các phương án thay thế hỗ trợ use case AOT không dùng reflection được nêu, đồng thời vẫn giữ nguyên hành vi generic.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Examples

// In addition to these:
T? ParseResult.GetValue<T>(Option<T> option);
new Option<T>(string name, params string[] aliases);
// etc...

// Make it possible to call them like this:
object? ParseResult.GetValue(Option option, Type valueType);
new Option(Type valueType, string name, params string[] aliases);

This is somewhat similar to what we have in DI:

// We can add service like this:
services.AddSignleton<T>();

// Or this:
services.AddSingleton(Type serviceType);

Justification

I am making my own library that adds proper hosting API, making using System.CommandLine similar to any regular ASP.NET project.
For example:

Program.cs

app.MapCommand("foo", ([Option] FileInfo file) =>
{
    foreach (string line in File.ReadLines(file.FullName))
        Console.WriteLine(line);
});

ShellApplication.cs

public void MapCommand(string name, Delegate command)
{
	RootCommand rootCommand = [];
	ParameterInfo[] parameters = command.Method.GetParameters();

	foreach (ParameterInfo parameterInfo in parameters)
	{
		if (parameterInfo.GetCustomAttribute<OptionAttribute>() is not null)
		{
			Option option = (typeof(Option<>).MakeGenericType(parameterInfo.ParameterType).GetConstructor([typeof(string), typeof(string[])])!.Invoke([name]) as Option)!;
			rootCommand.Options.Add(option);
		}
	}
	// etc...
}

At first glance this looks fine – just make a generic type using reflection. Only the problem is that this is not AOT compatible and will cause exceptions. Adding alternatives that accept types as parameters would solve this.

Additional context

I've glossed over the codebase, and it doesn't look like it will be that hard to implement changes. If that's ok with you, I can prepare a PR myself.

Ngôn ngữ chính
C#
Star
3.7k
Fork
434
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của dotnet/command-line-api

Tất cả issue của dotnet/command-line-api

Issue tương tự

Thêm issue về C#

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.