当前位置:  开发笔记 > 编程语言 > 正文

在没有活动表单实例的情况下调用Windows窗体的方法

如何解决《在没有活动表单实例的情况下调用Windows窗体的方法》经验,为你挑选了1个好方法。

我正在使用VS2005在C#中开发一个Windows应用程序.考虑我需要调用一个非静态方法(Method1),该方法位于另一个类(Class2)的类(Class1)中.为了调用该方法,我需要为该类创建对象.

但我的班级'Class1'有超过1000个变量.因此,每次我为'Class1'创建对象时,所有变量都会被实例化,但我的方法'Method1'使用的变量非常少.因此,不必要的所有其他变量都会被实例化.

我如何优化这段代码,因为它实例化了必须被调用的特定方法所不需要的变量.

或者是否有任何其他编码标准用于我可以克服这一点.提前致谢.

这是我的变量列表.编辑:

       private string m_sPath = String.Empty;
        private string m_sModule = String.Empty;
        private string m_sType = String.Empty;

        public static string strFileSave;
        private string fileName = "", strFactoryXML = "", strPriorityXml = "", strTempXml = "";
        private string selMethod = "";
        private string selClass = "";
        private string selMethodName = "";
        private string str, str1, str2;
        public static string strFolder;
        public static string strFilePath = "";
        public static string sel1, sel2;
        private string strSetPrFileName = "";
        private string results;
        private string strClassCompare, strMethodCompare, strTestResultCompare;
        public static string CurrWorkingDirectory = Environment.CurrentDirectory;
        private System.Threading.ThreadStart m_CummulativeTimeThreadStart = null;
        private System.Threading.Thread m_CummulativeTimeThread;
        private string[] OuterDelim = null;
        private string[] InnerDelim = null;
        public static ArrayList alObjectsNotDefined;
        public static ArrayList alSystemObjUnDefined = null;
        public static string objFileName = "";
        public static string XmlFileName = "";
        //Integers
        private int nMethodCount, index = 0;
        private static int nClearFlag, nExp = 0, cell;
        public static int flag;
        private static int nMemberCount, nPrevMemberCount;
        private static int classIndex, methodIndex;
        private static int[,] arrIndex = new int[100, 100];
        private int indexThread;
        private int bFlag = 0;
        //private int nIndexClass=0,nIndexMethod=0;
        frmCreateProject cp = new frmCreateProject();


        //Boolean
        private bool crFlag = false, tvAfter = true;
        private bool bSetPriority = false;
        private bool bFilter = false;

        private bool bCr = false;
        private bool snapShotFlag = false, factoryFlag = false, tvSel = false;
        private bool bResultsFlag = false, bSummaryFlag = false;
        public static bool bClick = true, rightMouse = false;
        private bool dataError = false;
        private bool bClassFilter = false, bMethodFilter = false;//,bTestResultFilter=false;
        private bool tvFact = false;
        //private bool bAssignFactory=false;
        //private bool factoryChanges=false;
        private bool bEntered = false;
        public static bool bAssign = false;
        private bool bChangeMethFactory = false;
        private bool bExecution = false;

                public bool bModule = false;
        public bool bClass = false;
        public bool bMethod = false;

        public bool bAssemLoaded = false;         public bool bLoadClicked = false;         public static bool bAssignCustom = false;
        public static string strPath;
        //  public static  int intRowCount;
        public bool bTestGen = false;
        //Others
        private Module[] mdls = null;
        private Assembly oAssembly;
        private DataTable dtSnapshot;
        private DataTable dtFactory;
        private DataTable dtFactoryGV        private DataTable dtExceptions = new DataTable();
        private DataTable dtResults = new DataTable();
        private DataTable dtSummary = new DataTable();
        private DataRow dr;
        private DataRow drGV;
        private DataColumn dc, dc1;
        private DataColumn dcGV;
        private Type m_Type;
        private Type[] types = new Type[100];
        private XmlDocument doc = new XmlDocument();
        //private XmlDocument docTemp=new XmlDocument();
        private DataGridTextBoxColumn TextCol;
        private DataGridTextBoxColumn TextColGV;
        private object[] arguments = new object[20];
        public static bool bSupport = false;
        DataGridTableStyle ts1 = null;
        DataGridTableStyle ts2 = null        DateTime gridMouseDownTime;
        XmlElement gridElement = null;
        XmlNodeList gridList = null, gridCustom = null;

        public string testAssem;




        public string strXmlTestCase;
        public string Gen_dll_path;
        public static string curClass = "";

        public static string curMethod = "";
        public static string curTestResult = "";

        public static bool comdll = false;

        public static bool bIncludeNullValue;
        public static string strMainExcep;

        public static string cmbFactoryClassSel = "";
        public static string cmbFactoryMethodSel = "";
        public static string dataGridParameter = "";
        private static string strReferredTable = "";


        public static string strExePath = ""; 
        public static string strProjPath = "";
        public static string strFactoryType = "";
        public static string strParameterName = "", strParameterType = "";
        public static string checkFactoryClass = "";
        public static string strFileXml = "";
        public static bool bEdit = false;
        public static bool bNumArr = false;
        public string checkType = "";
        public static bool bThread = true;
        public static bool bObjectsDefined = false;
        public static bool bSystemObjDefined = false;
        public static string cmbFactoryNewMethodSel = "";

        public static bool editAuto = false;
        private System.Windows.Forms.MainMenu mainMenu1;
        private System.Windows.Forms.MenuItem mnuFile;
        public System.Windows.Forms.MenuItem mnuTest;        private System.Windows.Forms.MenuItem mnuExit;
        private System.Windows.Forms.MenuItem mnuHelp;
        private System.Windows.Forms.MenuItem mnuAbout;
        public System.Windows.Forms.ToolBar toolBar1;        private System.Windows.Forms.TabControl tcResults;
        private System.Windows.Forms.TabPage tpSummary;
        private System.Windows.Forms.TabPage tpExceptions;
        private System.Windows.Forms.DataGrid dataGridSummary;
        private System.Windows.Forms.RichTextBox rtbOutput;
        private System.Windows.Forms.DataGrid dataGridExceptions;
        private System.Windows.Forms.Splitter splitterDown;
        private System.Windows.Forms.Splitter splitterTV;
        private System.Windows.Forms.OpenFileDialog openFileDialog1;
        private System.Windows.Forms.ImageList imageList1;
        private System.Windows.Forms.ToolBarButton toolBarOpen;
        private System.Windows.Forms.ToolBarButton toolBarTest;
        private System.Windows.Forms.Panel panelResults;
        private System.Windows.Forms.TabPage tpProblems;
        private System.Windows.Forms.RichTextBox rtbProblems;
        private System.Windows.Forms.MenuItem mnuCreateProject;
        private System.Windows.Forms.MenuItem mnuOpenProject;
        private System.Windows.Forms.ToolBarButton toolBarNew;
        private System.Windows.Forms.PictureBox pictureBox1;
        protected System.Windows.Forms.ContextMenu contextMenuTest;
        private System.Windows.Forms.ToolTip toolTip1;
        private System.Windows.Forms.MenuItem mnuAssignFactory;
        private System.Windows.Forms.ContextMenu contextMenuFactory;
        private System.Windows.Forms.MenuItem mnuViewAutoFactory;
        private System.Windows.Forms.ContextMenu contextMenu1;
        private System.Windows.Forms.MenuItem mnuEditCustomFactory;
        private System.Windows.Forms.MenuItem mnuDeleteCustomFactory;
        private System.Windows.Forms.ToolBarButton toolBarPriority;
        private System.Windows.Forms.MenuItem mnuSetPriority;
        private System.Windows.Forms.ContextMenu contextMenuResults;
        private System.Windows.Forms.MenuItem mnuPerformance;
        private System.ComponentModel.IContainer components;


        public static System.Windows.Forms.TreeView tempTV        public static string classNameGV;
        public string strAppPath = "";
        public string strTempPath = "";
        public bool bDllExists;
        public string smTemp1 = "";
        public string strTestFolder = "";
        public static bool bReloadNewDll = false;
        private string dllUnderTest = "";
        private System.Threading.ThreadStart m_UpdateDBTimeThreadStart = null;
        private System.Threading.Thread m_UpdateDBTimeThread;
        private TabPage tabPage2;
        private TreeView tvTestAssemb;
        private TabPage tabPage1;
        private TreeView tv;
        private TabControl tabControl1;
        public static string Output;
        private MenuItem menuItem3;
        private MenuItem menuItem4;
        private MenuItem menuItem5;
        private MenuItem menuItem1;
        private MenuItem menuItem2;
        private TabPage tpTestGen;
        public StatusBar statusBar1;
        private TextBox txtTestGen;
        private TabPage tpTestCaseTable;
        private DataGrid dataGrid2;
        private Panel panel1;
        private Button btnGenerate;
        private Button btnSave;
        private Button btnTestCase;
        private Label label5;
        private ComboBox cmbTestCase;
        private DataGrid dataGrid1;
        private TabPage tpGlobalVariable;
        public DataGrid dataGridFactoryGV;
        private Panel panelGlobalVariable;
        private ComboBox cmbMethodNameGV;
        private Label label2;
        private Label label1;
        private ComboBox cmbClassNameGV;
        private TabPage tpFactorySettings;
        private Panel panelFactory;
        public DataGrid dataGridFactory;
        private Panel panelClassDetails;
        private Label lblClassName;
        private ComboBox cmbClassName;
        private Label lblMethodName;
        private ComboBox cmbMethodName;
        private TabPage tpSnapshot;
        private Panel panelUpdateMsg;
        private PictureBox pictureBox2;
        private Label label4;
        private DataGrid datagridSnapshot;
        public TabControl tcSnapshot;
        public StatusBar statBar;


        public ArrayList alMultiLevelTableName = new ArrayList();

        public ArrayList alExpRes;        
public ArrayList alParVal; 
        public ArrayList alTestCaseID        
public ArrayList alClassName;
        private MenuItem menuItem6        
public ArrayList alMethSig;
        public ArrayList alComments; 

Anton Gogole.. 7

很难说可以采取哪些措施来解决这个特殊问题.什么说,不过,是有这样一个巨大的类绝对是一个代码味道.尝试重构您的类,以便操作方法所需的类Class2具有更"理智"的大小.

编辑圣牛!你显然试图在一个Form衍生类上调用一个方法.将所需的逻辑移至第三类.



1> Anton Gogole..:

很难说可以采取哪些措施来解决这个特殊问题.什么说,不过,是有这样一个巨大的类绝对是一个代码味道.尝试重构您的类,以便操作方法所需的类Class2具有更"理智"的大小.

编辑圣牛!你显然试图在一个Form衍生类上调用一个方法.将所需的逻辑移至第三类.


从表单中移出与UI无关的所有代码.这可能包括你试图调用的代码.
推荐阅读
惬听风吟jyy_802
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有