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
на публикацию и добавил метод сброса, чтобы изменить iCount
s на 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();
}
}
}
EDIT: Поскольку у вас есть 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);
}
}
}
Ошибка пользователя: (
fb.login(response => {
dispatch(facebookLogin(response, fb, onLogin));
}, {
scope: 'user_friends'
});