-2

我一直在为标题中提到的概念而苦苦挣扎。我做了很多研究,找到了一些很好的例子,我尝试实施其中一些,但遇到了障碍。

我最初有一个看起来像这样的类:

namespace sysVer
{
    public class testSuite
    {
        /* there is a bunch of test hardware setup
         * and acquisition also that is not shown
         */
        public struct testResultInfo
        {
            public testStatus testStat;
            public bool warning, error;
            public string warnStr, errStr, infoStr, muxDataStr;
            public double measDecVal;
            public Int16 measIntVal;
        };
        
        // NOTE there is no default constructor
        
        public static testResultInfo testCase1()
        {
            
        }
        public static testResultInfo testCase2()
        {
            
        }
        public static testResultInfo testCase3()
        {
            
        }
        // there are about 100 more
    }
}

并使用带有 105 个复选框和字典的 Windows 窗体来确定用户想要运行的函数,我能够使用 Invoke Member 通过字符串调用函数,如下所示:

// first attempt
namespace sysVer
{
    public partial class SysVerForm : Form
    {
        public static Pdgu1553ver.testResultInfo tcResult = new Pdgu1553ver.testResultInfo ( )
        {
            testStat = Pdgu1553ver.testStatus.pass,
            warning = true,
            error = true,
            warnStr = "This is a warning",
            errStr = "This is an error",
            infoStr = "This is info",
            muxDataStr = "Mux data of some sort",
            measDecVal = 69.69,
            measIntVal = 69
        };
        
        // for loop that iterates over checkbox checked items
        foreach ( var checkedItem in checkedItems )
        {
            var funcName = "";
            
            // retrieve function name from dictionary
            tcToFuncDic.TryGetValue ( checkedItem.ToString ( ), out funcName );
            
            tcResult = ( testSuite.testResultInfo ) typeof ( testSuite ).InvokeMember 
                (
                    funcName,
                    BindingFlags.InvokeMethod |
                    BindingFlags.Public |
                    BindingFlags.Static,
                    null,
                    null,
                    null 
                );
        }
    }
}

但是我在这里遇到了问题。当调用 InvokeMember 时,会执行静态方法并接收返回类型,但我不确定该方法是刚刚调用还是使用默认构造函数创建的类的实际实例?我开始注意到,在一次调用期间设置的此类的私有属性在另一次调用需要它们的不同静态函数期间不存在。

我假设我需要一个类的实例,所以我更改为以下内容:

namespace sysVer
{
    public class testSuite
    {
        /* there is a bunch of test hardware setup
         * and acquisition also that is not shown
         */
        public struct testResultInfo
        {
            public testStatus testStat;
            public bool warning, error;
            public string warnStr, errStr, infoStr, muxDataStr;
            public double measDecVal;
            public Int16 measIntVal;
        };
        
        // constructor
        public testSuite()
        {
            /* Here I initialize hardware, set variables, and
             * run a few other tasks.  All of which need to be done
             * an exist before this object can be used
             */
        }
        
        public testResultInfo testCase1()
        {
            
        }
        public testResultInfo testCase2()
        {
            
        }
        public testResultInfo testCase3()
        {
            
        }
        //... there are about 100 more
    }
}

现在,我撞到了另一面墙。我需要做这样的事情:

testSuite myTest = new testSuite();
var blah = myTest.InvokeMember(
                    funcName,
                    BindingFlags.InvokeMethod |
                    BindingFlags.Public |
                    BindingFlags.Static,
                    null,
                    null,
                    null 
                );

但是我的课程不包含该方法。调查后我了解到我可以将我的类更改为从“Type”继承,并且我必须从“Type”中覆盖大约 50 个左右的抽象。我开始走这条路,但觉得这可能比我需要做的工作更多。

我需要有一个我的类的实例,并且需要能够在运行时推断出要调用哪个非静态方法。在我读过的许多帖子中,这两个: 如何通过反射调用非静态方法并在 c# 中调用非静态方法 似乎将我推向了正确的方向,但我仍然在某处遗漏了一些东西。

我已经花了很多时间来覆盖所有这些函数,但我被困在那里如何实际实现被覆盖的 InvokeMember 函数,我不想在此时撤消所有工作以尝试做其他事情。

一如既往,非常感谢任何建议。感谢您的时间和考虑。

4

1 回答 1

-1

我感觉到您正在将许多假设和担忧与静态、结构、反射等复杂化,并且不知道您想去哪里。您显然试图以错误的方式解决错误的问题。但我会一次解决你的问题。

我不确定该方法是刚刚调用还是使用默认构造函数创建的类的实际实例?

你为什么那么想?您没有发布测试方法的代码,但我可以向您保证,它InvokeMember 实际上调用了 member

我开始注意到,在一次调用期间设置的此类的私有属性在另一次调用需要它们的不同静态函数期间不存在。

testResultInfo可能是因为测试方法每次都会创建一个新的,但是没有代码就不可能知道。但我不明白为什么这是一个问题。您需要“结转”哪些数据?也许您需要一个可以在方法中重用的静态testResultInfo属性?testSuite

调查后我了解到我可以将我的类更改为从“类型”继承

我看不出有什么理由你需要这样做,或者你认为这会解决什么问题。

停止尝试通过尝试解决其他问题(可能以错误的方式)产生的顶级创可贴问题。备份、分析您的程序并考虑创建不同实例的位置?这些测试方法是否应该神奇地知道您在表单中创建的实例,还是应该以某种方式为它们提供该实例?你应该怎么做?你应该把它作为输入参数吗?还是测试套件的静态属性?

于 2021-07-09T19:11:03.610 回答