110,566
社区成员
发帖
与我相关
我的任务
分享
谁告诉你的? 派生类只能调用基类的public成员!!!
[quote=引用 7 楼 lb_ing 的回复:] [quote=引用 5 楼 twotuli_software 的回复:] 错误在你的父类方法当中,自己检查, 跟派生无关。
[quote=引用 5 楼 twotuli_software 的回复:] 错误在你的父类方法当中,自己检查, 跟派生无关。
错误在你的父类方法当中,自己检查, 跟派生无关。
派生类只能调用基类的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.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: mark 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 marked 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");就会报错“未将对象引用设置到对象的实例”