/// <summary> /// Remove a UPnPStateVariable /// <para> /// An exception will be thrown if you try to remove a UPnPStateVariable that is associated with an action, /// or a UPnPStateVariable that is referenced by an Action. /// </para> /// </summary> /// <param name="stateVariable">UPnPStateVariable to remove</param> public void RemoveStateVariable(UPnPStateVariable stateVariable) { foreach (UPnPStateVariable.AssociationNode n in stateVariable.GetAssociations()) { try { if (GetAction(n.ActionName).GetArg(n.ArgName).StateVarName == stateVariable.Name) { throw (new UPnPStateVariable.CannotRemoveException("Associated with " + n.ActionName + ":" + n.ArgName)); } } catch (System.NullReferenceException) { } } StateVariables.Remove(stateVariable.Name); }
/// <summary> /// Add a UPnPStateVariable /// </summary> /// <param name="NewVar"></param> public void AddStateVariable(UPnPStateVariable NewVar) { NewVar.ParentService = this; StateVariables[NewVar.Name] = NewVar; UPnPStateVariable.AssociationNode[] Nodes; Hashtable ArgTable; Nodes = NewVar.GetAssociations(); for (int x = 0; x < Nodes.Length; ++x) { ArgTable = (Hashtable)VarAssociation[Nodes[x].ActionName]; if (ArgTable == null) ArgTable = new Hashtable(); if (ArgTable.ContainsKey(Nodes[x].ArgName) == true) { StateVariables.Remove(((UPnPStateVariable)ArgTable[Nodes[x].ArgName]).Name); } ArgTable[Nodes[x].ArgName] = NewVar; VarAssociation[Nodes[x].ActionName] = ArgTable; // THIS: Updated to throw meaningfull exception instead of null reference UPnPAction A = GetAction(Nodes[x].ActionName); if (A != null) { UPnPArgument Argument = A.GetArg(Nodes[x].ArgName); if (Argument == null) { throw (new Exception(string.Format("There is no parameter named {0} for the {1}.{2} method.", Nodes[x].ArgName, ServiceInstance.GetType().ToString(), Nodes[x].ActionName))); } Argument.StateVarName = NewVar.Name; } // THIS: End update } }