273 lines
5.9 KiB
C#

using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Text;
namespace Intel.Management.Wsman
{
class WsmanItem : IWsmanItem
{
private object _object;
internal class ObjectAndRef
{
public IManagedInstance _inst;
public IManagedReference _ref;
public ObjectAndRef(IManagedInstance instObj, IManagedReference refObj)
{
_inst = instObj;
_ref = refObj;
}
}
internal class NullObject {}
internal class ItemEnumerator : IEnumerator
{
WsmanItem _item;
int _index;
public ItemEnumerator(WsmanItem item)
{
_item = item;
_index = -1;
}
public bool HasNext
{
get
{
// can't enumerator a null object
if (_item.IsNull || _item.InternalArray == null)
return false;
if (_item.IsArray)
return (_index + 1) < _item.InternalArray.Length;
return _index < 0;
}
}
public object Current
{
get
{
if (_index<0)
throw new InvalidOperationException();
if (_item.IsArray)
{
if (_item.InternalArray == null || _index>=_item.InternalArray.Length)
throw new InvalidOperationException();
return _item.InternalArray[_index];
}
if (_index>0)
throw new InvalidOperationException();
return _item;
}
}
public bool MoveNext()
{
if (_item.IsNull)
return false;
_index++;
if (_item.IsArray)
{
if (_item.InternalArray == null)
return false;
return _index < _item.InternalArray.Length;
}
return _index < 1;
}
public void Reset()
{
_index = -1;
}
} // end WsmanEnumerator
public WsmanItem(IManagedInstance instObj,IManagedReference refObj)
{
_object = new ObjectAndRef(instObj, refObj);
}
public WsmanItem()
{
_object = new NullObject();
}
public WsmanItem(object obj)
{
_object = obj;
}
public object TypedObject
{
get
{
return null;
}
}
public IManagedInstance Object
{
get
{
if (IsObjectAndRef)
return ((ObjectAndRef)_object)._inst;
return (IManagedInstance)_object;
}
}
public IManagedReference Ref
{
get
{
if (IsObjectAndRef)
return ((ObjectAndRef)_object)._ref;
if (IsRef)
return (IManagedReference)_object;
return null;
}
}
public bool IsArray
{
get
{
return _object.GetType().IsArray;
}
}
public bool IsTyped
{
get
{
return false;
}
}
public bool IsObject
{
get
{
return _object is IManagedInstance;
}
}
public bool IsRef
{
get
{
return _object is IManagedReference;
}
}
public bool IsObjectAndRef
{
get
{
return _object is ObjectAndRef;
}
}
public bool IsString
{
get
{
return _object is string;
}
}
public bool IsNull
{
get
{
return _object is NullObject;
}
}
public int Count
{
get
{
if (IsArray)
return ((IWsmanItem[])_object).Length;
if (_object is NullObject)
return 0;
return 1;
}
}
public IWsmanItem Item(int index)
{
if (index >= Count)
throw new IndexOutOfRangeException();
if (IsArray)
return ((IWsmanItem[])_object)[index];
return this;
}
[DispId(-4)]
public IEnumerator GetEnumerator()
{
return new ItemEnumerator(this);
}
public override string ToString()
{
return _object.ToString();
}
public bool IsA(string typeName)
{
bool result=false;
if (IsObject)
{
if (Object.SimpleName.Equals(typeName) ||
Object.ResourceUri.Equals(typeName))
result = true;
}
else if (IsRef)
{
if (Ref.SimpleName.Equals(typeName) ||
Ref.ResourceUri.Equals(typeName) )
result = true;
}
return result;
}
public object[] InternalArray
{
get
{
return _object as object[];
}
}
public object InternalObject
{
get
{
return _object;
}
}
}
}