ManagementNamedValueCollection.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Wmi / managed / System / Management / ManagementNamedValueCollection.cs / 1305376 / ManagementNamedValueCollection.cs

                            using System; 
using System.Collections;
using System.Collections.Specialized;
using WbemClient_v1;
using System.Runtime.Serialization; 

namespace System.Management 
{ 
 	//CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC//	
	///  
	///     Represents a collection of named values
	///       suitable for use as context information to WMI operations. The
 	///       names are case-insensitive.
	///  
 	//CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC//
 	public class ManagementNamedValueCollection : NameObjectCollectionBase 
	{ 
 		// Notification of when the content of this collection changes
		internal event IdentifierChangedEventHandler IdentifierChanged; 

		//Fires IdentifierChanged event
		private void FireIdentifierChanged()
 		{ 
			if (IdentifierChanged != null)
 				IdentifierChanged(this, null); 
 		} 

		//default constructor 
 		/// 
		///    Initializes a new instance
		///    of the  class.
		///  
 		/// 
		///  Initializes a new instance of the  class, which is empty. This is 
 		///    the default constructor. 
 		/// 
		public ManagementNamedValueCollection() 
 		{
        }

 
        /// 
        /// Initializes a new instance of the  class that is serializable 
        ///    and uses the specified  
        ///    and .
        ///  
        /// The  to populate with data.
	/// The destination (see  ) for this serialization.
        protected ManagementNamedValueCollection(SerializationInfo info, StreamingContext context) : base(info, context)
        { 
        }
 
		///  
		///    Internal method to return an IWbemContext representation
 		///    of the named value collection. 
		/// 
 		internal IWbemContext GetContext()
 		{
			IWbemContext wbemContext = null; 

 			// Only build a context if we have something to put in it 
			if (0 < Count) 
			{
				int status = (int)ManagementStatus.NoError; 

 				try {
					wbemContext = (IWbemContext) new WbemContext ();
 
 					foreach (string name in this)
 					{ 
						object val = base.BaseGet(name); 
 						status = wbemContext.SetValue_ (name, 0, ref val);
						if ((status & 0x80000000) != 0) 
						{
							break;
 						}
					} 
 				} catch {}	//
 			} 
			 
 			return wbemContext;
		} 

		/// 
		///     Adds a single-named value to the collection.
 		///  
		/// The name of the new value.
 		/// The value to be associated with the name. 
 		public void Add (string name, object value) 
		{
 			// Remove any old entry 
			try
			{
				base.BaseRemove (name);
 			} catch {} 

			base.BaseAdd (name, value); 
 			FireIdentifierChanged (); 
 		}
 
		/// 
 		///     Removes a single-named value from the collection.
		///       If the collection does not contain an element with the
		///       specified name, the collection remains unchanged and no 
		///       exception is thrown.
 		///  
		/// The name of the value to be removed. 
 		public void Remove (string name)
 		{ 
			base.BaseRemove (name);
 			FireIdentifierChanged ();
		}
 
		/// 
		///    Removes all entries from the collection. 
 		///  
		public void RemoveAll ()
 		{ 
 			base.BaseClear ();
			FireIdentifierChanged ();
 		}
 
		/// 
		///    Creates a clone of the collection. Individual values 
		///       are cloned. If a value does not support cloning, then a  
 		///       is thrown. 
		///  
 		/// 
 		///    The new copy of the collection.
		/// 
 		public ManagementNamedValueCollection Clone () 
		{
			ManagementNamedValueCollection nvc = new ManagementNamedValueCollection(); 
 
			foreach (string name in this)
 			{ 
				// If we can clone the value, do so. Otherwise throw.
 				object val = base.BaseGet (name);

 				if (null != val) 
				{
 					Type valueType = val.GetType (); 
					 
					if (valueType.IsByRef)
					{ 
 						try
						{
 							object clonedValue = ((ICloneable)val).Clone ();
 							nvc.Add (name, clonedValue); 
						}
 						catch 
						{ 
							throw new NotSupportedException ();
						} 
 					}
					else
 					{
 						nvc.Add (name, val); 
					}
 				} 
				else 
					nvc.Add (name, null);
			} 

 			return nvc;
		}
 
 		/// 
 		///    Returns the value associated with the specified name from this collection. 
		///  
 		/// The name of the value to be returned.
		///  
		/// An  containing the
		///    value of the specified item in this collection.
 		/// 
		public object this[string name] 
 		{
 			get { 
				return base.BaseGet(name); 
            }
 		} 
    }

}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
using System; 
using System.Collections;
using System.Collections.Specialized;
using WbemClient_v1;
using System.Runtime.Serialization; 

namespace System.Management 
{ 
 	//CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC//	
	///  
	///     Represents a collection of named values
	///       suitable for use as context information to WMI operations. The
 	///       names are case-insensitive.
	///  
 	//CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC//
 	public class ManagementNamedValueCollection : NameObjectCollectionBase 
	{ 
 		// Notification of when the content of this collection changes
		internal event IdentifierChangedEventHandler IdentifierChanged; 

		//Fires IdentifierChanged event
		private void FireIdentifierChanged()
 		{ 
			if (IdentifierChanged != null)
 				IdentifierChanged(this, null); 
 		} 

		//default constructor 
 		/// 
		///    Initializes a new instance
		///    of the  class.
		///  
 		/// 
		///  Initializes a new instance of the  class, which is empty. This is 
 		///    the default constructor. 
 		/// 
		public ManagementNamedValueCollection() 
 		{
        }

 
        /// 
        /// Initializes a new instance of the  class that is serializable 
        ///    and uses the specified  
        ///    and .
        ///  
        /// The  to populate with data.
	/// The destination (see  ) for this serialization.
        protected ManagementNamedValueCollection(SerializationInfo info, StreamingContext context) : base(info, context)
        { 
        }
 
		///  
		///    Internal method to return an IWbemContext representation
 		///    of the named value collection. 
		/// 
 		internal IWbemContext GetContext()
 		{
			IWbemContext wbemContext = null; 

 			// Only build a context if we have something to put in it 
			if (0 < Count) 
			{
				int status = (int)ManagementStatus.NoError; 

 				try {
					wbemContext = (IWbemContext) new WbemContext ();
 
 					foreach (string name in this)
 					{ 
						object val = base.BaseGet(name); 
 						status = wbemContext.SetValue_ (name, 0, ref val);
						if ((status & 0x80000000) != 0) 
						{
							break;
 						}
					} 
 				} catch {}	//
 			} 
			 
 			return wbemContext;
		} 

		/// 
		///     Adds a single-named value to the collection.
 		///  
		/// The name of the new value.
 		/// The value to be associated with the name. 
 		public void Add (string name, object value) 
		{
 			// Remove any old entry 
			try
			{
				base.BaseRemove (name);
 			} catch {} 

			base.BaseAdd (name, value); 
 			FireIdentifierChanged (); 
 		}
 
		/// 
 		///     Removes a single-named value from the collection.
		///       If the collection does not contain an element with the
		///       specified name, the collection remains unchanged and no 
		///       exception is thrown.
 		///  
		/// The name of the value to be removed. 
 		public void Remove (string name)
 		{ 
			base.BaseRemove (name);
 			FireIdentifierChanged ();
		}
 
		/// 
		///    Removes all entries from the collection. 
 		///  
		public void RemoveAll ()
 		{ 
 			base.BaseClear ();
			FireIdentifierChanged ();
 		}
 
		/// 
		///    Creates a clone of the collection. Individual values 
		///       are cloned. If a value does not support cloning, then a  
 		///       is thrown. 
		///  
 		/// 
 		///    The new copy of the collection.
		/// 
 		public ManagementNamedValueCollection Clone () 
		{
			ManagementNamedValueCollection nvc = new ManagementNamedValueCollection(); 
 
			foreach (string name in this)
 			{ 
				// If we can clone the value, do so. Otherwise throw.
 				object val = base.BaseGet (name);

 				if (null != val) 
				{
 					Type valueType = val.GetType (); 
					 
					if (valueType.IsByRef)
					{ 
 						try
						{
 							object clonedValue = ((ICloneable)val).Clone ();
 							nvc.Add (name, clonedValue); 
						}
 						catch 
						{ 
							throw new NotSupportedException ();
						} 
 					}
					else
 					{
 						nvc.Add (name, val); 
					}
 				} 
				else 
					nvc.Add (name, null);
			} 

 			return nvc;
		}
 
 		/// 
 		///    Returns the value associated with the specified name from this collection. 
		///  
 		/// The name of the value to be returned.
		///  
		/// An  containing the
		///    value of the specified item in this collection.
 		/// 
		public object this[string name] 
 		{
 			get { 
				return base.BaseGet(name); 
            }
 		} 
    }

}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.

                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK