gpt4 book ai didi

c# - 递归拉取所有构造函数选项?

转载 作者:行者123 更新时间:2023-12-03 20:02:29 25 4
gpt4 key购买 nike

假设我有一个包含一个对象的对象,每个对象都有一个或多个构造函数:

 Foo(int p1, Bar p2);

Bar(String p3, String p4);

Bar(int p5, int p6);
我想在 UI 中显示构建 Foo 需要哪些参数。我认为有以下三种选择:
int p1, Bar p2
或者
int p1, String p3, String p4
或者
int p1, int p5, int p6
显然,随着嵌套的对象越来越多,它变得更加复杂。有没有办法拉出所有这些选项?在 C# 中怎么样?如果您可以展示如何在上面给出了选项 2 或 3 的情况下动态构建 Foo,则加分。

最佳答案

您可以(如评论中所建议的)使用反射和递归来实现您想要的。下降到另一个递归级别的标准取决于您。我已经实现了这些类来模仿你的类:

public class Foo
{
public Foo(int p1, Bar p2)
{ }
}
public class Bar
{
public Bar(string p3, string p4)
{ }
public Bar(int p5, int p6)
{ }
}
然后我实现了这个递归方法来打印可能的结构:
public static void PrintConstructors(Type t, string currentOutput)
{
foreach (var ctor in t.GetConstructors())
{
var construct = currentOutput;
foreach (var param in ctor.GetParameters())
{
if (param.ParameterType.IsClass && param.ParameterType != typeof(string))
{ PrintConstructors(param.ParameterType, construct); }
construct += $"{param.ParameterType} {param.Name}, ";
}
if (!string.IsNullOrEmpty(construct))
{ Console.WriteLine(construct.TrimEnd(',', ' ')); }
}
}
调用 PrintConstructors(typeof(Foo), "");产生以下输出:
System.Int32 p1, System.String p3, System.String p4
System.Int32 p1, System.Int32 p5, System.Int32 p6
System.Int32 p1, ConsoleApp.Bar p2
编辑 :当构造函数中有多个复杂类型时,另一种选择是(不检查循环依赖项,如@Michał Turczyn 所述):
public static IEnumerable<string> GetConstructs(Type t)
{
foreach (var ctor in t.GetConstructors())
{
var constructs = new List<string>() { "" };
foreach (var param in ctor.GetParameters())
{
if (param.ParameterType.IsClass && param.ParameterType != typeof(string))
{
var newConstructs = new List<string>();
foreach (var _construct in GetConstructs(param.ParameterType))
{
foreach (var construct in constructs)
{ newConstructs.Add(construct + $"{param.ParameterType } ({_construct}) {param.Name}, "); }
}
constructs = newConstructs;
}
else
{
for (var i = 0; i < constructs.Count; i++)
{ constructs[i] += $"{param.ParameterType} {param.Name}, "; }
}

}
foreach (var construct in constructs)
{ yield return construct.TrimEnd(',', ' '); }
}
}
假设您更新了 Foo(int p1, Bar p2) 的构造函数至 Foo(int p1, Bar p2, string s1, Bar y)你得到的输出:
foreach (var construct in GetConstructs(typeof(Foo)))
{ Console.WriteLine(construct); }
是:
System.Int32 p1, ConsoleApp.Bar (System.String p3, System.String p4) p2, System.String s1, ConsoleApp.Bar (System.String p3, System.String p4) y
System.Int32 p1, ConsoleApp.Bar (System.Int32 p5, System.Int32 p6) p2, System.String s1, ConsoleApp.Bar (System.String p3, System.String p4) y
System.Int32 p1, ConsoleApp.Bar (System.String p3, System.String p4) p2, System.String s1, ConsoleApp.Bar (System.Int32 p5, System.Int32 p6) y
System.Int32 p1, ConsoleApp.Bar (System.Int32 p5, System.Int32 p6) p2, System.String s1, ConsoleApp.Bar (System.Int32 p5, System.Int32 p6) y

关于c# - 递归拉取所有构造函数选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65187374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com