派生类的普通程序函数A中调用了基类的保护普通成员函数B,派生类的对象CC通过访问函数A输出信息。
程序编译时正常,运行时报错,提示“未将对象引用设置到对象的实例”,程序停止在函数A中调用函数B的地方。
刚学习C#,遇到这个问题不知道怎么解决,请指教!

解决方案 »

  1.   

    派生类只能调用基类的public成员!!!
      

  2.   


    ///////////////////////////////////HTRuntime.dll/////////////////////////////////////////////////////////////
    using System;
    using System.Threading;
    using HTRuntime.Result;namespace HTRuntime.Library
    {
        public abstract class LibraryBase
        {
            protected LibraryBase();        protected CancellationToken AbortToken { get; }
            protected int CellNumber { get; }
            protected bool IsInCleanProcess { get; }
            protected int PanelNumber { get; }        public void SetLibHost(ILibraryHost libHost);
            protected void Abort(string message);
            protected void CloseDevice<T>(Instrument<T> instrument, Action action);
            protected MeasurementResult CreateTestStepResult();
            protected MeasurementResult CreateTestStepResult(string name, bool isMeas = false, string desc = "", string measName = "", string unit = "");
            protected void DeviceOperation<T>(Instrument<T> instrument, Action operation);
            protected TResult DeviceOperation<T, TResult>(Instrument<T> instrument, Func<TResult> operation);
            protected void Fail(string message);
            protected void Fail(string format, params object[] args);
            protected Instrument<T> InitializeDevice<T>(string deviceName, bool sharing, Func<T> initial);
            protected bool LibFunctionArgumentRequireNotNull(out string ErrorMsg, params object[] Arguments);
            protected bool LibFunctionArgumentRequireNotNull(out string ErrorMsg, out string methodName, params object[] Arguments);
            protected void LibSleepMilliseconds(uint millSeconds);
            protected void LibSleepSeconds(uint Sec);
            protected void Pass();
            protected void Pass(string message);
            protected void Pass(double measurement);
            protected void Pass(string format, params object[] args);
            protected void Pass(double measurement, string message);
            protected void Pass(double measurement, string format, params object[] args);
            protected bool ThrowIfAbortRequested();
            protected void UpdateVerboseMessage(string msg);
            protected void UpdateVerboseMessage(string msgFormat, params object[] Args);
        }
    }///////////////////////////////////////////////////////////HTLibrary.cs////////////////////////////////////////////////////////////
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Runtime.InteropServices;// first you need add reference of HTRuntime.DLL
    using HTRuntime;
    using HTRuntime.Library;
    /// <summary>
    /// no requirement of the namespace, you can use whatever you like
    /// </summary>
    namespace LibraryDemo
    {
        /// <summary>
        /// HellenTest libary class;
        /// no requirement of the clase name, you can use whatever you like
        /// the class must be public, and inherit <see cref="LibraryBase"/>.
        /// </summary>
        public class HTLibrary : LibraryBase
        {
            /// <summary>
            /// library class must have a non parameter constructor
            /// </summary>
            public HTLibrary()
            {        }        // Win32 API
            //int WINAPI MessageBox(
            //   _In_opt_  HWND     hWnd,
            //   _In_opt_  LPCTSTR  lpText,
            //   _In_opt_  LPCTSTR  lpCaption,
            //   _In_      UINT     uType
            //   );
            // https://msdn.microsoft.com/en-us/library/windows/desktop/ms645505%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396
            [DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern int MessageBox(IntPtr hWnd, [MarshalAs(UnmanagedType.LPTStr)]string lpText, [MarshalAs(UnmanagedType.LPTStr)]string lpCaption, uint uType);        /// <summary>
            /// simple math library method
            /// HellenTest supported below C Sharp types:
            /// <see cref="void"/>
            /// <see cref="bool"/>
            /// <see cref="byte"/>
            /// <see cref="sbyte"/>
            /// <see cref="ushort"/>
            /// <see cref="short"/>
            /// <see cref="uint"/>
            /// <see cref="int"/>
            /// <see cref="ulong"/>
            /// <see cref="long"/>
            /// <see cref="float"/>
            /// <see cref="double"/>
            /// <see cref="string"/>
            /// <see cref="object"/>
            /// 
            /// and the array types
            /// <see cref="bool[]"/>
            /// <see cref="byte[]"/>
            /// <see cref="sbyte[]"/>
            /// <see cref="ushort[]"/>
            /// <see cref="short[]"/>
            /// <see cref="uint[]"/>
            /// <see cref="int[]"/>
            /// <see cref="ulong[]"/>
            /// <see cref="long[]"/>
            /// <see cref="float[]"/>
            /// <see cref="double[]"/>
            /// <see cref="string[]"/>
            /// <see cref="object[]"/>
            /// 
            /// if you want return custom type, please use type <see cref="object"/>
            /// </summary>
            /// <param name="x">number x</param>
            /// <param name="y">number x</param>
            /// <returns>number of result</returns>
            // LibValidate:  you method so HellenTest will recognize this method
            [LibValidate(true, "Author", "2018-01-26", "library method version", "description of library method")]        
            public int Add(int x, int y)
            {
                this.CreateTestStepResult("", false, "");            // print verbose message to HellenTest debug message panel
                // note: default setting for display verbose message is off.
                this.UpdateVerboseMessage("do math plus: x + y");            return x + y;
            }        [LibValidate(true, "Author", "2018-01-26", "library method version", "description of library method")]
            public void ShowWin32MessageBox(string text, string caption)
            {
                uint MB_OK = 0x00000000;            MessageBox(IntPtr.Zero, text, caption, MB_OK);
            }        /// <summary>
            /// not ed by LibValidateAttribute will not be recognized by HellenTest
            /// </summary>
            public void Test()
            {        }
            static void Main(string[] args)
            {
                HTLibrary msc = new HTLibrary();
                var x = msc.Add(5, 55);
            }
        }}在Add()中运行到CreateTestStepResult("", false, ""); 和 UpdateVerboseMessage("do math plus: x + y");就会报错“未将对象引用设置到对象的实例”
      

  3.   

    基类是在dll中,这个工程中加载了这个dll。是否是这个dll有问题?
      

  4.   

    基类是在dll中,这个工程中加载了这个dll。是否是这个dll有问题?
      很明显错误是在这个dll中抛出的。
      

  5.   

    基类是在dll中,这个工程中加载了这个dll。是否是这个dll有问题?
      很明显错误是在这个dll中抛出的。
    好的,谢谢,我再看看
      

  6.   

    那你需要看 LibraryBase中的CreateTestStepResult("", false, ""); 和 UpdateVerboseMessage("do math plus: x + y");
      

  7.   

    你调用一个private的成员我看看?
      

  8.   

    你调用一个private的成员我看看?
    protected 不知道?基础太差