如何使用字符串引用现有的类变量?

时间:2015-12-04 03:36:37

标签: c#

我想使用字符串来获取对现有类变量的引用。我已经看到了类似事情的一些例子,但似乎可以解决这个问题。

请帮助评论部分!

strcpy_s

3 个答案:

答案 0 :(得分:1)

即使使用反射,也无法通过字符串名称访问本地变量。一种选择是将它们存储在字典中:

var dict = new Dictionary<string, MyClass>();
dict.Add("myClass", myClass);

string varName = "myClass";
MyClass myOtherClass = dict[varName];

但是根本不清楚为什么你想通过字符串名称来访问它。我怀疑你的真正的问题有更好的解决方案。

答案 1 :(得分:1)

虽然问题有点奇怪,但这是愚蠢的解决方案:

public class ClassRoll {

private ArrayList<Student> students = new ArrayList<Student>();
private String title;
private String filename = "data.txt";

ClassRoll(String f) {
    Scanner kb = new Scanner(System.in);
    String inpFileName = kb.next();
    File inpFile = new File(inpFileName);
    Student s = new Student(fName, lName);

}

void Remove() {
    Scanner kb = new Scanner(System.in);

    System.out.println("What is the Student's first name?");
    String fName = kb.next();

    System.out.println("What is the Student's last name?");
    String lName = kb.next();

    Student s = new Student(fName, lName);

    for (int i = 0; i < students.size(); i++) {
        if (s.compareTo(students.get(i)) == 0) {
            students.remove(i);

        } else {
            System.out.println("Error: Student is not in Class");
        }

    }

}

void Display() {

}

void Add() {
    Scanner kb = new Scanner(System.in);

    System.out.println("What is the Student's first name?");
    String fName = kb.next();

    System.out.println("What is the Student's last name?");
    String lName = kb.next();

    System.out.println("What is the Student's first score?");
    int score1 = kb.nextInt();

    System.out.println("What is the Student's second score?");
    int score2 = kb.nextInt();

    System.out.println("What is the Student's third score?");
    int score3 = kb.nextInt();

    Student s = new Student(fName, lName);

    for (int i = 0; i < students.size(); i++) {
        if (s.compareTo(students.get(i)) == 0) {
            System.out.println("Student already in class");
        } else {
            students.add(s);
        }
    }
}

void changeScore1() {
    Scanner kb = new Scanner(System.in);

    System.out.println("What is the Student's first name?");
    String fName = kb.next();

    System.out.println("What is the Student's last name?");
    String lName = kb.next();

    System.out.println("What is the Student's first score?");
    int score1 = kb.nextInt();

    Student s = new Student(fName, lName);

    for (int i = 0; i < students.size(); i++) {
        if (s.compareTo(students.get(i)) == 0) {
            s.setScore1(i);
        } else {
            System.out.println("Error: Student is not in Class");
        }

    }

}

void changeScore2() {
    Scanner kb = new Scanner(System.in);

    System.out.println("What is the Student's first name?");
    String fName = kb.next();

    System.out.println("What is the Student's last name?");
    String lName = kb.next();

    System.out.println("What is the Student's first score?");
    int score1 = kb.nextInt();

    Student s = new Student(fName, lName);

    for (int i = 0; i < students.size(); i++) {
        if (s.compareTo(students.get(i)) == 0) {
            s.setScore2(i);
        } else {
            System.out.println("Error: Student is not in Class");
        }

    }

}

void changeScore3() {
    Scanner kb = new Scanner(System.in);

    System.out.println("What is the Student's first name?");
    String fName = kb.next();

    System.out.println("What is the Student's last name?");
    String lName = kb.next();

    System.out.println("What is the Student's first score?");
    int score1 = kb.nextInt();

    Student s = new Student(fName, lName);

    for (int i = 0; i < students.size(); i++) {
        if (s.compareTo(students.get(i)) == 0) {
            s.setScore3(i);
        } else {
            System.out.println("Error: Student is not in Class");
        }

    }

}

public void sortAverage() {
    for (int i = 0; i < students.size() - 1; i++) {
        for (int j = i + 1; j < students.size(); j++) {
            Student s1 = (Student) students.get(i);
            Student s2 = (Student) students.get(j);
            if (s1.getAverage() < s2.getAverage()) {
                students.set(i, s2);
                students.set(j, s1);
            }
        }
    }
}

public void sortNames() {
    for (int i = 0; i < students.size() - 1; i++) {
        for (int j = i + 1; j < students.size(); j++) {
            Student s1 = (Student) students.get(i);
            Student s2 = (Student) students.get(j);
            if (s1.compareTo(s2) > 0) {
                students.set(i, s2);
                students.set(j, s1);
            }
        }
    }
}

public void save(){

}

按名称访问此实例:

var instances = new Dictionary<string, MyClass>();
MyClass instance= new MyClass(1);
//the string "instance" can be replaced with "nameof(instance)" using C# 6.0
instances.Add("instance", instance); 

答案 2 :(得分:0)

奇怪的问题,很想知道这是否是你想要的:

public class MyClass
    {
        public int myInt;

        public MyClass(int i)
        {
            myInt = i;
        }
    }

    public class Starter
    {
        private MyClass myclass;
        public void Start()
        {
            myclass = new MyClass(1);

            var type = this.GetType();

            var variable = type.GetField("myclass", BindingFlags.NonPublic | BindingFlags.Instance);

            MyClass myOtherClass = (MyClass)variable.GetValue(this);
        }
    }