简体   繁体   English

部分实例化

[英]Partial Instantiation

This is more a question of terminology than how to do something.这与其说是如何做某事,不如说是一个术语问题。

I found a sort of 3rd state a property can be in. The first is a valid property.我发现了一种财产可以处于的第三种状态。第一种是有效的财产。 For example, bool is true or false.例如,bool 是 true 或 false。 The second is null.第二个是空的。 The third I do not know what to call it.第三个我不知道叫它什么。 It is a property of an object that has been instantiated.它是已实例化的对象的属性。 This property kind of looks like it has not been instantiated.这个属性看起来好像还没有被实例化。 Not sure is that is the proper way to state it.不确定这是陈述它的正确方式。

Example: Working with System.Diagnostics.Process to open a virtual keyboard, OSK.示例:使用 System.Diagnostics.Process 打开虚拟键盘 OSK。 I need a routine to toggle the keyboard on and off.我需要一个例程来打开和关闭键盘。 This works great.这很好用。

 using System.Diagnostics; namespace Bsc { public static class OnScreenKeyboard { private static Process virtualKeyboard = new Process(); public static void ToggleHideShow() { try { if (virtualKeyboard.HasExited) virtualKeyboard = Process.Start("osk.exe"); else virtualKeyboard.Kill(); } catch { virtualKeyboard = Process.Start("osk.exe"); } } } }

Looking at the object virtualKeyboard in a watch window I can see how virtualKeyboard has been instantiated, but not completely.在监视窗口中查看对象 virtualKeyboard 我可以看到 virtualKeyboard 是如何实例化的,但不是完全实例化。 On the first pass virtualKeyboard.HasExited throws an exception.在第一遍 virtualKeyboard.HasExited 抛出异常。 Watching the watch window, it also looks like it is not there.看着监视窗口,好像也没有了。 The line has a nice bright red dot in front of it with an X in it.这条线前面有一个漂亮的鲜红点,里面有一个 X。

Name姓名 Value价值 Type类型
HasExited已退出 'virtualKeyboard.HasExited' threw an exception of type 'System.InvalidOperationException' “virtualKeyboard.HasExited”引发了“System.InvalidOperationException”类型的异常 bool {System.InvalidOperationException} bool {System.InvalidOperationException}

Still on first pass the try/catch jumps to the Process.Start.仍然在第一次通过时,try/catch 跳转到 Process.Start。 After this executes the line looks like you would expect for an instantiated property.执行后,该行看起来像您期望的实例化属性。

Name姓名 Value价值 Type类型
HasExited已退出 false错误的 bool布尔值

All calls after the first one HasExited works like you would expect and the method toggles the keyboard on and off.第一个 HasExited 之后的所有调用都像您期望的那样工作,并且该方法会打开和关闭键盘。

I have not run into an object before that seemingly was only partially instantiated.在看似只是部分实例化之前,我还没有遇到过一个对象。 What are the proper technical terms for this scenario?这种情况的正确技术术语是什么? I have used the term 'instantiated', is that correct?我用过“实例化”这个词,对吗?

As per the documentation , the HasExited property throws an InvalidOperationException when there is no OS process associated with the Process instance.根据文档,当没有与Process实例关联的 OS 进程时, HasExited属性会抛出InvalidOperationException

Your property is initialized to a new instance of the Process class, which has not been started.您的属性已初始化为尚未启动的Process类的新实例。 Therefore, there is no OS process associated with that instance, and the HasExited property will throw an exception.因此,没有与该实例关联的操作系统进程,并且HasExited属性将引发异常。

Remove the field initializer, and test for null in your method.删除字段初始值设定项,并在您的方法中测试是否为null

public static class OnScreenKeyboard
{
    private static Process virtualKeyboard;
    public static void ToggleHideShow()
    {
        try
        {
            if (virtualKeyboard == null || virtualKeyboard.HasExited)
                virtualKeyboard = Process.Start("osk.exe");
            else
                virtualKeyboard.Kill();
        }
        catch
        {
            virtualKeyboard = Process.Start("osk.exe");
        }
    }
}

It is not possible for an object to be half-initialised based on the working of the language.不可能根据语言的工作情况对对象进行半初始化。 If an object is not in a correct state, this is due to the working of the class/properties etc, and how the code has been written to initialise the object or not throw an exception when accessing the properties.如果对象未处于正确状态,这是由于类/属性等的工作,以及如何编写代码来初始化对象或在访问属性时不抛出异常。

If the object does not always do what you expect then you have to check for the exception, do some defensive coding and handle the various scenarios in your own code.如果对象并不总是按照您的预期执行,那么您必须检查异常,进行一些防御性编码并在您自己的代码中处理各种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM