gpt4 book ai didi

c# - 是否可以在函数结束或超出范围后重置静态字段?

转载 作者:太空宇宙 更新时间:2023-11-03 14:52:06 24 4
gpt4 key购买 nike

我有多种类型的组件类和 1 个生成器类。组件类有一个名称字段,由生成器类生成时必须是唯一的。

所以,我写的组件类和生成器类如下:

public class Component_A{
static int iCount;
public string name {get;set;}
public Component_A(){
name = String.Format("ComponentA_{0}", iCount++);
}
}

public class Component_B{
static int iCount;
public string name {get;set;}
public Component_A(){
name = String.Format("ComponentB_{0}", iCount++);
}
}

public class Generator(){
public List<String> generateComponent(){
Component_A a1 = new Component_A();
Component_A a2 = new Component_A();
Component_B b1 = new Component_B();

List<string> strList = new List<string>();
strList.Add(a1.name);
strList.Add(a2.name);
strList.Add(b1.name);

return strList;
}
}

public class MainClass(){
public static void main(){
Generator gen;
for(int i=0; i<2; i++){
gen = new Generator();
List<String> componentNameList = gen.generateComponent();

foreach(var s in componentNameList){
Console.Out.WriteLine(s);
}
Console.Out.WriteLine();
}

}
}

但是,我想在初始化新生成器时重置组件 iCount 字段。

例如:以上代码执行结果为:

ComponentA_0
ComponentA_1
ComponentB_0

ComponentA_2
ComponentA_3
ComponentB_1

但我希望它是这样的:

ComponentA_0
ComponentA_1
ComponentB_0

ComponentA_0
ComponentA_1
ComponentB_0

我怎样才能做到这一点?

最佳答案

public class Component_A{
public static int iCount;
public string name {get;set;}
public Component_A(){
name = String.Format("ComponentA_{0}", iCount++);
}
}

public class Component_B{
public static int iCount;
public string name {get;set;}
public Component_A(){
name = String.Format("ComponentB_{0}", iCount++);
}
}

public class Generator(){
public List<String> generateComponent(){
Component_A a1 = new Component_A();
Component_A a2 = new Component_A();
Component_B b1 = new Component_B();

List<string> strList = new List<string>();
strList.Add(a1.name);
strList.Add(a2.name);
strList.Add(b1.name);

return strList;
}
public void reset() {
Component_A.iCount = 0;
Component_B.iCount = 0;
}
}

只是将 iCount 更改为 public 并添加了一个重置​​方法以将 iCount 更改为 0。

像这样使用它:

public class MainClass(){
public static void main(){
Generator gen;
for(int i=0; i<2; i++){
gen = new Generator();
List<String> componentNameList = gen.generateComponent();

foreach(var s in componentNameList){
Console.Out.WriteLine(s);
}
gen.reset();
Console.Out.WriteLine();
}

}
}

编辑:由于您有 100 多个组件,反射似乎是一个不错的选择。我已将组件移到它们自己的类中。

public class Components{
public class Component_A{
public static int iCount;
public string name {get;set;}
public Component_A(){
name = String.Format("ComponentA_{0}", iCount++);
}
}

public class Component_B{
public static int iCount;
public string name {get;set;}
public Component_B(){
name = String.Format("ComponentB_{0}", iCount++);
}
}
}

public class Generator {
public List<String> generateComponent(){
var a1 = new Components.Component_A();
var a2 = new Components.Component_A();
var b1 = new Components.Component_B();

List<string> strList = new List<string>();
strList.Add(a1.name);
strList.Add(a2.name);
strList.Add(b1.name);

return strList;
}
public void reset() {
var components = typeof(Components).GetNestedTypes().ToList();
foreach (var component in components) {
var property = component.GetField("iCount", BindingFlags.Public | BindingFlags.Static);
property.SetValue(null, 0);
}
}
}

关于c# - 是否可以在函数结束或超出范围后重置静态字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51320993/

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