Example #1
0
        private void DepersistControl() {
            Freeze(true);

            if (ocxState == null) {
                // must init new:
                //
                if (instance is UnsafeNativeMethods.IPersistStreamInit) {
                    iPersistStreamInit = (UnsafeNativeMethods.IPersistStreamInit) instance;
                    try {
                        storageType = STG_STREAMINIT;
                        iPersistStreamInit.InitNew();
                    }
                    catch (Exception e1) {
                        Debug.WriteLineIf(AxHTraceSwitch.TraceVerbose, "Exception thrown trying to IPersistStreamInit.InitNew(). Is this good?"  + e1.ToString());
                    }
                    return;
                }
                if (instance is UnsafeNativeMethods.IPersistStream) {
                    storageType = STG_STREAM;
                    iPersistStream = (UnsafeNativeMethods.IPersistStream) instance;
                    return;
                }
                if (instance is UnsafeNativeMethods.IPersistStorage) {
                    storageType = STG_STORAGE;
                    ocxState = new State(this);
                    iPersistStorage = (UnsafeNativeMethods.IPersistStorage) instance;
                    try {
                        iPersistStorage.InitNew(ocxState.GetStorage());
                    }
                    catch (Exception e2) {
                        Debug.WriteLineIf(AxHTraceSwitch.TraceVerbose, "Exception thrown trying to IPersistStorage.InitNew(). Is this good?"  + e2.ToString());
                    }
                    return;
                }
                if (instance is UnsafeNativeMethods.IPersistPropertyBag) {
                    Debug.WriteLineIf(AxHTraceSwitch.TraceVerbose, this + " supports IPersistPropertyBag.");
                    iPersistPropBag = (UnsafeNativeMethods.IPersistPropertyBag) instance;
                    try {
                        iPersistPropBag.InitNew();
                    }
                    catch (Exception e1) {
                        Debug.WriteLineIf(AxHTraceSwitch.TraceVerbose, "Exception thrown trying to IPersistPropertyBag.InitNew(). Is this good?"  + e1.ToString());
                    }
                }
                
                Debug.Fail("no implemented persitance interfaces on object");
                throw new InvalidOperationException(SR.GetString(SR.UnableToInitComponent));
            }
            
            // Otherwise, we have state to deperist from:
            switch (ocxState.Type) {
                case STG_STREAM:
                    try {
                        iPersistStream = (UnsafeNativeMethods.IPersistStream) instance;
                        DepersistFromIStream(ocxState.GetStream());
                    }
                    catch (Exception e) {
                        Debug.WriteLineIf(AxHTraceSwitch.TraceVerbose, "Exception thrown trying to IPersistStream.DepersistFromIStream(). Is this good?"  + e.ToString());
                    }
                    break;
                case STG_STREAMINIT:
                    if (instance is UnsafeNativeMethods.IPersistStreamInit) {
                        try {
                            iPersistStreamInit = (UnsafeNativeMethods.IPersistStreamInit) instance;
                            DepersistFromIStreamInit(ocxState.GetStream());
                        }
                        catch (Exception e) {
                            Debug.WriteLineIf(AxHTraceSwitch.TraceVerbose, "Exception thrown trying to IPersistStreamInit.DepersistFromIStreamInit(). Is this good?"  + e.ToString());
                        }
                        GetControlEnabled();
                    }
                    else {
                        ocxState.Type = STG_STREAM;
                        DepersistControl();
                        return;
                    }
                    break;
                case STG_STORAGE:
                    try {
                        iPersistStorage = (UnsafeNativeMethods.IPersistStorage) instance;
                        DepersistFromIStorage(ocxState.GetStorage());
                    }
                    catch (Exception e) {
                        Debug.WriteLineIf(AxHTraceSwitch.TraceVerbose, "Exception thrown trying to IPersistStorage.DepersistFromIStorage(). Is this good?"  + e.ToString());
                    }
                    break;
                default:
                    Debug.Fail("unknown storage type.");
                    throw new InvalidOperationException(SR.GetString(SR.UnableToInitComponent));
            }
        
            if (ocxState.GetPropBag() != null) {
                try {
                    iPersistPropBag = (UnsafeNativeMethods.IPersistPropertyBag)instance;
                    DepersistFromIPropertyBag(ocxState.GetPropBag());
                }
                catch (Exception e) {
                    Debug.WriteLineIf(AxHTraceSwitch.TraceVerbose, "Exception thrown trying to IPersistPropertyBag.DepersistFromIPropertyBag(). Is this good?"  + e.ToString());
                }
            }
        }