C#中派生类的成员函数调用基类保护成员函数的问题 派生类的普通程序函数A中调用了基类的保护普通成员函数B,派生类的对象CC通过访问函数A输出信息。程序编译时正常,运行时报错,提示“未将对象引用设置到对象的实例”,程序停止在函数A中调用函数B的地方。刚学习C#,遇到这个问题不知道怎么解决,请指教! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 派生类只能调用基类的public成员!!! ///////////////////////////////////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.DLLusing 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");就会报错“未将对象引用设置到对象的实例” 基类是在dll中,这个工程中加载了这个dll。是否是这个dll有问题? 基类是在dll中,这个工程中加载了这个dll。是否是这个dll有问题? 很明显错误是在这个dll中抛出的。 基类是在dll中,这个工程中加载了这个dll。是否是这个dll有问题? 很明显错误是在这个dll中抛出的。好的,谢谢,我再看看 那你需要看 LibraryBase中的CreateTestStepResult("", false, ""); 和 UpdateVerboseMessage("do math plus: x + y"); 你调用一个private的成员我看看? 你调用一个private的成员我看看?protected 不知道?基础太差 foreach用法 调用新浪微博API发送图片失败 求交流 如何更换C# winform开发的程序的图标 在net2005中,如何根据传递值使treeview的节点被选中 帮忙翻译两个ASP函数 关于System.configuration的保存问题 VS2003SP1已经出来了 如何在winform下,实现定时打开一个网页,然后关闭网页,谢谢 求助:C#如何实现自动上传文件到网站然后获取返回值??? 请教下 silverlight中rtsp流H264格式的视频播放 winform开发的软件容易感染病毒 请教大家一个关于Newtonsoft.Json的小问题
///////////////////////////////////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");就会报错“未将对象引用设置到对象的实例”
很明显错误是在这个dll中抛出的。
很明显错误是在这个dll中抛出的。
好的,谢谢,我再看看
protected 不知道?基础太差