Вы можете использовать статическое поле в Index классе, если вы хотите сохранить предыдущее значение
public static int ID1;
И вот как вы можете установить его значение вне класса
Index.ID1 = 23;
Да. Можно использовать отражение. Что-то вроде этого:
Type thisType = this.GetType();
MethodInfo theMethod = thisType.GetMethod(TheCommandString);
theMethod.Invoke(this, userParameters);
Можно вызвать методы экземпляра класса с помощью отражения, делая динамический вызов метода:
Предположим, что Вам назвали метод привет в фактический экземпляр (это):
string methodName = "hello";
//Get the method information using the method info class
MethodInfo mi = this.GetType().GetMethod(methodName);
//Invoke the method
// (null- no parameter for the method call
// or you can pass the array of parameters...)
mi.Invoke(this, null);
В C# можно создать делегатов как указатели функции. Проверьте следующую статью MSDN для получения информации об использовании: http://msdn.microsoft.com/en-us/library/ms173171 (По сравнению с 80) .aspx
public static void hello()
{
Console.Write("hello world");
}
/* code snipped */
public delegate void functionPointer();
functionPointer foo = hello;
foo(); // Writes hello world to the console.
class Program
{
static void Main(string[] args)
{
Type type = typeof(MyReflectionClass);
MethodInfo method = type.GetMethod("MyMethod");
MyReflectionClass c = new MyReflectionClass();
string result = (string)method.Invoke(c, null);
Console.WriteLine(result);
}
}
public class MyReflectionClass
{
public string MyMethod()
{
return DateTime.Now.ToString();
}
}