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

时间:2009-04-28 14:08:13

标签: c# winforms optimization

我正在使用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; 

11 个答案:

答案 0 :(得分:7)

很难说可以采取哪些措施来解决这一特定问题。但可以可以说是拥有如此庞大的类绝对是code smell。尝试重构您的类,以便在Class2中操作方法所需的类具有更“理智”的大小。

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

答案 1 :(得分:2)

设计良好的类需要1000多个变量是非常罕见的。所以听起来Class1的设计有问题。如果您需要调用该非静态方法,那么您需要实例化该类,并且没有办法解决性能问题。所以,如果可以的话,你需要重构。

你可以:

  • Class1拆分为较小的类,这些类更专注于单个作业。
  • 您可以重构您需要调用的方法,因此它不需要实例化类(可能因为它不会对类数据使用太多)。
祝你好运。

答案 2 :(得分:1)

听起来你需要重新考虑一下你的实现。如果你有一个包含1000个变量的类,其中任何给定的实例只使用这些变量的一小部分,那么使用某种属性包会更好。

public class C1 {
  private Dictionary<string,object> m_bag = new Dictionary<string,object>();
  public string Name {
    get { return (string)m_bag["Name"]; }
    set { m_bag["Name"] = value; }
  }
  ...
}

否则,您必须为每个变量声明一个字段。如果你只为每个对象使用这些字段中的一些,那么你的程序中会有大量的浪费。这个样本很浪费,因为它使用了后备存储,但它几乎肯定比使用1000字段类中的少量字段更浪费。

答案 3 :(得分:1)

当他们说单个类中的1000个变量是代码气味并暗示分裂类时,每个人都非常好。老实说,它远不如此。

单个类中的1000个变量是不可原谅的。

答案 4 :(得分:1)

让我们假设您的对象类设计得很好并且必然很大并且坚持手头的问题。如果要创建Class1的实例以仅调用一个方法和一个方法,则逻辑可能属于其他位置。

该方法是否会更改Class1的成员? 您是否正在读取Class1的属性以获得输出? 获得输出后,Class1的实例是用?

完成的

如果这些问题的答案是肯定的,则此方法可能根本不属于Class1。尝试重新设计方法,以便在逻辑中只使用通过的参数,返回值是唯一的输出。

答案 5 :(得分:0)

我不知道“Class1”的具体目的,但是如果某个类有超过1000个变量,它就不会“闻”好。看来,存在一些设计问题。

但是你可以看看“Singleton”和“IOC容器”的设计模式,可能会有所帮助。

答案 6 :(得分:0)

我建议将你的课分开,&gt;单个班级中的1000个变量可能是该课程做得过多的代码气味?

如果将Method1所需的变量拆分为单独的类,可能会使生活更简单。

答案 7 :(得分:0)

Class1有1000多个变量?仅这一点就暗示你在课堂上遇到了一些设计问题。此外,如果你需要经常使用该类的一个实例只使用该类的一部分,你肯定应该能够将该类分成更小的部分。

Class1背后的想法是什么?它试图实现什么目标?

答案 8 :(得分:0)

我建议您详细了解Single Responsibility Principle(SRP)。

答案 9 :(得分:0)

我的整个IronScheme系统有452个静态只读字段。然后是另外2000个其他静态字段(用于盒装符号和全局引用)。

加载时仍需要100毫秒。

答案 10 :(得分:0)

开始拆分此类的一种方法是查看在哪些方法中使用哪些字段。

如上所述,您很快就会发现并非所有方法都使用该类中的所有字段。这表明你的班级缺乏内部凝聚力。你想要做的是将这个类重构为几个内部更具凝聚力的类。

因此,寻找在相同字段上运行的函数集群。这些集群是较小的类,希望摆脱你在这里的超级类。当您移动这些方法集群并将它们使用的字段放入各自的类中时,会查找集群所代表的概念的表达名称。

许多结果类本身都很有用(并且可以自己测试)。剩下的原始类可能最终使用许多新创建的类来执行它提供的功能。

请注意,这不是一个完全机械化的过程,而是一种启发式方法,可以帮助您清理这个烂摊子。