日期:2014-05-17  浏览次数:20773 次

.net里如何通过一个calss作为出口调用其他的calss文件
。net里面例如我有2个了类A,B.我现在想用一个类X作为程序的入口,来调用A,B类(让X先运行,在X里有控制A,B运行的线程),有办法么?

------解决方案--------------------
X类中定义一个启动方法Strat() 里面开始两个线程分别调用A类和B类中的方法

程序的入口,实例化X,然后调用Strat
------解决方案--------------------
用反射也可以限定指定的类运行:

public static void getFileName()
 {
            StackTrace trace = new StackTrace();
            StackFrame frame = trace.GetFrame(1);
            MethodBase method = frame.GetMethod();
            String className = method.ReflectedType.Name;
            Console.Write("ClassName:" + className + "\nMethodName:" + method.Name);
            System.Diagnostics.Debug.WriteLine("ClassName:" + className + "\nMethodName:" + method.Name+"\n命名空间:"+ method.ReflectedType.Namespace);
 }

这样可以获取到调用当前类的命名空间,类名和方法名。
通过判断是否是 X 类然后中断类的初始化。

private A(){}

        public static A GetA)
        {
            StackTrace trace = new StackTrace();
            StackFrame frame = trace.GetFrame(1);
            MethodBase method = frame.GetMethod();
            String className = method.ReflectedType.Name;
            if (className.Equals("X"))
            {
                return new A();
            }
            return null;
        }

------解决方案--------------------
这样写:

class X { }

class EntryPointClass
{
    static X x = new X();
}

class A : EntryPointClass
{
   ...
}

class B : EntryPointClass
{
   ...
}
这样肯定X先在所有的A B创建之前就创建了!